VOOZH about

URL: https://dzone.com/articles/practical-guide-for-converting-between-date-and-te

⇱ Practical Guide For Converting Between Date and Temporal


Related

  1. DZone
  2. Coding
  3. Languages
  4. Practical Guide For Converting Between Date and Temporal

Practical Guide For Converting Between Date and Temporal

Check out this practical code guide on converting between Date and Temporal classes in Java.

Likes
Comment
Save
32.3K Views

Join the DZone community and get the full member experience.

Join For Free

πŸ‘ Temporal and Date in Java

Check out this guide on how to convert between Date and Temporal classes in Java.

This article will cover the following Temporalclassesβ€” Instant,  LocalDate,  LocalDateTime,  ZonedDateTime, OffsetDateTime,  LocalTime, and OffsetTime.

You may also like: Java Date and Time

Date – Instant

In order to convert from Dateto Instant, the solution can rely on the Date.toInstant()method. The reverse can be accomplished via the Date.from(Instant instant)method:

  •  Dateto Instantcan be accomplished like this:

Date date = new Date();

// e.g., 2019-02-27T12:02:49.369Z, UTC
Instant instantFromDate = date.toInstant();
  •  Instant tDate 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 that  Date is not time-zone aware, but it is displayed in the system default time zone (for example, via   toString()).   Instantis 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:

πŸ‘ Image title

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

Dateobject can be converted to LocalDatevia an Instantobject. Once we have obtained the Instantobject from the given Dateobject, 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 LocalDateto Dateshould take into account that LocalDatedoesn'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 Dateto DateLocalTimeis the same as converting from Dateto  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 LocalDateTimeto Dateis 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 Dateto ZonedDateTimecan be accomplished via the Instantobject obtained from the given Dateobject 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 ZonedDateTimeto Dateis just about converting ZonedDateTimeto  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 Dateto OffsetDateTimerelies 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 OffsetDateTimeto Daterequires two steps. First, convert OffsetDateTimeto LocalDateTime. Second, convert LocalDateTimeto  Instantwith 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 Dateto LocalTimecan 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 LocalTimeto Dateshould take into account that LocalTimedoesn'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 Dateto OffsetTimecan 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 OffsetTimeto Dateshould take into account that OffsetTimedoesn'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

Object (computer science) Java (programming language) Convert (command) Snippet (programming) Coding (social sciences) Book GitHub

Opinions expressed by DZone contributors are their own.

Related

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Writing DTOs With Java8, Lombok, and Java14+
  • Redefining Java Object Equality
  • How to Convert Files to Thumbnail Images in Java

Partner Resources

Γ—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: