Java ZonedDateTime.of() Method with Examples
Tags: Java ZonedDateTime Java 8
In this Java core tutorial we learn how to use the ZonedDateTime.of() method to create new ZonedDateTime in Java programming language.
Table of contents
- Create ZonedDateTime object from LocalDate LocalTime and ZoneId object
- Create ZonedDateTime object from LocalDateTime and ZoneId object
- Create ZonedDateTime object from Year Month Day Hour Minute Second Nano of Second and ZoneId object
Create ZonedDateTime object from LocalDate LocalTime and ZoneId object
In Java we can use the ZonedDateTime.of(LocalDate date, LocalTime time, ZoneId zone) method to create new ZonedDateTime object from given LocalDate, LocalTime and ZoneId object as the following Java example code.
ZonedDateTimeOfExample1.java
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeOfExample1 {
public static void main(String... args) {
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
ZoneId zoneId1 = ZoneId.systemDefault();
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime1 = ZonedDateTime.of(localDate, localTime, zoneId1);
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(localDate, localTime, zoneId2);
System.out.println("localDate: " + localDate);
System.out.println("localTime: " + localTime);
System.out.println("zoneId1: " + zoneId1);
System.out.println("zoneId2: " + zoneId2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
}
}
localDate: 2022-04-25
localTime: 22:47:13.097203300
zoneId1: Asia/Bangkok
zoneId2: Europe/Paris
zonedDateTime1: 2022-04-25T22:47:13.097203300+07:00[Asia/Bangkok]
zonedDateTime2: 2022-04-25T22:47:13.097203300+02:00[Europe/Paris]
Create ZonedDateTime object from LocalDateTime and ZoneId object
Java also provide the ZonedDateTime.of(LocalDateTime localDateTime, ZoneId zone) method to create new ZonedDateTime object from given LocalDateTime and ZoneId object as below.
ZonedDateTimeOfExample2.java
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeOfExample2 {
public static void main(String... args) {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zoneId1 = ZoneId.systemDefault();
ZoneId zoneId2 = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime1 = ZonedDateTime.of(localDateTime, zoneId1);
ZonedDateTime zonedDateTime2 = ZonedDateTime.of(localDateTime, zoneId2);
System.out.println("localDateTime: " + localDateTime);
System.out.println("zoneId1: " + zoneId1);
System.out.println("zoneId2: " + zoneId2);
System.out.println("zonedDateTime1: " + zonedDateTime1);
System.out.println("zonedDateTime2: " + zonedDateTime2);
}
}
localDateTime: 2022-04-25T22:49:08.176475400
zoneId1: Asia/Bangkok
zoneId2: Europe/Paris
zonedDateTime1: 2022-04-25T22:49:08.176475400+07:00[Asia/Bangkok]
zonedDateTime2: 2022-04-25T22:49:08.176475400+02:00[Europe/Paris]
Create ZonedDateTime object from Year Month Day Hour Minute Second Nano of Second and ZoneId object
In the following Java example program we learn how to use the ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) method to create new ZonedDateTime object from given Year Month Day Hour Minute Second Nano of Second and ZoneId values.
ZonedDateTimeOfExample3.java
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateTimeOfExample3 {
public static void main(String... args) {
int year = 2022;
int month = 4;
int dayOfMonth = 25;
int hour = 10;
int minute = 20;
int second = 30;
int nanoOfSecond = 40;
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zonedDateTime = ZonedDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond, zoneId);
System.out.println(zonedDateTime);
}
}
2022-04-25T10:20:30.000000040+02:00[Europe/Paris]
Happy Coding 😊
Related Articles
Java ZonedDateTime.now() method with Examples
Java ZonedDateTime.ofLocal() Method with Examples
Java ZonedDateTime.ofInstant() Method with Examples
Java ZonedDateTime.ofStrict() 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