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

Backend Pro

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 2 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. 


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 3 classes provide the method `ofInstant` which accepts Instant and zone ID and converts it to an object of the respective class. 


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 2 simple steps. 


Tags

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !