The Java 8 OffsetDateTime represents date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system.
Java 8 OffsetDateTime.ofInstant(Instant.ofEpochMilli(1580232122000L),ZoneId.systemDefault())
function can be used to convert Epoch time in Milliseconds to Java 8 OffsetDateTime object.
This tutorial will teach you different ways to convert Epoch time in milliseconds to OffsetDateTime.
To convert Epoch Time in Milliseconds to Java 8 ZonedDateTime, you can check this tutorial How to convert Epoch time in Milliseconds to Java 8 ZonedDateTime.
Using OffsetDateTime.ofInstant()
The OffsetDateTime.ofInstant() method accepts the Instant value created using Epoch Time in Milliseconds value.
Code
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
//main
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(Instant.ofEpochMilli(1580232122000L),ZoneId.systemDefault());
System.out.println(offsetDateTime); //2020-01-28T17:22:02Z
Using Instant.ofEpochMilli()
The Java 8 Instant class provides the offsetDateTime value based on the Epoch millisecond.
Code
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
//main
long epochInMilliSeconds = Instant.now().toEpochMilli();
System.out.println(epochInMilliSeconds); //1675018639820
OffsetDateTime offsetDateTime = Instant.ofEpochMilli(epochInMilliSeconds).atZone(ZoneId.systemDefault()).toOffsetDateTime();
System.out.println(offsetDateTime); //2023-01-29T18:57:19.820Z
In the above example, Instant.ofEpochMilli()
accepts the millisecond value along with the timezone and returns the OffsetDateTime value.