How can Java get Epoch time, and what is it?

TARA RAM GOYAL
2 min readJul 30, 2023

--

We’ll investigate what epoch time is and how to get it in Java.

Photo by Jon Tyson on Unsplash

In computer science, the epoch time, often known as Unix time, is the number of seconds from January 1, 1970. In other words, it is the amount of seconds since that date.

The epoch is a specific point in time. Thus, epoch time refers to the number of seconds since the zero point.

Why Is an Epoch Required?

A computer needs a starting value before it can add some seconds on top of it. Adding some value to an unknown point in time makes no sense. The epoch time has been included to provide an initial zero point.

Facts about Epoch Time

  • Epoch time, sometimes referred to as Unix time or POSIX time, is a method of expressing timestamps as the duration of time since an established reference point known as the “epoch.”
  • The epoch is the fixed point in time from which all subsequent timestamps are calculated.
  • The epoch time is defined in the context of Unix-based operating systems and programming as the amount of seconds that have passed since January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). This is often known as the “Unix Epoch”.
  • Using epoch time offers various benefits, including: -
    - Simplicity: Representing time as a single integer (number of seconds) simplifies date and time computations in programming.
    - Portability: Epoch time is not reliant on any time zone, making it simple to store and transfer timestamps across different systems and locations.
    - Precision: Using seconds as the unit of measurement provides sufficient precision for many applications.

How to get epoch in java

As java is more popular for this type of calculation and for designing complicated applications, I’ve included a code snippet for your reference below -

package epoch;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class EpochExample {

public static void main(String[] args) {

//get Epoch from current Instant
long epochInMillisecond = Instant.now().toEpochMilli();

System.out.println("Epoch in Millisecond: " + epochInMillisecond);

Instant instant = Instant.ofEpochMilli(epochInMillisecond);

LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
String formattedDate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"));
System.out.println("Java System Local Date Time: " + formattedDate);


//get epoch of specific zone DateTime
long epochSecond = localDateTime.toEpochSecond(ZoneOffset.of("+04:00"));
instant = Instant.ofEpochSecond(epochSecond);
localDateTime = instant.atZone(ZoneId.of("Asia/Kolkata")).toLocalDateTime();
formattedDate = localDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"));
System.out.println("Java Specific Zone Local Date Time: " + formattedDate);
}
}

/***
Output
Epoch in Millisecond: 1690699823712
Java System Local Date Time: 2023/07/30 12:20
Java Specific Zone Local Date Time: 2023/07/30 13:53
***/

--

--