Java ZonedDateTime Snippets

·

4 min read

Instantiation methods

now() and now(ZoneId zone)

// Current datetime from system clock with system's default timezone
ZonedDateTime nowDefault = ZonedDateTime.now();

// Current datetime from system clock in UTC
ZonedDateTime nowUTC = ZonedDateTime.now(ZoneOffset.UTC);

// Current datetime from system clock with specific timezone
ZonedDateTime nowIST = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));

System.out.println(nowDefault);
System.out.println(nowUTC);
System.out.println(nowIST);
## Output ##
2025-01-21T14:44:17.944442600+05:30[Asia/Calcutta]
2025-01-21T09:14:17.944442600Z
2025-01-21T14:44:17.944442600+05:30[Asia/Kolkata]

ofInstant(Instant instant, ZoneId zone)

// Arbitrary instant
Instant instant = Instant.ofEpochSecond(1737451942);

// ZDT with specific timezone
ZonedDateTime zdtIST = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata"));

// Existing ZDT converted to US timezone 
ZonedDateTime zdtPST = zdtIST.toInstant().atZone(ZoneId.of("GMT-08:00"));

System.out.println(instant);
System.out.println(zdtIST);
System.out.println(zdtPST);
## Output ##
2025-01-21T09:32:22Z
2025-01-21T15:02:22+05:30[Asia/Kolkata]
2025-01-21T01:32:22-08:00[GMT-08:00]

parse(CharSequence text)

try {
    String dateStr = "2025-01-21T15:02:22Z";
    ZonedDateTime zdt = ZonedDateTime.parse(dateStr);
    System.out.println(zdt);
} catch (DateTimeParseException exception) {
    System.out.println(exception.getLocalizedMessage());
}
## Output ##
2025-01-21T09:32:22Z
2025-01-21T15:02:22+05:30[Asia/Kolkata]
2025-01-21T01:32:22-08:00[GMT-08:00]

Timezone Conversion

withZoneSameInstant(ZoneId zone)

ZonedDateTime nowUTC = ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime nowUTCinIST = nowUTC.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
ZonedDateTime nowUTCConverted = nowUTCinIST.withZoneSameInstant(ZoneOffset.UTC);

System.out.println(nowUTC);
System.out.println(nowUTCinIST);
System.out.println(nowUTCConverted);
## Output ##
2025-01-21T10:17:22.863352Z
2025-01-21T15:47:22.863352+05:30[Asia/Kolkata]
2025-01-21T10:17:22.863352Z

Utils & Truncations

plus*(long number) and minus*(long number)

Static utility methods that return a copy of ZDT with plus/minus units specified.

ZonedDateTime thisInstantYesterday = nowIST.minusDays(1);
ZonedDateTime nextHourNextWeek = nowIST.plusHours(1).plusWeeks(1);
ZonedDateTime lastWeekInNextDecade = nowIST.minusWeeks(1).plusYears(10);

DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(nowIST.format(formatter));
System.out.println(thisInstantYesterday.format(formatter));
System.out.println(nextHourNextWeek.format(formatter));
System.out.println(lastWeekInNextDecade.format(formatter));
## Output ##
Wed, 22 Jan 2025 15:14:24 +0530
Tue, 21 Jan 2025 15:14:24 +0530
Wed, 29 Jan 2025 16:14:24 +0530
Mon, 15 Jan 2035 15:14:24 +0530

with*(long number)

Static utility methods that return a copy of ZDT, with only the specified temporal field modified.

ZonedDateTime thisInstantInDec = nowIST.withMonth(12);
ZonedDateTime thisInstantInMidMonth = nowIST.withDayOfMonth(15);
ZonedDateTime thisInstantIn2050 = nowIST.withYear(2050);

DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(nowIST.format(formatter));
System.out.println(thisInstantInDec.format(formatter));
System.out.println(thisInstantInMidMonth.format(formatter));
System.out.println(thisInstantIn2050.format(formatter));
## Output ##
Wed, 22 Jan 2025 15:09:28 +0530
Mon, 22 Dec 2025 15:09:28 +0530
Wed, 15 Jan 2025 15:09:28 +0530
Sat, 22 Jan 2050 15:09:28 +0530

