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
Call
What 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
Parameter
Description
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 Value
Description
LocalDateTime
The date-time that a clock with the given offset shows at that moment.
seconds: 1773570600
date-time: 2026-03-15T10:30:00.500
back again: true
How It Works
A real timestamp.getEpochSecond() gives the seconds since 1970 for 10:30 UTC on 15 March 2026.
Add half a second. The second argument, 500_000_000 nanoseconds, becomes the fraction .500.
Convert back.toEpochSecond(ZoneOffset.UTC) drops the fraction and returns the original seconds.
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.