Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / Java

How to Convert an Instant to LocalDateTime or LocalDate or LocalTime in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Feb 2023CPOL3 min read 5.7K   5   1
Convert Instant to LocalDateTime/LocalDate/LocalTime
In this tutorial, we see how to convert an Instant to LocalDateTime/LocalDate/LocalTime in Java with two simple steps.
Image 1

1. What is Instant in Java?

In Java, Instant is a class in the Java Standard Library (java.time package) that represents a specific point in time without any timezone associated with it, i.e., UTC. An Instant is an immutable representation of a point in time, with a resolution of nanoseconds. It is used to represent timestamps, elapsed time, and other time-related values.

Instant can be created from epoch time, which is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC, or from other date and time-related values, such as LocalDateTime or ZonedDateTime.

An Instant in Java does not have a time zone associated with it. An Instant represents a single point in time, independent of any particular time zone. The value of an Instant is the same regardless of where in the world it is being used, and it does not change based on daylight saving time or other time zone changes.

2. What is LocalDateTime, LocalDate, LocalTime?

LocalDateTime, LocalDate and LocalTime are classes in the Java Standard Library (java.time package) that represent date and time information in a more human-readable format.

LocalDateTime represents a date and time without a time zone, such as 2022-01-20 12:30:15.

LocalDate represents a date without a time or time zone, such as 2022-01-20.

LocalTime represents a time without a date or time zone, such as 12:30:15.

These classes are used when you don't need to track time zones and just need to represent date and time values in a more readable format. They are also used when you need to perform operations on date and time values that don't require knowledge of a specific time zone.

LocalDateTime, LocalDate and LocalTime still provide methods to obtain the current date and time in the default time zone (local time zone where JVM is running) without storing time zone details.

LocalDateTime, LocalDate, LocalTime are not the same across the world. LocalDateTime represents a date and time in the context of a specific time zone. Different time zones have different offsets from Coordinated Universal Time (UTC), so the same LocalDateTime in one time zone will have a different equivalent in another time zone.

Now, let's look at how to convert Instant to LocalDateTime or LocalDate, or LocalTime.

3. Convert from Instant to LocalDateTime/LocalDate/LocalTime

There are two ways to convert from Instant:

3.1. Using atZone() and specifying a ZoneId

The Instant class provides a method atZone which returns ZonedDateTime at the specified time zone. Using this ZonedDateTime, we can convert to LocalDateTime and others.

Java
Instant instant = Instant.now();

LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();

LocalTime localTime = instant.atZone(ZoneId.systemDefault()).toLocalTime();
As Instant is always in the UTC time zone, we must first convert it to ZonedDateTime at a specific zone and then need to convert it to LocalDateTime/LocalDate/LocalTime.

3.2. Using ofInstant() of LocalDateTime/LocalDate/LocalTime

All these three classes provide the method ofInstant which accepts Instant and zone ID and converts it to an object of the respective class:

Java
Instant instant = Instant.now();

LocalDateTime.ofInstant(instant, ZoneId.systemDefault())

LocalDate.ofInstant(instant, ZoneId.systemDefault())

LocalTime.ofInstant(instant, ZoneId.systemDefault())

Even though LocalDateTime/LocalDate/LocalTime don't store the time zone information, they always represent date & time in the context of a specific time zone, be it either the system default time zone or any specific time zone across the world. So we need to pass Zone Id along with instant.

4. Conclusion

In this tutorial, we have seen how to convert an Instant to LocalDateTime/LocalDate/LocalTime in Java with two simple steps.

This article was originally posted at https://www.thebackendpro.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 5 Pin
Mirzakhmet Syzdykov8-Feb-23 22:47
professionalMirzakhmet Syzdykov8-Feb-23 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.