Java Convert ZonedDateTime to Epoch Seconds

Tags: Java ZonedDateTime Java 8

In this Java core tutorial we learn how to convert a java.time.ZonedDateTime object to epoch seconds which is the number of seconds from the epoch of 1970-01-01T00:00:00Z.

How to convert ZonedDateTime to Epoch Seconds in Java

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

ConvertZonedDateTimeToEpochSecondExample1.java

import java.time.ZonedDateTime;

public class ConvertZonedDateTimeToEpochSecondExample1 {
    public static void main(String... args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        long epochSeconds = zonedDateTime.toEpochSecond();

        System.out.println("ZonedDateTime: " + zonedDateTime);
        System.out.println("Epoch Seconds: " + epochSeconds);
    }
}
The output as below.
ZonedDateTime: 2022-05-07T15:59:33.487502100+07:00[Asia/Bangkok]
Epoch Seconds: 1651913973

Happy Coding 😊

Java Convert ZonedDateTime to Instant

Java Convert ZonedDateTime to Date

Java Convert ZonedDateTime to Calendar

Java Convert ZonedDateTime to LocalDateTime

Java Convert ZonedDateTime to OffsetDateTime

Java Convert ZonedDateTime to LocalTime

Java Convert ZonedDateTime to LocalDate

Java Convert ZonedDateTime to ZoneOffset

Java Convert ZonedDateTime to ZoneId

Java Convert ZonedDateTime to String

Java Convert String to ZonedDateTime