Java Convert OffsetDateTime to Epoch Seconds

Tags: Java OffsetDateTime Java 8

In this Java core tutorial we learn how to convert an OffsetDateTime object to epoch seconds in Java programming language.

How to convert OffsetDateTime to Epoch Seconds in Java

In Java, with a given OffsetDateTime object we can use the to the toEpochSecond() method to convert it to a number of seconds from the epoch of 1970-01-01T00:00:00Z as the following Java code.

ConvertOffsetDateTimeToEpochSecondsExample1.java

import java.time.OffsetDateTime;

public class ConvertOffsetDateTimeToEpochSecondsExample1 {
    public static void main(String... args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.parse("2022-05-08T14:10:20.878564600+02:00");

        long epochSeconds = offsetDateTime.toEpochSecond();

        System.out.println("OffsetDateTime: " + offsetDateTime);
        System.out.println("Epoch Seconds: " + epochSeconds);
    }
}
The output as below.
OffsetDateTime: 2022-05-08T14:10:20.878564600+02:00
Epoch Seconds: 1652011820

Happy Coding 😊

Java Convert OffsetDateTime to ZoneOffset

Java Convert OffsetDateTime to LocalDate

Java Convert OffsetDateTime to LocalTime

Java Convert OffsetDateTime to LocalDateTime

Java Convert OffsetDateTime to ZonedDateTime

Java Convert OffsetDateTime to Instant

Java Convert OffsetDateTime to Date

Java Convert OffsetDateTime to Calendar

Java Convert OffsetDateTime to OffsetTime

Java Convert OffsetDateTime to String

Java Convert String to OffsetDateTime

Java Convert Date to OffsetDateTime

Java Convert Calendar to OffsetDateTime