long until(Temporal endExclusive, TemporalUnit unit)

// Calculates the amount of time until another date-time in terms of the specified unit.
long hoursToDeadline = nextHourNextWeek.until(lastWeekInNextDecade, ChronoUnit.HOURS);

System.out.println( hoursToDeadline + " hours left");
## Output ##
87311 hours left

truncatedTo(TemporalUnit unit)

  • Static methods that returns a copy of ZDT, that is truncated to the start of specified unit.

  • The truncation can work only if the unit is within a day's length. Otherwise UnsupportedTemporalTypeException is raised.

ZonedDateTime startOfHour = nowIST.truncatedTo(ChronoUnit.HOURS);
ZonedDateTime startOfDay = nowIST.truncatedTo(ChronoUnit.DAYS);
ZonedDateTime endOfDay = nowIST.truncatedTo(ChronoUnit.DAYS).plusHours(23).plusMinutes(59).plusSeconds(59);

DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(nowIST.format(formatter));
System.out.println(startOfHour.format(formatter));
System.out.println(startOfDay.format(formatter));
System.out.println(endOfDay.format(formatter));

try {
    ZonedDateTime startOfWeek = nowIST.truncatedTo(ChronoUnit.WEEKS);
    System.out.println(startOfWeek.format(formatter));
} catch (UnsupportedTemporalTypeException exception) {
    System.out.println(exception.getLocalizedMessage());
}
## Output ##
Wed, 22 Jan 2025 15:31:19 +0530
Wed, 22 Jan 2025 15:00:00 +0530
Wed, 22 Jan 2025 00:00:00 +0530
Wed, 22 Jan 2025 23:59:59 +0530
Unit is too large to be used for truncation

Printing

/*
Formats this date-time using the specified formatter.
This date-time will be passed to the formatter to produce a string.
*/
public String format(DateTimeFormatter formatter)

Static Formating Options

System.out.println("Default format: " + nowIST);
System.out.println("ISO Date Time format: " + nowIST.format(DateTimeFormatter.ISO_DATE_TIME));
System.out.println("RFC 1123 Date Time format: " + nowIST.format(DateTimeFormatter.RFC_1123_DATE_TIME));
System.out.println("ISO Date format: " + nowIST.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println("ISO Local Date format: " + nowIST.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println("ISO Local Time format: " + nowIST.format(DateTimeFormatter.ISO_LOCAL_TIME));
## Output ##
Default format: 2025-01-24T14:52:36.862067600+05:30[Asia/Kolkata]
ISO Date Time format: 2025-01-24T14:52:36.8620676+05:30[Asia/Kolkata]
RFC 1123 Date Time format: Fri, 24 Jan 2025 14:52:36 +0530
ISO Date format: 20250124+0530
ISO Local Date format: 2025-01-24
ISO Local Time format: 14:52:36.8620676

Custom Formating

## Placeholder format specifiers ##

‘y’: year
‘M’: month (in number form, e.g. “1” for January)
‘MMM’: month abbreviation (e.g. “Jan”)
‘MMMM’: full month name (e.g. “January”)
‘d’: day of the month
‘E’: day of week abbreviation (e.g. “Mon”)
‘EEEE’: full day of week name (e.g. “Monday”)
‘h’: hour (in 1-12 form)
‘H’: hour (in 0-23 form)
‘m’: minute
‘s’: second
‘S’: fraction of second
‘a’: AM/PM marker
‘z’: time zone abbreviation (e.g. “PST”)
‘Z’: time zone offset (e.g. “-0800”)
System.out.println(nowIST.format(DateTimeFormatter.ofPattern("HH:mm a, MMM yyyy")));
System.out.println(nowIST.format(DateTimeFormatter.ofPattern("HH:mm a, E, MMMM yyyy, z")));
## Output ##
15:05 pm, Jan 2025
15:05 pm, Fri, January 2025, IST

References