1. Overview
In this article, We'll learn how to convert the time from epoch milliseconds to LocalDate or LocalDateTime in java 8.
But, Converting epoch time to LocalDate or LocalDateTime can not be done directly so we need to convert first to ZonedDateTime and then next to needed type such as LocalDate or LocalDateTime.
Use Instant.ofEpochMilli(epochMilliSeconds).atZone(ZoneId.systemDefault()) method and next use either toLocalDate() or toLocalDateTime() methods.
2. Java 8 - Convert Epoch to LocalDate
Below is the sample program to get the LocalDate from epoch time.
In this program, first we have shown the step by step to get the required objects to get the LocalDate and finally shown the simplified the code in single line to get the same.
package com.javaprogramto.java8.dates.milliseconds; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; /** * * java Example to convert millisconds to LocalDate * * @author JavaProgramTo.com * */ public class EpochMillisToLocalDate { public static void main(String[] args) { // current time in epoch format long epochMilliSeconds = 1608647457000l; // epoch time to Instant Instant instant = Instant.ofEpochMilli(epochMilliSeconds); // Instant to ZonedDateTime ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault()); // ZonedDateTime to LocalDate LocalDate localDate1 = zonedDateTime.toLocalDate(); System.out.println("LocalDate 1 : "+localDate1); // simplified code LocalDate localDate2 = Instant.ofEpochMilli(epochMilliSeconds).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println("LocalDate 2 : "+localDate2); } }
Output:
LocalDate 1 : 2020-12-22 LocalDate 2 : 2020-12-22
3. Java 8 - Convert Epoch to LocalDateTime
The below example is to retrieve the LocalDateTime object from milliseconds.
package com.javaprogramto.java8.dates.milliseconds; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; /** * * java Example to convert millisconds to LocalDateTime * * @author JavaProgramTo.com * */ public class EpochMillisToLocalDateTime { public static void main(String[] args) { // current time in epoch format long epochMilliSeconds = 1608647457000l; // epoch time to Instant Instant instant = Instant.ofEpochMilli(epochMilliSeconds); // Instant to ZonedDateTime ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault()); // ZonedDateTime to LocalDate LocalDateTime localDateTime1 = zonedDateTime.toLocalDateTime(); System.out.println("LocalDateTime 1 : "+localDateTime1); // simplified code LocalDateTime localDateTime2 = Instant.ofEpochMilli(epochMilliSeconds).atZone(ZoneId.systemDefault()).toLocalDateTime(); System.out.println("LocalDateTime 2 : "+localDateTime2); } }
Output:
LocalDateTime 1 : 2020-12-22T20:00:57 LocalDateTime 2 : 2020-12-22T20:00:57
4. Conclusion
In this article, we've seen how to do conversion epoch millis to LocalDate or LocalDateTime in java 8.
No comments:
Post a Comment
Please do not add any spam links in the comments section.