- Convert between Java LocalDate and Epoch
- 1. LocalDate to Epoch
- 1.1 LocalDate to Epoch Days
- 1.2 LocalDate to Epoch Seconds
- 1.3 LocalDate to Epoch Milliseconds
- 2. Epoch to LocalDate
- 2.1 Epoch to LocalDate using LocalDate.ofEpochDay()
- 2.2 Epoch to LocalDate using Instant
- 2.3 Epoch to LocalDate using LocalDateTime
- 2.4 Epoch to LocalDate using Timestamp
- Convert Milliseconds To LocalDateTime In Java 8
- 1. Convert Milliseconds to LocalDateTime
- 2. Convert Milliseconds to LocalDate
- 3. Convert LocalDateTime to Milliseconds
- 4. Convert LocalDate to Milliseconds
- 5. Get TimeZone
- 6. Conclusion
- How To Convert Epoch time MilliSeconds to LocalDate and LocalDateTime in Java 8?
- 1. Overview
- 2. Java 8 — Convert Epoch to LocalDate
- 3. Java 8 — Convert Epoch to LocalDateTime
- 4. Conclusion
- Labels:
- SHARE:
- About Us
- Java 8 Tutorial
- Java Threads Tutorial
- Kotlin Conversions
- Kotlin Programs
- Java Conversions
- Java String API
- Spring Boot
- $show=Java%20Programs
- $show=Kotlin
Convert between Java LocalDate and Epoch
This page will provide examples to convert between Java LocalDate and epoch. An epoch is an instant in time used as an origin of particular calendar era. Epoch is a reference point from which a time is measured. The epoch reference point for LocalDate is 1970-01-01 in YYYY-MM-DD format. When we convert a LocalDate such as 2019-11-15 into epoch days, then the result will be number of days from 1970-01-01 to 2019-11-15. In the same way, when we convert epoch days such as 18215 to LocalDate then the resulting LocalDate will be obtained by adding 18215 days to 1970-01-01.
1. Find the code snippet to covert LocalDate to epoch days using LocalDate.toEpochDay() .
long numberOfDays = localDate.toEpochDay();
LocalDate localDate = LocalDate.ofEpochDay(numberOfDays);
Contents
1. LocalDate to Epoch
To convert LocalDate to epoch days is the days calculation starting from 1970-01-01 up to given local date. To convert LocalDate to epoch seconds or milliseconds is the time calculation starting from 1970-01-01T00:00:00Z up to given local date.
LocalDateToEpoch.java
package com.concretepage; import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; public class LocalDateToEpoch < public static void main(String[] args) < //Epoch reference of date is 1970-01-01 LocalDate localDate = LocalDate.parse("2019-11-15"); //LocalDate to epoch days long numberOfDays = localDate.toEpochDay(); System.out.println(numberOfDays); //LocalDate to epoch seconds long timeInSeconds = localDate.toEpochSecond(LocalTime.NOON, ZoneOffset.MIN); System.out.println(timeInSeconds); //LocalDate to epoch milliseconds Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); long timeInMillis = instant.toEpochMilli(); System.out.println(timeInMillis); instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); timeInMillis = instant.toEpochMilli(); System.out.println(timeInMillis); >>
18215 1573884000 1573756200000 1573756200000
1.1 LocalDate to Epoch Days
toEpochDay() converts this date to epoch Day. The toEpochDay() calculates number of days starting from 1970-01-01 up to given local date. If given local date is 1970-01-01 then count of epoch days will be 0.
LocalDate localDate = LocalDate.parse("2019-11-15"); long numberOfDays = localDate.toEpochDay();
1.2 LocalDate to Epoch Seconds
In Java 9, LocalDate provides toEpochSecond() method to convert local date into epoch seconds. Find the Java doc.
long toEpochSecond(LocalTime time, ZoneOffset offset)
toEpochSecond() converts this LocalDate to the number of seconds since the epoch 1970-01-01T00:00:00Z. The LocalDate is combined with given time and zone offset to calculate seconds starting from 1970-01-01T00:00:00Z.
long timeInSeconds = localDate.toEpochSecond(LocalTime.NOON, ZoneOffset.MIN);
1.3 LocalDate to Epoch Milliseconds
To convert LocalDate to epoch milliseconds, we can use Instant.toEpochMilli() that converts this instant to the number of milliseconds from the epoch 1970-01-01T00:00:00Z. To get epoch milliseconds, first we will convert LocalDate to Instant and then will use its toEpochMilli() method.
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant(); long timeInMillis = instant.toEpochMilli(); instant = localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant(); timeInMillis = instant.toEpochMilli();
2. Epoch to LocalDate
The given epoch days, epoch seconds or epoch milliseconds are converted into LocalDate by adding the given time to 1970-01-01T00:00:00Z. Find the code.
EpochToLocalDate.java
package com.concretepage; import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; public class EpochToLocalDate < public static void main(String[] args) < //Epoch reference of date is 1970-01-01 long numberOfDays = 18215; LocalDate localDate = LocalDate.ofEpochDay(numberOfDays); System.out.println(localDate); //Using Instant long timeInSeconds = 1567109422L; localDate = LocalDate.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); System.out.println(localDate); LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); localDate = localDateTime.toLocalDate(); System.out.println(localDate); long timeInMillis = 1567109422123L; localDate = LocalDate.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault()); System.out.println(localDate); //Using Timestamp localDate = new Timestamp(timeInMillis).toLocalDateTime().toLocalDate(); System.out.println(localDate); >>
2019-11-15 2019-08-30 2019-08-30 2019-08-30 2019-08-30
2.1 Epoch to LocalDate using LocalDate.ofEpochDay()
LocalDate.ofEpochDay() obtains an instance of LocalDate by adding days to 1970-01-01. Find the Java doc.
static LocalDate ofEpochDay(long epochDay)
LocalDate localDate = LocalDate.ofEpochDay(numberOfDays);
2.2 Epoch to LocalDate using Instant
Java 9 LocalDate.ofInstant() accepts Instant and zone id and returns LocalDate object. Find the Java doc.
static LocalDate ofInstant(Instant instant, ZoneId zone)
Instant provides following methods to handle epoch.
1. The below method obtains an instance of Instant using seconds from the epoch 1970-01-01T00:00:00Z.
static Instant ofEpochSecond(long epochSecond)
localDate = LocalDate.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault());
2. The below method obtains an instance of Instant using milliseconds from the epoch 1970-01-01T00:00:00Z.
static Instant ofEpochMilli(long epochMilli)
localDate = LocalDate.ofInstant(Instant.ofEpochMilli(timeInMillis), ZoneId.systemDefault());
2.3 Epoch to LocalDate using LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timeInSeconds), ZoneId.systemDefault()); localDate = localDateTime.toLocalDate();
2.4 Epoch to LocalDate using Timestamp
public Timestamp(long time)
This will construct a Timestamp object using milliseconds time value since 1970-01-01T00:00:00Z.
Find the code snippet.
localDate = new Timestamp(timeInMillis).toLocalDateTime().toLocalDate();
Convert Milliseconds To LocalDateTime In Java 8
This quick tutorial is going to cover how to convert milliseconds to LocalDateTime in Java 8 and vice versa.
1. Convert Milliseconds to LocalDateTime
Let’s see the following example which converts milliseconds to LocalDateTime in Java:
Instant . ofEpochMilli ( milliseconds ) . atZone ( ZoneId . systemDefault ( ) ) . toLocalDateTime ( ) ;
The conversion was done in the system default TimeZone. Let’s see another example which we use the UTC timezone:
2. Convert Milliseconds to LocalDate
The following example will convert milliseconds to LocalDate:
We have converted using the system default timezone. However, we can change the timezone easily by providing the correct TimeZone. Let’s try another example with the UTC timezone:
LocalDate utcDate = Instant . ofEpochMilli ( milliseconds ) . atZone ( ZoneId . of ( «UTC» ) ) . toLocalDate ( ) ;
As for the list of available timezone, please refer to the section #5.
3. Convert LocalDateTime to Milliseconds
Let’s see how we can convert LocalDateTime to milliseconds by the following example:
long localDTInMilli = localDT . atZone ( ZoneId . systemDefault ( ) ) . toInstant ( ) . toEpochMilli ( ) ;
We have done the conversion in the system default timezone. However, we can change to another timezone if needed. Let’s see an example which we use the UTC timezone:
long localUTDTInMilli = localUTCDT . atZone ( ZoneId . of ( «UTC» ) ) . toInstant ( ) . toEpochMilli ( ) ;
4. Convert LocalDate to Milliseconds
Let’s see how we can convert LocalDate to Milliseconds by in below example:
5. Get TimeZone
We can obtain a list of available timezones for reference by using the TimeZone.getAvailableIDs() method. Let’s see an example which we print out the list of available timezone:
[ Africa / Abidjan , Africa / Accra , Africa / Addis_Ababa , Africa / Algiers , Africa / Asmara , Africa / Asmera , . . .6. Conclusion
The tutorial has just illustrated how to convert milliseconds to LocalDateTime, how to convert milliseconds to LocalDate and vice versa in Java 8. Note that we should provide the correct Timezone in order to have exact conversions.
Below are other related articles for your references:
How To Convert Epoch time MilliSeconds to LocalDate and LocalDateTime in Java 8?
A quick guide to convert epoch time from milli seconds to LocalDate or LocalDateTime using java 8 api.
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
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); >>
LocalDate 1 : 2020-12-22 LocalDate 2 : 2020-12-22
3. Java 8 — Convert Epoch to LocalDateTime
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); >>
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.
Labels:
SHARE:
About Us
Java 8 Tutorial
- Java 8 New Features
- Java 8 Examples Programs Before and After Lambda
- Java 8 Lambda Expressions (Complete Guide)
- Java 8 Lambda Expressions Rules and Examples
- Java 8 Accessing Variables from Lambda Expressions
- Java 8 Method References
- Java 8 Functional Interfaces
- Java 8 — Base64
- Java 8 Default and Static Methods In Interfaces
- Java 8 Optional
- Java 8 New Date Time API
- Java 8 — Nashorn JavaScript
Java Threads Tutorial
Kotlin Conversions
Kotlin Programs
Java Conversions
- Java 8 List To Map
- Java 8 String To Date
- Java 8 Array To List
- Java 8 List To Array
- Java 8 Any Primitive To String
- Java 8 Iterable To Stream
- Java 8 Stream To IntStream
- String To Lowercase
- InputStream To File
- Primitive Array To List
- Int To String Conversion
- String To ArrayList
Java String API
- charAt()
- chars() — Java 9
- codePointAt()
- codePointCount()
- codePoints() — Java 9
- compareTo()
- compareToIgnoreCase
- concat()
- contains()
- contentEquals()
- copyValueOf()
- describeConstable() — Java 12
- endsWith()
- equals()
- equalsIgnoreCase()
- format()
- getBytes()
- getChars()
- hashcode()
- indent() — Java 12
- indexOf()
- intern()
- isBlank() — java 11
- isEmpty()
- join()
- lastIndexOf()
- length()
- lines()
- matches()
- offsetByCodePoints()
- regionMatches()
- repeat()
- replaceFirst()
- replace()
- replaceAll()
- resolveConstantDesc()
- split()
- strip(), stripLeading(), stripTrailing()
- substring()
- toCharArray()
- toLowerCase()
- transform() — Java 12
- valueOf()
Spring Boot
$show=Java%20Programs
$show=Kotlin
accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
A quick guide to convert epoch time from milli seconds to LocalDate or LocalDateTime using java 8 api.