Java ZonedDateTime Class
\\\\\\\\n\\\\\\\\nZonedDateTime is an important class in the date-time API introduced in Java 8 (java.time package), representing a date and time with a time zone. This class combines LocalDateTime and ZoneId, enabling precise representation of a specific point in time within a particular time zone.
\\\\\\\\n\\\\\\\\nMain Features
\\\\\\\\n- \\\\\\\\n
- Contains date (year, month, day) \\\\\\\\n
- Contains time (hour, minute, second, nanosecond) \\\\\\\\n
- Contains time zone information \\\\\\\\n
- Is an immutable and thread-safe class \\\\\\\\n
- Supports time zone conversion and automatic daylight saving time adjustments \\\\\\\\n
\\\\\\\\n\\\\\\\\n
Creating ZonedDateTime Instances
\\\\\\\\n\\\\\\\\n1. Create Using Current Time
\\\\\\\\nExample
\\\\\\\\n// Get currentsystem default timezone()DayDateHourTime\\\\\\\\n\\\\\\\\n ZonedDateTime now = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n// Get Current Date and Time of Specified Timezone\\\\\\\\n\\\\\\\\n ZonedDateTime nowInTokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));\\\\\\\\n\\\\\\\\n2. Create From Components
\\\\\\\\nExample
\\\\\\\\n// Specify Year, Month, Day, Hour, Minute, Second and Timezone\\\\\\\\n\\\\\\\\n ZonedDateTime customDateTime = ZonedDateTime.of(\\\\\\\\n2023, 12, 25, 15, 30, 0, 0, \\\\\\\\n ZoneId.of("America/New_York")\\\\\\\\n);\\\\\\\\n\\\\\\\\n3. Combine LocalDateTime and ZoneId
\\\\\\\\nExample
\\\\\\\\nLocalDateTime localDateTime = LocalDateTime.of(2023, 6, 15, 10, 30);\\\\\\\\n\\\\\\\\n ZoneId zoneId = ZoneId.of("Europe/Paris");\\\\\\\\n\\\\\\\\n ZonedDateTime parisTime = ZonedDateTime.of(localDateTime, zoneId);\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
Common Operations
\\\\\\\\n\\\\\\\\n1. Retrieve Time Information
\\\\\\\\nExample
\\\\\\\\nZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\nint year = zdt.getYear();// Year\\\\\\\\n\\\\\\\\n Month month = zdt.getMonth();// Month\\\\\\\\n\\\\\\\\nint day = zdt.getDayOfMonth();// Day\\\\\\\\n\\\\\\\\nint hour = zdt.getHour();// Hour\\\\\\\\n\\\\\\\\nint minute = zdt.getMinute();// Minute\\\\\\\\n\\\\\\\\nint second = zdt.getSecond();// Second\\\\\\\\n\\\\\\\\n ZoneId zone = zdt.getZone();// Timezone\\\\\\\\n\\\\\\\\n2. Add/Subtract Time
\\\\\\\\nExample
\\\\\\\\nZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n// Add 1 day\\\\\\\\n\\\\\\\\n ZonedDateTime tomorrow = zdt.plusDays(1);\\\\\\\\n\\\\\\\\n// Subtract 2 hours\\\\\\\\n\\\\\\\\n ZonedDateTime twoHoursEarlier = zdt.minusHours(2);\\\\\\\\n\\\\\\\\n// Add 3 weeks\\\\\\\\n\\\\\\\\n ZonedDateTime threeWeeksLater = zdt.plusWeeks(3);\\\\\\\\n\\\\\\\\n3. Time Zone Conversion
\\\\\\\\nExample
\\\\\\\\nZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));\\\\\\\\n\\\\\\\\n// Convert to New York time\\\\\\\\n\\\\\\\\n ZonedDateTime newYorkTime = tokyoTime.withZoneSameInstant(ZoneId.of("America/New_York"));\\\\\\\\n\\\\\\\\n// Convert to UTC time\\\\\\\\n\\\\\\\\n ZonedDateTime utcTime = tokyoTime.withZoneSameInstant(ZoneOffset.UTC);\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
Practical Application Examples
\\\\\\\\n\\\\\\\\n1. Calculate Time Difference Between Two Time Zones
\\\\\\\\nExample
\\\\\\\\nZonedDateTime londonTime = ZonedDateTime.now(ZoneId.of("Europe/London"));\\\\\\\\n\\\\\\\\n ZonedDateTime sydneyTime = ZonedDateTime.now(ZoneId.of("Australia/Sydney"));\\\\\\\\n\\\\\\\\n// Calculate Hour Difference\\\\\\\\n\\\\\\\\nlong hoursBetween = ChronoUnit.HOURS.between(londonTime, sydneyTime);\\\\\\\\n\\\\\\\\nSystem.out.println("Current time difference between London and Sydney: "+ hoursBetween +"Hour");\\\\\\\\n\\\\\\\\n2. Handle Cross-Timezone Meeting Times
\\\\\\\\nExample
\\\\\\\\n// The meeting time in New York is 3 PM\\\\\\\\n\\\\\\\\n ZonedDateTime meetingTimeNY = ZonedDateTime.of(\\\\\\\\n LocalDate.now(), \\\\\\\\n LocalTime.of(15, 0), \\\\\\\\n ZoneId.of("America/New_York")\\\\\\\\n);\\\\\\\\n\\\\\\\\n// Convert to Tokyo Time\\\\\\\\n\\\\\\\\n ZonedDateTime meetingTimeTokyo = meetingTimeNY.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));\\\\\\\\n\\\\\\\\nSystem.out.println("Attendees in Tokyo should be at local time: "+ meetingTimeTokyo);\\\\\\\\n\\\\\\\\n3. Handle Daylight Saving Time
\\\\\\\\nExample
\\\\\\\\n// Time in New York around daylight saving time change\\\\\\\\n\\\\\\\\n ZonedDateTime beforeDST = ZonedDateTime.of(\\\\\\\\n LocalDateTime.of(2023, 3, 11, 1, 30),\\\\\\\\n ZoneId.of("America/New_York")\\\\\\\\n);\\\\\\\\n\\\\\\\\nZonedDateTime afterDST = beforeDST.plusHours(1);\\\\\\\\n\\\\\\\\nSystem.out.println("Actual time difference: "+\\\\\\\\n\\\\\\\\n Duration.between(beforeDST, afterDST).toHours()+"Hour");\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
Converting ZonedDateTime to Other Date-Time Classes
\\\\\\\\n\\\\\\\\n1. Convert to Instant
\\\\\\\\nExample
\\\\\\\\nZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n Instant instant = zdt.toInstant();// Convert to UTC timestamp\\\\\\\\n\\\\\\\\n2. Convert to LocalDateTime
\\\\\\\\nExample
\\\\\\\\nZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n LocalDateTime ldt = zdt.toLocalDateTime();// Loss of Timezone Information\\\\\\\\n\\\\\\\\n3. Create From Instant
\\\\\\\\nExample
\\\\\\\\nInstant instant = Instant.now();\\\\\\\\n\\\\\\\\n ZonedDateTime zdt = instant.atZone(ZoneId.of("Asia/Shanghai"));\\\\\\\\n\\\\\\\\nComprehensive Example
\\\\\\\\nExample
\\\\\\\\nimport java.time.ZonedDateTime;\\\\\\\\n\\\\\\\\nimport java.time.ZoneId;\\\\\\\\n\\\\\\\\nimport java.time.format.DateTimeFormatter;\\\\\\\\n\\\\\\\\npublic class ZonedDateTimeDemo {\\\\\\\\n\\\\\\\\npublic static void main(String[] args){\\\\\\\\n\\\\\\\\n// Get Current Timezone's Current Hour Time\\\\\\\\n\\\\\\\\n ZonedDateTime now = ZonedDateTime.now();\\\\\\\\n\\\\\\\\nSystem.out.println("Current Timezone Time: "+ now);\\\\\\\\n\\\\\\\\n// Get Specified Timezone's Current Hour Time\\\\\\\\n\\\\\\\\n ZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));\\\\\\\\n\\\\\\\\nSystem.out.println("Tokyo time: "+ tokyoTime);\\\\\\\\n\\\\\\\\n// 3. Create specific()with / carryingTimezoneHourTime\\\\\\\\n\\\\\\\\n ZonedDateTime specificTime = ZonedDateTime.of(\\\\\\\\n2023, 5, 15, 10, 30, 0, 0, \\\\\\\\n\\\\\\\\n ZoneId.of("America/New_York")\\\\\\\\n);\\\\\\\\n\\\\\\\\nSystem.out.println("New York specific time: "+ specificTime);\\\\\\\\n\\\\\\\\n// 4. TimezoneConvert\\\\\\\\n\\\\\\\\n ZonedDateTime londonTime = specificTime.withZoneSameInstant(ZoneId.of("Europe/London"));\\\\\\\\n\\\\\\\\nSystem.out.println("Corresponding London time: "+ londonTime);\\\\\\\\n\\\\\\\\n// 5. Format output\\\\\\\\n\\\\\\\\n DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");\\\\\\\\n\\\\\\\\nSystem.out.println("Format time: "+ now.format(formatter));\\\\\\\\n\\\\\\\\n// 6. Get information of each part\\\\\\\\n\\\\\\\\nSystem.out.println("Timezone: "+ now.getZone());\\\\\\\\n\\\\\\\\nSystem.out.println("Year: "+ now.getYear());\\\\\\\\n\\\\\\\\nSystem.out.println("Month: "+ now.getMonth());\\\\\\\\n\\\\\\\\nSystem.out.println("Hour: "+ now.getHour());\\\\\\\\n\\\\\\\\n}\\\\\\\\n\\\\\\\\n}\\\\\\\\n\\\\\\\\nOutput results may look similar to the following:
\\\\\\\\nCurrent Timezone Time: 2025-05-01T11:29:07.411467+08:00[Asia/Shanghai]Tokyo time: 2025-05-01T12:29:07.417938+09:00[Asia/Tokyo]New York specific time: 2023-05-15T10:30-04:00[America/New_York]Corresponding London time: 2023-05-15T15:30+01:00[Europe/London]Format time: 2025-05-01 11:29:07 CST Timezone: Asia/ShanghaiYear: 2025Month: MAY Hour: 11\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
Best Practices
\\\\\\\\n- \\\\\\\\n
- Storage and Transmission: When storing in databases or transmitting over networks, it is recommended to use
InstantorZonedDateTimein UTC. \\\\\\\\n - Display: Only convert to local time zones when displaying to users in the UI. \\\\\\\\n
- Comparison: When comparing times across different time zones, first convert them to the same time zone or
Instant. \\\\\\\\n - Daylight Saving Time: Always use full time zone IDs (e.g., "America/New_York") instead of simple offsets (e.g., "-05:00"). \\\\\\\\n
ZonedDateTime is a powerful tool for handling timezone-sensitive applications. Correct usage can avoid many common date-time issues.
Static Factory Methods
\\\\\\\\n| Method | \\\\\\\\nDescription | \\\\\\\\nExample | \\\\\\\\n
|---|---|---|
static ZonedDateTime now() | \\\\\\\\nGet current date-time in the system default time zone | \\\\\\\\nZonedDateTime.now() | \\\\\\\\n
static ZonedDateTime now(ZoneId zone) | \\\\\\\\nGet current date-time in the specified time zone | \\\\\\\\nZonedDateTime.now(ZoneId.of("Asia/Shanghai")) | \\\\\\\\n
static ZonedDateTime of(int year, int month, int day, int hour, int minute, int second, int nano, ZoneId zone) | \\\\\\\\nCreate from specified parameters | \\\\\\\\nZonedDateTime.of(2023, 6, 15, 14, 30, 0, 0, ZoneId.of("GMT+8")) | \\\\\\\\n
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone) | \\\\\\\\nCombine LocalDate, LocalTime, and time zone | \\\\\\\\nZonedDateTime.of(LocalDate.now(), LocalTime.now(), ZoneId.systemDefault()) | \\\\\\\\n
static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone) | \\\\\\\\nCreate from LocalDateTime and time zone | \\\\\\\\nZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/New_York")) | \\\\\\\\n
static ZonedDateTime ofInstant(Instant instant, ZoneId zone) | \\\\\\\\nCreate from Instant and time zone | \\\\\\\\nZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC")) | \\\\\\\\n
static ZonedDateTime parse(CharSequence text) | \\\\\\\\nParse from string | \\\\\\\\nZonedDateTime.parse("2023-06-15T14:30:45+08:00[Asia/Shanghai]") | \\\\\\\\n
static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter) | \\\\\\\\nParse using specified format | \\\\\\\\nZonedDateTime.parse("2023-06-15 14:30 CST", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm z")) | \\\\\\\\n
Getting Date-Time Information
\\\\\\\\n| Method | \\\\\\\\nDescription | \\\\\\\\nExample | \\\\\\\\n
|---|---|---|
int getYear() | \\\\\\\\nGet year | \\\\\\\\n2023 | \\\\\\\\n
Month getMonth() | \\\\\\\\nGet month (Month enum) | \\\\\\\\nMonth.JUNE | \\\\\\\\n
int getMonthValue() | \\\\\\\\nGet month value (1-12) | \\\\\\\\n6 | \\\\\\\\n
int getDayOfMonth() | \\\\\\\\nGet day of month | \\\\\\\\n15 | \\\\\\\\n
int getDayOfYear() | \\\\\\\\nGet day of year | \\\\\\\\n166 | \\\\\\\\n
DayOfWeek getDayOfWeek() | \\\\\\\\nGet day of week (DayOfWeek enum) | \\\\\\\\nDayOfWeek.THURSDAY | \\\\\\\\n
int getHour() | \\\\\\\\nGet hour (0-23) | \\\\\\\\n14 | \\\\\\\\n
int getMinute() | \\\\\\\\nGet minute (0-59) | \\\\\\\\n30 | \\\\\\\\n
int getSecond() | \\\\\\\\nGet second (0-59) | \\\\\\\\n45 | \\\\\\\\n
int getNano() | \\\\\\\\nGet nanosecond | \\\\\\\\n0 | \\\\\\\\n
ZoneId getZone() | \\\\\\\\nGet time zone | \\\\\\\\nAsia/Shanghai | \\\\\\\\n
ZoneOffset getOffset() | \\\\\\\\nGet time zone offset | \\\\\\\\n+08:00 | \\\\\\\\n
Date-Time Operations
\\\\\\\\n| Method | \\\\\\\\nDescription | \\\\\\\\nExample | \\\\\\\\n
|---|---|---|
ZonedDateTime plusDays(long days) | \\\\\\\\nAdd days | \\\\\\\\nzdt.plusDays(5) | \\\\\\\\n
ZonedDateTime plusHours(long hours) | \\\\\\\\nAdd hours | \\\\\\\\nzdt.plusHours(2) | \\\\\\\\n
ZonedDateTime plusMinutes(long minutes) | \\\\\\\\nAdd minutes | \\\\\\\\nzdt.plusMinutes(30) | \\\\\\\\n
ZonedDateTime plusSeconds(long seconds) | \\\\\\\\nAdd seconds | \\\\\\\\nzdt.plusSeconds(45) | \\\\\\\\n
ZonedDateTime plusNanos(long nanos) | \\\\\\\\nAdd nanoseconds | \\\\\\\\nzdt.plusNanos(1000000) | \\\\\\\\n
ZonedDateTime minusDays(long days) | \\\\\\\\nSubtract days | \\\\\\\\nzdt.minusDays(5) | \\\\\\\\n
ZonedDateTime minusHours(long hours) | \\\\\\\\nSubtract hours | \\\\\\\\nzdt.minusHours(2) | \\\\\\\\n
ZonedDateTime minusMinutes(long minutes) | \\\\\\\\nSubtract minutes | \\\\\\\\nzdt.minusMinutes(30) | \\\\\\\\n
ZonedDateTime minusSeconds(long seconds) | \\\\\\\\nSubtract seconds | \\\\\\\\nzdt.minusSeconds(45) | \\\\\\\\n
ZonedDateTime minusNanos(long nanos) | \\\\\\\\nSubtract nanoseconds | \\\\\\\\nzdt.minusNanos(1000000) | \\\\\\\\n
ZonedDateTime withYear(int year) | \\\\\\\\nModify year | \\\\\\\\nzdt.withYear(2024) | \\\\\\\\n
ZonedDateTime withMonth(int month) | \\\\\\\\nModify month | \\\\\\\\nzdt.withMonth(12) | \\\\\\\\n
ZonedDateTime withDayOfMonth(int day) | \\\\\\\\nModify day of month | \\\\\\\\nzdt.withDayOfMonth(1) | \\\\\\\\n
ZonedDateTime withHour(int hour) | \\\\\\\\nModify hour | \\\\\\\\nzdt.withHour(0) | \\\\\\\\n
ZonedDateTime withMinute(int minute) | \\\\\\\\nModify minute | \\\\\\\\nzdt.withMinute(0) | \\\\\\\\n
ZonedDateTime withSecond(int second) | \\\\\\\\nModify second | \\\\\\\\nzdt.withSecond(0) | \\\\\\\\n
ZonedDateTime withNano(int nano) | \\\\\\\\nModify nanosecond | \\\\\\\\nzdt.withNano(0) | \\\\\\\\n
ZonedDateTime withZoneSameInstant(ZoneId zone) | \\\\\\\\n\\\\\\\\n | \\\\\\\\n |
YouTip