Practical Guide For Converting Between Date and Temporal
This article will cover the following Temporal
classes— Instant
, LocalDate
, LocalDateTime
, ZonedDateTime
, OffsetDateTime
, LocalTime
, and OffsetTime
.
You may also like: Java Date and Time
Date – Instant
In order to convert from Date
to Instant
, the solution can rely on the Date.toInstant()
method. The reverse can be accomplished via the Date.from(Instant instant)
method:
Date
toInstant
can be accomplished like this:
Date date = new Date();
// e.g., 2019-02-27T12:02:49.369Z, UTC
Instant instantFromDate = date.toInstant();
Instant
toDate
can be accomplished like this:
Instant instant = Instant.now();
// Wed Feb 27 14:02:49 EET 2019, default system time zone
Date dateFromInstant = Date.from(instant);
Keep in mind thatDate
is not time-zone aware, but it is displayed in the system default time zone (for example, viatoString()
).Instant
is with a UTC time zone.
Let's quickly wrap these snippets of code in two utility methods, defined in a utility
class — DateConverters
:
public static Instant dateToInstant(Date date) {
return date.toInstant();
}
public static Date instantToDate(Instant instant) {
return Date.from(instant);
}
Further, let's enrich this class with the methods from the following screenshot:
The constant from the screenshot, DEFAULT_TIME_ZONE
, is the system default time zone:
public static final ZoneId DEFAULT_TIME_ZONE = ZoneId.systemDefault();
Date – LocalDate
A Date
object can be converted to LocalDate
via an Instant
object. Once we have obtained the Instant
object from the given Date
object, the solution can apply to it the system default time zone, and call the toLocaleDate()
method:
// e.g., 2019-03-01
public static LocalDate dateToLocalDate(Date date) {
return dateToInstant(date).atZone(DEFAULT_TIME_ZONE).toLocalDate();
}
Converting from LocalDate
to Date
should take into account that LocalDate
doesn't contain a time component as Date
, so the solution must supply a time component as the start of the day:
// e.g., Fri Mar 01 00:00:00 EET 2019
public static Date localDateToDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(
DEFAULT_TIME_ZONE).toInstant());
}
Date – DateLocalTime
Converting from Date
to DateLocalTime
is the same as converting from Date
to LocalDate
, apart from the fact that the solution should call the toLocalDateTime()
method as follows:
// e.g., 2019-03-01T07:25:25.624
public static LocalDateTime dateToLocalDateTime(Date date) {
return dateToInstant(date).atZone(
DEFAULT_TIME_ZONE).toLocalDateTime();
}
Converting from LocalDateTime
to Date
is straightforward. Just apply the system default time zone and call toInstant()
:
// e.g., Fri Mar 01 07:25:25 EET 2019
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(
DEFAULT_TIME_ZONE).toInstant());
}
Date – ZonedDateTime
Converting Date
to ZonedDateTime
can be accomplished via the Instant
object obtained from the given Date
object and the system default time zone:
// e.g., 2019-03-01T07:25:25.624+02:00[Europe/Athens]
public static ZonedDateTime dateToZonedDateTime(Date date) {
return dateToInstant(date).atZone(DEFAULT_TIME_ZONE);
}
Converting ZonedDateTime
to Date
is just about converting ZonedDateTime
to Instant
:
// e.g., Fri Mar 01 07:25:25 EET 2019
public static Date zonedDateTimeToDate(ZonedDateTime zonedDateTime) {
return Date.from(zonedDateTime.toInstant());
}
Date – OffsetDateTime
Converting from Date
to OffsetDateTime
relies on the toOffsetDateTime()
method:
// e.g., 2019-03-01T07:25:25.624+02:00
public static OffsetDateTime dateToOffsetDateTime(Date date) {
return dateToInstant(date).atZone(
DEFAULT_TIME_ZONE).toOffsetDateTime();
}
An approach for converting from OffsetDateTime
to Date
requires two steps. First, convert OffsetDateTime
to LocalDateTime
. Second, convert LocalDateTime
to Instant
with the corresponding offset:
// e.g., Fri Mar 01 07:55:49 EET 2019
public static Date offsetDateTimeToDate(OffsetDateTime offsetDateTime) {
return Date.from(offsetDateTime.toLocalDateTime()
.toInstant(ZoneOffset.of(offsetDateTime.getOffset().getId())));
}
Date – LocalTime
Converting Date
to LocalTime
can rely on the LocalTime.toInstant()
method as
follows:
// e.g., 08:03:20.336
public static LocalTime dateToLocalTime(Date date) {
return LocalTime.ofInstant(dateToInstant(date), DEFAULT_TIME_ZONE);
}
Converting LocalTime
to Date
should take into account that LocalTime
doesn't have a date component. This means that the solution should set the date on January 1, 1970, the epoch:
// e.g., Thu Jan 01 08:03:20 EET 1970
public static Date localTimeToDate(LocalTime localTime) {
return Date.from(localTime.atDate(LocalDate.EPOCH)
.toInstant(DEFAULT_TIME_ZONE.getRules()
.getOffset(Instant.now())));
}
Date – OffsetTime
Converting Date
to OffsetTime
can rely on the OffsetTime.toInstant()
method as follows:
// e.g., 08:03:20.336+02:00
public static OffsetTime dateToOffsetTime(Date date) {
return OffsetTime.ofInstant(dateToInstant(date), DEFAULT_TIME_ZONE);
}
Converting OffsetTime
to Date
should take into account that OffsetTime
doesn't have a date component. This means that the solution should set the date on January 1, 1970, the epoch:
// e.g., Thu Jan 01 08:03:20 EET 1970
public static Date offsetTimeToDate(OffsetTime offsetTime) {
return Date.from(offsetTime.atDate(LocalDate.EPOCH).toInstant());
}
Done! The complete code is available on GitHub.
If you enjoyed this article, then I'm sure that you will love my book, Java Coding Problems, which contains an entire chapter dedicated to date and time problems.
Further Reading
Java Date and Time
A Deeper Look Into the Java 8 Date and Time API