Java Date Create buildDatetime(Date datePart, Date hourMinutePart)

Here you can find the source of buildDatetime(Date datePart, Date hourMinutePart)

Description

build Datetime

License

Open Source License

Declaration

public static Date buildDatetime(Date datePart, Date hourMinutePart) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final String FORMAT_FULL_DATETIME = "yyyy-MM-dd HH:mm:ss.SSS";
    private static final String FORMAT_DATE = "yyyy-MM-dd";
    private static final String FORMAT_HOUR_MINUTE = "HH:mm";

    public static Date buildDatetime(Date datePart, Date hourMinutePart) {
        String str = formatDate(datePart) + " " + formatHourMinute(hourMinutePart) + ":00.000";
        Date result = parseFullDatetime(str);

        return result;
    }//from  w  w  w.  j  a  v  a2 s.c o m

    public static String formatDate(Date date) {
        return format(date, FORMAT_DATE);
    }

    public static String formatHourMinute(Date date) {
        return format(date, FORMAT_HOUR_MINUTE);
    }

    public static Date parseFullDatetime(String str) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_FULL_DATETIME);
        Date date;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        return date;
    }

    private static String format(Date date, String format) {
        if (date == null) {
            return null;
        }

        String str = new SimpleDateFormat(format).format(date);

        return str;
    }
}

Related

  1. add24HtoDate(Date date)
  2. addTimeToDate(Date date, Date time)
  3. buildDate(int y, int m, int d)
  4. buildDate(String dateAsString)
  5. buildDateFormat(final String pattern)
  6. buildDateTime(String curDate, String curTime, String meridiem)
  7. buildDateTimeUTC(Calendar cal)
  8. castToDate(Object value)
  9. create(int year, int month, int date, TimeZone timeZone)