- 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
- Epoch Converter and Date/Time in Java
- Output:
- Formatting Dates
- Output:
- Convert from Epoch to Human Readable Date
- Output:
- Convert from Human Readable Date to Epoch
- Output:
- How to convert epoch time to date in java
- Related Posts:
- Epoch date to date java
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();
Epoch Converter and Date/Time in Java
An easy way to get the current date and time in Java is using the Date class which is available in the java.util package.
import java.util.Date; class Main < public static void main(String[] args) < Date date = new Date(); System.out.println(date); >>
Output:
Sat Feb 16 03:50:30 UTC 2019
Formatting Dates
There are multiple ways to format dates.
import java.util.*; import java.text.*; class Main < public static void main(String[] args) < //1st way System.out.println(); Date date = new Date(); System.out.printf("%1$s %2$tB %2$td, %2$tY", "Current date:", date); //2nd way System.out.println("\n"); Date d = new Date(); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy-MM-dd 'at' hh:mm:ss a zzz"); System.out.println("Current Date and Time: " + ft.format(d)); //3rd way System.out.println(); Date d2 = new Date(); // display time and date String str = String.format("Current Date and Time: %tc", d2 ); System.out.printf(str); >>
Output:
Current date: February 16, 2019 Current Date and Time: Sat 2019-02-16 at 03:52:46 AM UTC Current Date and Time: Sat Feb 16 03:52:46 UTC 2019
Click here to learn more about formatting dates.
Convert from Epoch to Human Readable Date
Here is the way in Java to convert epoch or unix time to a human readable date.
import java.util.*; class Main < public static void main(String[] args) < //You could start with a string representation of epoch String epochString = "1549499024"; long epoch = Long.parseLong( epochString ); System.out.println("Convert Epoch " + epoch + " to date: "); Date d = new Date( epoch * 1000 ); //convert epoch seconds to microseconds System.out.println(d); //You could start with a long number epoch long epoch2 = 1550544173; System.out.println(new Date(epoch2 * 1000)); >>
Output:
Convert Epoch 1549499024 to date: Thu Feb 07 00:23:44 UTC 2019 Tue Feb 19 02:42:53 UTC 2019
Convert from Human Readable Date to Epoch
Here is the way in Java to format the date to show as epoch or unix time.
import java.util.*; class Main < public static void main(String[] args) < //Get the unix timestamp using Date Date currentDate = new Date(); long epoch = currentDate.getTime() / 1000; System.out.println("Epoch: " + epoch); //You could use string format to format to unix timestamp Date date = new Date(); String str = String.format("Epoch using string format: %ts", date ); System.out.printf(str); >>
Output:
Epoch: 1550544489 Epoch using string format: 1550544489
How to convert epoch time to date in java
public final class Instant extends Object This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
public static Instant now()
Obtains the current instant from the system clock.This will query the system UTC clock to obtain the current instant.Using this method will prevent the ability to use an alternate time-source for testing because the clock is effectively hard-coded.
public long toEpochMilli()
Converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z. If this instant represents a point on the time-line too far in the future or past to fit in a long milliseconds, then an exception is thrown. If this instant has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.
package com.candidjava.datetime; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; public class ConvertEpochToDate < public static void main(String[] args) < long epoch=Instant.now().toEpochMilli(); System.out.println("Epoch Time:"+epoch); LocalDate date=Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println("Date:"+date); >>
Epoch Time:1605708617207 Date:2020-11-18
Related Posts:
- getting milliseconds from localDatetime in java 8
- converting java.time.localdatetime to java.util.date
- Java get current date and time based on system date
- Java program to convert java.util.Date to…
- Java 8 Epoch LocalDate | ofEpochDay | ofYearDay
- Java get current date without time using LocalDate…
- How to get the Current time in Milliseconds Java…
- How to get Date without Time in Java
- Get system current date time in java
- Java 8 time API
- How to get Year Month Date from Calendar Object in Java
- How to get Month Year Week using OffsetDateTime Object
- How to Convert Date into Timestamp format in Java
- How to convert String to Date in Java in yyyy-mm-dd format
- How to convert string to localdate in java 8
- How To Get System time using LocalDateTime java 8
- formatting date using new date time api
- How to Convert java.util.Date to java.sql.Date
- SimpleDateformat in java To Display Date in…
- How to Calculate Age from Date of birth Java
- How to get NanoSeconds in current Date time in Java
- How To Compare Two Dates in java using java 8 LocalDateTime
- How to Get Day from Date in Java using Calendar Object
- How to sort dates in java
- Java convert seconds to days hours minutes
Epoch date to date java
Java is a popular object oriented programming language. It is intended to let developers write once, run anywhere compiled Java code that run on all platforms that support Java without the need for recompilation. Java has java.time package that work with date and time. So with Java, we we can easily handle epoch or Unix timestamp conversion into human readable dates or can convert human readable dates to Unix timestamp.
Here we will explain Java classes and methods to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.
Get current epoch or Unix timestamp in Java
We can get the current epoch or timestamp using Date class from Java. It will returns the current epoch in the number of seconds.
Date date = new Date();
long unixTime = date.getTime() / 1000L;
System.out.println(unixTime);
Convert epoch or Unix timestamp to human readable date in Java
We can convert the epoch or timestamp to readable date format using Java Date() class. The function formats and return epoch or timestamp to human readable date and time.
Date date = new Date(1585990763);
SimpleDateFormat format = new SimpleDateFormat(«yyyy-MM-dd HH:mm:ss»);
String myDate = format.format(date);
Output
2020-04-05:05:36::40
Convert date to epoch or unix timestamp in Java
We can convert human readable date to timestamp using Java Date class. The function convert English textual datetime into a Unix timestamp.
String myDate = «2020-04-05 05:36:40»;
SimpleDateFormat dateFormat = new SimpleDateFormat(«yyyy-MM-dd HH:mm:ss.SSS»);
Date date = dateFormat.parse(myDate);
long epoch = date.getTime();
System.out.println(epoch);
Output
1585990763