Java LocalDateTime ofEpochSecond() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, LocalDateTime.ofEpochSecond(seconds, nanos, offset) turns a Unix timestamp into a LocalDateTime. The timestamp counts seconds since 1 January 1970 at 00:00 UTC, the "epoch".

Because the same timestamp shows a different wall-clock time in each place, you must also pass a ZoneOffset that says which clock to read it on.

A static method of java.time.LocalDateTime (since Java 8): call it on the class, as LocalDateTime.ofEpochSecond(), with no object needed. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Pass the seconds, the extra nanoseconds, and the offset.

Java
LocalDateTime a = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC);                  // 1970-01-01T00:00
LocalDateTime b = LocalDateTime.ofEpochSecond(86_400, 0, ZoneOffset.UTC);             // 1970-01-02T00:00
LocalDateTime c = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.ofHoursMinutes(5, 30)); // 1970-01-01T05:30
CallWhat it does
ofEpochSecond(s, n, offset)Seconds since 1970, extra nanoseconds, and the offset to read them in.
dt.toEpochSecond(offset)The reverse: turns a date-time and an offset back into seconds.
Instant.ofEpochSecond(s)The same timestamp as an Instant, which has no offset.
LocalDateTime.ofInstant(i, zone)Uses an Instant and a ZoneId instead.

Java timestamps are in seconds here. If your number is in milliseconds, divide it by 1000 first, or use Instant.ofEpochMilli().

Library declarations
Java
// java.time.LocalDateTime
public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)

Parameters

ParameterDescription
epochSecond (long)Seconds since 1970-01-01T00:00:00Z. It can be negative for dates before 1970.
nanoOfSecond (int)Extra nanoseconds, from 0 to 999,999,999.
offset (ZoneOffset)The offset from UTC used to turn the timestamp into a local date-time.

Return Value

Return ValueDescription
LocalDateTimeThe date-time that a clock with the given offset shows at that moment.

Compare with Similar Methods

Ways to turn a moment into a LocalDateTime:

MethodHow it differs
ofInstant(instant, zone)Uses an Instant and a real time zone with daylight saving rules.
of(...)Builds a date-time from calendar numbers.
now()Reads the current date and time.

Java Examples

Example 1 — Start from the Epoch

Turn the seconds 0, 86400, and -1 into date-times in UTC.

Java
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class LdtOfEpochSecondDemo {
    public static void main(String[] args) {
        System.out.println(LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC));
        System.out.println(LocalDateTime.ofEpochSecond(86_400, 0, ZoneOffset.UTC));
        System.out.println(LocalDateTime.ofEpochSecond(-1, 0, ZoneOffset.UTC));
    }
}

How It Works

  1. Second 0. It is the epoch itself, midnight on 1 January 1970 in UTC.
  2. 86,400 seconds. A day has 86,400 seconds, so this is midnight on 2 January 1970.
  3. Minus one second. Negative numbers go back in time, so -1 is the last second of 1969.

Example 2 — Read the Same Second on Different Clocks

Use the timestamp 0 with an offset of 5 hours 30 minutes and with an offset of minus 8 hours.

Java
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class LdtOfEpochSecondOffsets {
    public static void main(String[] args) {
        LocalDateTime india = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.ofHoursMinutes(5, 30));
        LocalDateTime california = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.ofHours(-8));

        System.out.println("UTC+05:30: " + india);
        System.out.println("UTC-08:00: " + california);
    }
}

How It Works

  1. The same timestamp. Both calls use second 0, the same moment in time.
  2. Ahead of UTC. An offset of +05:30 adds five and a half hours, so the clock shows 05:30 on 1 January 1970.
  3. Behind UTC. An offset of -08:00 subtracts eight hours, so the clock shows 16:00 on 31 December 1969.

Example 3 — Go from a Date to a Timestamp and Back

Get the timestamp of an instant, add half a second of nanoseconds, and convert back.

Java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class LdtOfEpochSecondRoundTrip {
    public static void main(String[] args) {
        long seconds = Instant.parse("2026-03-15T10:30:00Z").getEpochSecond();
        LocalDateTime dt = LocalDateTime.ofEpochSecond(seconds, 500_000_000, ZoneOffset.UTC);

        System.out.println("seconds:   " + seconds);
        System.out.println("date-time: " + dt);
        System.out.println("back again: " + (dt.toEpochSecond(ZoneOffset.UTC) == seconds));
    }
}

How It Works

  1. A real timestamp. getEpochSecond() gives the seconds since 1970 for 10:30 UTC on 15 March 2026.
  2. Add half a second. The second argument, 500_000_000 nanoseconds, becomes the fraction .500.
  3. Convert back. toEpochSecond(ZoneOffset.UTC) drops the fraction and returns the original seconds.
  4. They match. The comparison prints true, so no information was lost.

Frequently Asked Questions

Use LocalDateTime.ofEpochSecond(timestamp, 0, ZoneOffset.UTC), or LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), zone) for a real time zone.
Use LocalDateTime.ofInstant(Instant.ofEpochMilli(ms), zone), or divide the number by 1000 for this method.
Use dt.toEpochSecond(offset) with the same offset, or dt.atZone(zone).toEpochSecond().
A timestamp is one moment, but local clocks show different times. The offset tells Java which clock to read.
1 January 1970 at 00:00 in UTC. This moment is called the Unix epoch.

Next: ofInstant()

Convert an Instant into the date-time of a time zone.

ofInstant() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful