java - Epoch or Unix timestamp and convert to from Date with examples

java - Beginner Guide to Epoch Time with examples

What is the Current Epoch time?

Epoch time is many seconds that already passed from 1 January 1970 UTC.

For a day, the total number of seconds is 24* 60*60 seconds. Epoch time returns the long number between the current time and 1 January 1970.

It is also called Epoch Unix time.

Every programming language provides API for handling Unix time. Java also provides a Date API for manipulation of Epoch Time.

The reason for 1970 is time in UNIX OS was introduced by taking this time.

EPOCH time = unix epoch time = Number of millseconds 01/01/1970 00:00:00

Epcoh/Unix time Examples in java

This tutorial talks about frequently used examples for manipulation of Epoch time in java.

  • Find epoch time in
  • Convert Date and time to milliseconds
  • Convert epoch time to LocalDate and LocalDateTime

How to get Epoch time or Unix timestamp in java?

Epoch time is a number of seconds that already passed since 1970 January 1 UTC. Prior to java8, Used System class to get Epoch time,

long epochTimeinSeconds = System.currentTimeMillis() / 1000L;  

With java8, Date and Time API is improved and introduced java.time package for dealing with date, time, and zones over legacy classes.\ Instant is a class of points in a time zone.

long epochTimewithJava8 = Instant.now().getEpochSecond();  

Both return the same output


How to convert Date and time to Epoch time seconds?

java.time.Instant class provides the toEpochMilli() method to convert the date with time into epoch milliseconds.

Here is a code for converting Date and time into epoch milliseconds

long epochTimewithJava81= Instant.now().toEpochMilli(); //Long = 1450879900184  

Convert epoch time java.time.LocalDateTime

LocalDateTime represents Date and time zone information, epoch time is in long seconds. For this, we need to have the System Default Timezone retrieved and the Instant object is required to convert to it.

long epochTimewithJava8 = System.currentTimeMillis();  
Instant instant = Instant.ofEpochMilli(epochTimewithJava8);  
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();  

The output of the above java program is


Convert epoch time to LocalDate

LocalDate represents date only. To convert, Instant object creation is required with the default timezone.

long epochTimewithJava8 = System.currentTimeMillis();  
Instant instant = Instant.ofEpochMilli(epochTimewithJava8);  
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();  

Output is


How to Convert String Date to Epoch Time

String data is first converted to Date using SimpleDateFormat. The date format class accepts a formatted string and returns the Date object by calling the parse()method

String str="2018-09-03T04:51:00+05:0";  
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");  
Date date=null;  
try {  
 date = parser.parse(str);  
 } catch (ParseException e) {  
 // TODO Auto-generated catch block  

Output is



You learned epoch milliseconds in java with examples and convert from/to date,localdate, and LocalDatetime from Unix milliseconds.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.