Java – Convert Instant to LocalDateTime, LocalDate or LocalTime

Learn to convert a given Instant to LocalDateTime, LocalDate, or LocalTime instances in the current system timezone or a specified timezone in Java.

1. Difference between Instant and LocalDateTime

An Instant is an instantaneous point on the time-line without any timezone information associated with it. Internally, it uses the system UTC clock to obtain the instant so, technically, an instant is a point on the time-line in UTC timezone (Zero offset).

A LocalDateTime is an instant in the current timezone or local timezone. It resembles the timeline as recived from the user’s computer where the program is running. LocalDateTime is also without any timezone associated with it, so internally it is adjusted to system’s default timezone.

To convert from an Instant to LocalDateTime, we need to adjust the offset difference between the UTC and local timezones.

2. Converting From Instant to LocalDateTime, LocalDate or LocalTime

There are generally two simple ways to convert a given Instant to LocalDateTime instance:

2.1. Using LocalDateTime.ofInstant()

we can use the LocalDateTime.ofInstant(), LocalDateTime.ofInstant() or LocalDateTime.ofInstant() methods to get a local date or time instance in the specified timezone.

In following example, we are using the ZoneOffset.systemDefault() so it adjusts the instant to current timezone of the machine where program is run.

Instant instant = Instant.now();	//2023-02-02T08:22:04.824071900Z

LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.systemDefault());	//2023-02-02T13:52:04.824071900
LocalDate ld = LocalDate.ofInstant(instant, ZoneOffset.systemDefault());		//2023-02-02
LocalDate lt = LocalDate.ofInstant(instant, ZoneOffset.systemDefault());		//13:52:04.824071900

If we have deployed our application in cloud infrastructure then it is hard to guess the timezone where the application will run. In this case, to find the LocalDateTime in a specific timezone only, we can pass the spcific timezone information in the method.

Instant instant = Instant.now();	//2023-02-02T08:22:04.824071900Z

LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneOffset.of("+05:30"));
LocalDate ld = LocalDate.ofInstant(instant, ZoneOffset.of("+05:30"));
LocalTime lt = LocalTime.ofInstant(instant, ZoneOffset.of("+05:30"));

2.2. Using Instant.atZone()

The basic principle remains same that we have to adjust the timezone offset between the UTC and system’s default timezone. The atZone() method combines the instant with the specified time-zone and returns ZonedDateTime.

Finally, we use the ZonedDateTime‘s toLocalDateTime(), toLocalDate() or toLocalTime() methods to get the respective instant in local timezone.

Instant instant = Instant.now();

LocalDateTime ldt = instant.atZone(ZoneOffset.systemDefault()).toLocalDateTime();
LocalDate ld = instant.atZone(ZoneOffset.systemDefault()).toLocalDate();
LocalTime lt = instant.atZone(ZoneOffset.systemDefault()).toLocalTime();

We can specify any other zoneoffset to obtain the local date-time in any other time-zone.

3. Converting From LocalDateTime to Instant

Opposite to previous examples, for converting a LocalDateTime to an Instant in UTC, we must remove the
zone offset effect. For this conversion, convert the LocalDateTime to ZonedDateTime in default time-zone, adjust the ZoneDateTime to UTC, and then get the Instant in UTC.

LocalDateTime ldt = LocalDateTime.now();

Instant instant = ldt.atZone(ZoneId.systemDefault())
    .withZoneSameInstant(ZoneId.of("UTC"))
    .toInstant();

4. Conclusion

In this short Java tutorial, we learned to convert between LocalDateTime and Instant instances using Java Date Time API. We learned about the effect of time-zone in both classes and how to adjust the zone offsets to convert between them.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode