Java Convert ZonedDateTime to ZoneOffset
Tags: Java ZonedDateTime Java ZoneOffset Java 8
In this Java core tutorial we learn how to convert a java.time.ZonedDateTime object into java.time.ZoneOffset object in Java programming language.
How to convert ZonedDateTime to ZoneOffset in Java
In Java, to convert a ZonedDateTime object to ZoneOffset object we can use the ZonedDateTime.getOffset() method as the Java program below.
ConvertZonedDateTimeToZoneOffsetExample1.java
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
public class ConvertZonedDateTimeToZoneOffsetExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneOffset zoneOffset = zonedDateTime.getOffset();
System.out.println("ZonedDateTime: " + zonedDateTime);
System.out.println("ZoneOffset: " + zoneOffset);
}
}
ZonedDateTime: 2022-05-07T00:17:16.787630600+07:00[Asia/Bangkok]
ZoneOffset: +07:00
Happy Coding 😊
Related Articles
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 ZoneId
Java Convert ZonedDateTime to Epoch Seconds