YouTip LogoYouTip

Java Zoneddatetime Class

Java ZonedDateTime Class \\\\\\\\n\\\\\\\\n

Java ZonedDateTime Class

\\\\\\\\n\\\\\\\\n

ZonedDateTime 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\\\\\\\\n

Main 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
\\\\\\\\n\\\\\\\\n

Creating ZonedDateTime Instances

\\\\\\\\n\\\\\\\\n

1. Create Using Current Time

\\\\\\\\n

Example

\\\\\\\\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\\\\\\\\n

2. Create From Components

\\\\\\\\n

Example

\\\\\\\\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\\\\\\\\n

3. Combine LocalDateTime and ZoneId

\\\\\\\\n

Example

\\\\\\\\n
LocalDateTime 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\\\\\\\\n

1. Retrieve Time Information

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime 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\\\\\\\\n

2. Add/Subtract Time

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime 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\\\\\\\\n

3. Time Zone Conversion

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime 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\\\\\\\\n

1. Calculate Time Difference Between Two Time Zones

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime 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\\\\\\\\n

2. Handle Cross-Timezone Meeting Times

\\\\\\\\n

Example

\\\\\\\\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\\\\\\\\n

3. Handle Daylight Saving Time

\\\\\\\\n

Example

\\\\\\\\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\\\\\\\\n

1. Convert to Instant

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n Instant instant = zdt.toInstant();// Convert to UTC timestamp
\\\\\\\\n\\\\\\\\n

2. Convert to LocalDateTime

\\\\\\\\n

Example

\\\\\\\\n
ZonedDateTime zdt = ZonedDateTime.now();\\\\\\\\n\\\\\\\\n LocalDateTime ldt = zdt.toLocalDateTime();// Loss of Timezone Information
\\\\\\\\n\\\\\\\\n

3. Create From Instant

\\\\\\\\n

Example

\\\\\\\\n
Instant instant = Instant.now();\\\\\\\\n\\\\\\\\n ZonedDateTime zdt = instant.atZone(ZoneId.of("Asia/Shanghai"));
\\\\\\\\n\\\\\\\\n

Comprehensive Example

\\\\\\\\n

Example

\\\\\\\\n
import 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\\\\\\\\n

Output results may look similar to the following:

\\\\\\\\n
Current 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
  1. Storage and Transmission: When storing in databases or transmitting over networks, it is recommended to use Instant or ZonedDateTime in UTC.
  2. \\\\\\\\n
  3. Display: Only convert to local time zones when displaying to users in the UI.
  4. \\\\\\\\n
  5. Comparison: When comparing times across different time zones, first convert them to the same time zone or Instant.
  6. \\\\\\\\n
  7. Daylight Saving Time: Always use full time zone IDs (e.g., "America/New_York") instead of simple offsets (e.g., "-05:00").
  8. \\\\\\\\n
\\\\\\\\n\\\\\\\\n

ZonedDateTime is a powerful tool for handling timezone-sensitive applications. Correct usage can avoid many common date-time issues.

\\\\\\\\n\\\\\\\\n

Static Factory Methods

\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
MethodDescriptionExample
static ZonedDateTime now()Get current date-time in the system default time zoneZonedDateTime.now()
static ZonedDateTime now(ZoneId zone)Get current date-time in the specified time zoneZonedDateTime.now(ZoneId.of("Asia/Shanghai"))
static ZonedDateTime of(int year, int month, int day, int hour, int minute, int second, int nano, ZoneId zone)Create from specified parametersZonedDateTime.of(2023, 6, 15, 14, 30, 0, 0, ZoneId.of("GMT+8"))
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)Combine LocalDate, LocalTime, and time zoneZonedDateTime.of(LocalDate.now(), LocalTime.now(), ZoneId.systemDefault())
static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone)Create from LocalDateTime and time zoneZonedDateTime.of(LocalDateTime.now(), ZoneId.of("America/New_York"))
static ZonedDateTime ofInstant(Instant instant, ZoneId zone)Create from Instant and time zoneZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
static ZonedDateTime parse(CharSequence text)Parse from stringZonedDateTime.parse("2023-06-15T14:30:45+08:00[Asia/Shanghai]")
static ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter)Parse using specified formatZonedDateTime.parse("2023-06-15 14:30 CST", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm z"))
\\\\\\\\n\\\\\\\\n

Getting Date-Time Information

\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n\\\\\\\\n
MethodDescriptionExample
int getYear()Get year2023
Month getMonth()Get month (Month enum)Month.JUNE
int getMonthValue()Get month value (1-12)6
int getDayOfMonth()Get day of month15
int getDayOfYear()Get day of year166
DayOfWeek getDayOfWeek()Get day of week (DayOfWeek enum)DayOfWeek.THURSDAY
int getHour()Get hour (0-23)14
int getMinute()Get minute (0-59)30
int getSecond()Get second (0-59)45
int getNano()Get nanosecond0
ZoneId getZone()Get time zoneAsia/Shanghai
ZoneOffset getOffset()Get time zone offset+08:00
\\\\\\\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utes)Add minuteszdt.plusMinutes(30)
ZonedDateTime plusSeconds(long seconds)Add secondszdt.plusSeconds(45)
ZonedDateTime plusNanos(long nanos)Add nanosecondszdt.plusNanos(1000000)
ZonedDateTime minusDays(long days)Subtract dayszdt.minusDays(5)
ZonedDateTime minusHours(long hours)Subtract hourszdt.minusHours(2)
ZonedDateTime minusMinutes(long minutes)Subtract minuteszdt.minusMinutes(30)
ZonedDateTime minusSeconds(long seconds)Subtract secondszdt.minusSeconds(45)
ZonedDateTime minusNanos(long nanos)Subtract nanosecondszdt.minusNanos(1000000)
ZonedDateTime withYear(int year)Modify yearzdt.withYear(2024)
ZonedDateTime withMonth(int month)Modify monthzdt.withMonth(12)
ZonedDateTime withDayOfMonth(int day)Modify day of monthzdt.withDayOfMonth(1)
ZonedDateTime withHour(int hour)Modify hourzdt.withHour(0)
ZonedDateTime withMinute(int minute)Modify minutezdt.withMinute(0)
ZonedDateTime withSecond(int second)Modify secondzdt.withSecond(0)
ZonedDateTime withNano(int nano)Modify nanosecondzdt.withNano(0)
ZonedDateTime withZoneSameInstant(ZoneId zone)
← Java Datetimeformatter ClassJava Localdate Class β†’