Java Date Time Java
Following utility methods convert Java 8 LocalTime , LocalDate and LocalDateTime to java.util.Calendar and java.util.Date .
package com.logicbig.example;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
public class LocalDateTimeToDate {
public static Date localTimeToDate(LocalTime localTime) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
//assuming year/month/date information is not important
calendar.set(0, 0, 0, localTime.getHour(), localTime.getMinute(), localTime.getSecond());
return calendar.getTime();
}
public static Date localDateToDate(LocalDate localDate) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
//assuming start of day
calendar.set(localDate.getYear(), localDate.getMonthValue()-1, localDate.getDayOfMonth());
return calendar.getTime();
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(localDateTime.getYear(), localDateTime.getMonthValue()-1, localDateTime.getDayOfMonth(),
localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond());
return calendar.getTime();
}
public static void main(String[] args) {
Date date = localTimeToDate(LocalTime.now());
System.out.println(date);
date = localDateToDate(LocalDate.now());
System.out.println(date);
date = localDateTimeToDate(LocalDateTime.now());
System.out.println(date);
}
}
Wed Dec 31 13:18:27 CST 2 Mon Jun 11 00:00:00 CDT 2018 Mon Jun 11 13:18:27 CDT 2018
Above methods can be useful for simple cases. Use following methods if you want the conversion to be more accurate and want to target a specific time zone.
package com.logicbig.example;
import java.time.*;
import java.util.Date;
public class LocalDateTimeToDate2 {
public static Date localTimeToDate(LocalTime localTime, int year, int month, int day,
ZoneOffset zoneOffset) {
LocalDateTime dateTime = localTime.atDate(LocalDate.of(year, month, day));
return localDateTimeToDate(dateTime, zoneOffset);
}
public static Date localTimeToDate(LocalTime localTime, int year, int month, int day,
ZoneId zoneId) {
LocalDateTime dateTime = localTime.atDate(LocalDate.of(year, month, day));
return localDateTimeToDate(dateTime, zoneId);
}
public static Date localDateToDate(LocalDate localDate, ZoneOffset zoneOffset) {
LocalDateTime dateTime = localDate.atStartOfDay();//assuming start of day
return localDateTimeToDate(dateTime, zoneOffset);
}
public static Date localDateToDate(LocalDate localDate, ZoneId zoneId) {
LocalDateTime dateTime = localDate.atStartOfDay();//assuming start of day
return localDateTimeToDate(dateTime, zoneId);
}
public static Date localDateTimeToDate(LocalDateTime localDateTime, ZoneId zoneId) {
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
return zoneDateTimeToDate(zonedDateTime);
}
public static Date localDateTimeToDate(LocalDateTime localDateTime, ZoneOffset zoneOffset) {
OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffset);
return offsetDateTimeToDate(offsetDateTime);
}
public static Date offsetDateTimeToDate(OffsetDateTime offsetDateTime) {
return Date.from(offsetDateTime.toInstant());
}
public static Date zoneDateTimeToDate(ZonedDateTime zonedDateTime) {
return Date.from(zonedDateTime.toInstant());
}
public static void main(String[] args) {
System.out.println("-- using ZoneOffset --");
ZoneOffset zoneOffset = ZoneOffset.from(OffsetDateTime.now());
System.out.println("ZoneOffset: " + zoneOffset);
Date date = localTimeToDate(LocalTime.now(), 2000, 1, 1, zoneOffset);
System.out.println(date);
date = localDateToDate(LocalDate.now(), zoneOffset);
System.out.println(date);
date = localDateTimeToDate(LocalDateTime.now(), zoneOffset);
System.out.println(date);
date = offsetDateTimeToDate(OffsetDateTime.now());
System.out.println(date);
System.out.println("-- using ZoneId --");
ZoneId zoneId = ZoneId.from(ZonedDateTime.now());
System.out.println("ZoneId: " + zoneId);
date = localTimeToDate(LocalTime.now(), 2000, 1, 1, zoneOffset);
System.out.println(date);
date = localDateToDate(LocalDate.now(), zoneOffset);
System.out.println(date);
date = localDateTimeToDate(LocalDateTime.now(), zoneOffset);
System.out.println(date);
date = zoneDateTimeToDate(ZonedDateTime.now());
System.out.println(date);
}
}
-- using ZoneOffset -- ZoneOffset: -05:00 Sat Jan 01 12:18:33 CST 2000 Mon Jun 11 00:00:00 CDT 2018 Mon Jun 11 13:18:33 CDT 2018 Mon Jun 11 13:18:33 CDT 2018 -- using ZoneId -- ZoneId: America/Chicago Sat Jan 01 12:18:33 CST 2000 Mon Jun 11 00:00:00 CDT 2018 Mon Jun 11 13:18:33 CDT 2018 Mon Jun 11 13:18:33 CDT 2018
To convert java.util.Date to java.util.Calendar and vice-versa use following methods:
package com.logicbig.example;
import java.util.Calendar;
import java.util.Date;
public class DateToCalendar {
public Calendar dateToCalendar(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static Date calendarToDate(Calendar calendar) {
return calendar.getTime();
}
}
|
|