Java Programs›Date & Time›Convert Epoch Time to Date

Convert Epoch Time to Date in Java

beginner·  Date & Time  ·  Date & Time

Problem

Epoch time counts seconds (or milliseconds) since 1 January 1970 at 00:00 UTC, which is how many systems store timestamps.

Convert the Unix timestamp 1773048600 into a UTC time, a Kolkata local time and an epoch value in milliseconds.

Input
1773048600
Output
2026-03-09T09:30:00Z, 9 Mar 2026, 15:00 in Kolkata

Java Program

Java
import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Locale; public class ConvertEpochToDate { public static void main(String[] args) { long epochSeconds = 1_773_048_600L; // seconds since 1970-01-01T00:00:00Z Instant instant = Instant.ofEpochSecond(epochSeconds); DateTimeFormatter f = DateTimeFormatter.ofPattern("d MMM yyyy, HH:mm", Locale.ENGLISH); System.out.println("UTC: " + instant); System.out.println("Kolkata: " + instant.atZone(ZoneId.of("Asia/Kolkata")).format(f)); System.out.println("Millis: " + instant.toEpochMilli()); } }

Output

UTC: 2026-03-09T09:30:00Z Kolkata: 9 Mar 2026, 15:00 Millis: 1773048600000

Core Logic

Instant.ofEpochSecond turns the number into a point on the timeline, and a zone turns that point into a local clock reading.

How It Works
  1. 1Instant.ofEpochSecond(epochSeconds) creates the instant.
  2. 2Printing the Instant shows it in UTC (the Z suffix).
  3. 3atZone(ZoneId.of("Asia/Kolkata")) converts it to Indian time, which is 5 hours 30 minutes ahead.
  4. 4toEpochMilli() gives the same moment in milliseconds.
09:30 UTC is 15:00 in Kolkata.
💡

Key Point: Check the unit: JavaScript and many APIs use milliseconds — passing them to ofEpochSecond gives a date thousands of years away.

Key Concepts

Instant.ofEpochSecond()atZone()toEpochMilli()

Approach 2: Converting a Date-Time to Epoch

Java
import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; public class ConvertDateToEpoch { public static void main(String[] args) { LocalDateTime meeting = LocalDateTime.of(2026, 3, 9, 9, 30); long utcSeconds = meeting.toEpochSecond(ZoneOffset.UTC); // as if the time were UTC long kolkataSeconds = meeting.atZone(ZoneId.of("Asia/Kolkata")).toEpochSecond(); // as Kolkata local time System.out.println("Epoch seconds (read as UTC): " + utcSeconds); System.out.println("Epoch seconds (read as Kolkata): " + kolkataSeconds); System.out.println("Difference in hours: " + (utcSeconds - kolkataSeconds) / 3600.0); } }

Output

Epoch seconds (read as UTC): 1773048600 Epoch seconds (read as Kolkata): 1773028800 Difference in hours: 5.5

Core Logic

A LocalDateTime has no zone, so converting it to epoch time requires saying which zone the clock reading belongs to.

How It Works
  1. 1toEpochSecond(ZoneOffset.UTC) treats 09:30 as UTC.
  2. 2atZone(Kolkata).toEpochSecond() treats it as Kolkata time.
  3. 3The two results differ by exactly 5.5 hours.
The difference printed is 5.5 hours, Kolkata's offset from UTC.
💡

Key Point: The same wall-clock time means different instants in different zones.

Key Concepts

toEpochSecond(ZoneOffset)ZonedDateTime.toEpochSecond()

Interview Questions

What is the "year 2038 problem"?

Systems storing epoch seconds in a signed 32-bit integer overflow on 19 January 2038. Java's long and Instant don't have this limit.

How do you get the current epoch time in Java?

Instant.now().getEpochSecond() for seconds, or System.currentTimeMillis() for milliseconds.

Learn the Concept

Related Programs