Java ZonedDateTime.ofStrict() Method with Examples

Tags: Java ZonedDateTime Java 8

In this Java core tutorial we learn how to use the java.time.ZonedDateTime.ofStrict() method to convert a LocalDateTime object to ZonedDateTime object by combination of LocalDateTime, ZoneOffset and ZoneId. It also ensure that the offset is valid for the local date time according to the rules of the specified zone.

How to use ZonedDateTime.ofStrict() method

In the following Java program we show how to use the ZonedDateTime.ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) method.

ZonedDateTimeOfStrictExample1.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeOfStrictExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset zoneOffset = ZoneOffset.ofHours(2);
        ZoneId zoneId = ZoneId.of("Europe/Paris");

        ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(localDateTime, zoneOffset, zoneId);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("ZoneOffset: " + zoneOffset);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-26T22:12:24.707866200
ZoneOffset: +02:00
ZonedDateTime: 2022-04-26T22:12:24.707866200+02:00[Europe/Paris]

If the offset is invalid, a DateTimeException exception is thrown. For example, we change zone offset to 7 as below.

ZonedDateTimeOfStrictExample2.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeOfStrictExample2 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset zoneOffset = ZoneOffset.ofHours(7);
        ZoneId zoneId = ZoneId.of("Europe/Paris");

        ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(localDateTime, zoneOffset, zoneId);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("ZoneOffset: " + zoneOffset);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
Exception in thread "main" java.time.DateTimeException: ZoneOffset '+07:00' is not valid for LocalDateTime '2022-04-26T22:12:46.986929900' in zone 'Europe/Paris'
	at java.base/java.time.ZonedDateTime.ofStrict(ZonedDateTime.java:490)
	at ZonedDateTimeOfStrictExample2.main(ZonedDateTimeOfStrictExample2.java:12)

Happy Coding 😊

Java ZonedDateTime.now() method with Examples

Java ZonedDateTime.of() Method with Examples

Java ZonedDateTime.ofInstant() Method with Examples

Java ZonedDateTime.parse() Method with Examples

Java ZonedDateTime.getOffset() Method with Examples

Java ZonedDateTime.withEarlierOffsetAtOverlap() Method with Examples

Java ZonedDateTime.withLaterOffsetAtOverlap() Method with Examples

Java ZonedDateTime.getZone() Method with Examples

Java ZonedDateTime.withZoneSameLocal() Method with Examples

Java ZonedDateTime.withZoneSameInstant() Method with Examples

Java ZonedDateTime.withFixedOffsetZone() Method with Examples

Java ZonedDateTime.toLocalDateTime() Method with Examples

Java ZonedDateTime.toLocalDate() Method with Examples

Java ZonedDateTime.toLocalTime() Method with Examples

Java ZonedDateTime.getYear() Method with Examples

Java ZonedDateTime.getMonthValue() Method with Examples

Java ZonedDateTime.getMonth() Method with Examples

Java ZonedDateTime.getDayOfMonth() Method with Examples

Java ZonedDateTime.getDayOfYear() Method with Examples

Java ZonedDateTime.getDayOfWeek() Method with Examples

Java ZonedDateTime.getHour() Method with Examples

Java ZonedDateTime.getMinute() Method with Examples

Java ZonedDateTime.getSecond() Method with Examples

Java ZonedDateTime.getNano() Method with Examples

Java ZonedDateTime.withYear() Method with Examples

Java ZonedDateTime.withMonth() Method with Examples

Java ZonedDateTime.withDayOfMonth() Method with Examples

Java ZonedDateTime.withDayOfYear() Method with Examples

Java ZonedDateTime.withHour() Method with Examples

Java ZonedDateTime.withMinute() Method with Examples

Java ZonedDateTime.withSecond() Method with Examples

Java ZonedDateTime.withNano() Method with Examples

Java ZonedDateTime.plusYears() Method with Examples

Java ZonedDateTime.plusMonths() Method with Examples

Java ZonedDateTime.plusWeeks() Method with Examples

Java ZonedDateTime.plusDays() Method with Examples

Java ZonedDateTime.plusHours() Method with Examples

Java ZonedDateTime.plusMinutes() Method with Examples

Java ZonedDateTime.plusSeconds() Method with Examples

Java ZonedDateTime.plusNanos() Method with Examples

Java ZonedDateTime.minusYears() Method with Examples

Java ZonedDateTime.minusMonths() Method with Examples

Java ZonedDateTime.minusWeeks() Method with Examples

Java ZonedDateTime.minusDays() Method with Examples

Java ZonedDateTime.minusHours() Method with Examples

Java ZonedDateTime.minusMinutes() Method with Examples

Java ZonedDateTime.minusSeconds() Method with Examples

Java ZonedDateTime.minusNanos() Method with Examples

Java ZonedDateTime.format() Method with Examples

Java ZonedDateTime.toOffsetDateTime() Method with Examples

Java ZonedDateTime.equals() Method with Examples

Java ZonedDateTime.toString() Method with Examples

Java Compare two ZonedDateTime Values

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDate to ZonedDateTime