Java 8 – ZonedDateTime with method details and examples

In this article, we will discuss about newly introduced ZonedDateTime class in Java 1.8 version for dealing with date and time along with Zone information in program with ease and convenience.

Prior to introducing LocalDateLocalTimeLocalDateTime, OffsetDateTime and ZonedDateTime in Java 1.8 version, we have to deal with java.util.Datejava.util.Calendarjava.sql.Timestampjava.sql.Time, java.util.TimeZone for date/time/zone handling in Java which isn’t easy & straight-forward and there are few issues as mentioned below,

  • Thread-safety :- Existing Date/Time classes and its methods aren’t thread-safe and hence it’s not convenient to handle in concurrent/parallel environment
  • Not so-easy API design :- Existing Date/Time classes’ methods aren’t convenient to use in day-to-day programmer’s coding or development
  • Time-zone settings :- Developers or Programmer’s life becomes difficult while dealing with time zone settings in programs

Let’s move forward and discuss about java.time.ZonedDateTime introduced in Java 1.8 version

1. ZonedDateTime v/s LocalDateTime:

Before moving forward to detail out about ZonedDateTime, let’s understand what are the difference between LocalDateTime & ZonedDateTime and what extra information we get from using ZonedDateTime instead of using plain LocalDateTime

  • ZonedDateTime = LocalDateTime + Time-Zone
  • That’s ZonedDateTime provides all those information what LocalDateTime provide plus Zone information in the ISO-8601 calendar system
  • In the below illustration, we are printing ZonedDateTime & LocalDateTime twice
    • First in the system default zone (i.e., Asia/Calcutta)
    • Second, after providing ZoneId (America/Los_Angeles) as argument to overloaded now() method
  • Both ZonedDateTime & LocalDateTime prints current Date/time in their specific Zone
  • ZonedDateTime provides date-time to nano-second precision plus time-difference from Greenwich Mean Time (GMT) and time-zone information in square bracket like,
    • +05:30[Asia/Calcutta]
    • -07:00[America/Los_Angeles]
  • Note: Time-difference from Greenwich Mean Time is known as Offset

ZonedDateTimeVsLocalDateTime.java

package in.bench.resources.zoneddatetime.sorting;

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

public class ZonedDateTimeVsLocalDateTime {

	public static void main(String[] args) {

		// 1. LocalDateTime and ZonedDateTime in system default zone
		System.out.println("LocalDateTime & ZonedDateTime in default zone - " 
				+ ZoneId.systemDefault());


		// 1.1 current date/time in default zone
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("\n1. ZonedDateTime  - " + zonedDateTime);

		// 1.2 current date/time in default zone
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("2. LocalDateTime - " + localDateTime);



		// 2. LocalDateTime and ZonedDateTime in America/Los_Angeles Zone 
		ZoneId zoneId = ZoneId.of("America/Los_Angeles");
		System.out.println("\n\nLocalDateTime & ZonedDateTime in America/Los_Angeles Zone");


		// 2.1 current date/time in specific/passed zone
		ZonedDateTime zonedDateTime2 = ZonedDateTime.now(zoneId);
		System.out.println("\n1. ZonedDateTime  - " + zonedDateTime2);


		// 2.2 current date/time in specific/passed zone
		LocalDateTime localDateTime2 = LocalDateTime.now(zoneId);
		System.out.print("2. LocalDateTime - " + localDateTime2);
	}
}

Output:

LocalDateTime & ZonedDateTime in default zone - Asia/Calcutta

1. ZonedDateTime  - 2022-08-15T13:10:02.987935700+05:30[Asia/Calcutta]
2. LocalDateTime - 2022-08-15T13:10:02.987935700


LocalDateTime & ZonedDateTime in America/Los_Angeles Zone

1. ZonedDateTime  - 2022-08-15T00:40:02.988935800-07:00[America/Los_Angeles]
2. LocalDateTime - 2022-08-15T00:40:03.000548500

2. ZonedDateTime :

  • There are 3 ways to get/form a ZonedDateTime,
    1. First is to get current system date/time in default system time-zone using static factory method now() or get current system date/time in specific time-zone by providing ZoneId as argument to static factory method now(ZoneId)
    2. Second is to form a ZonedDateTime using static factory method of() passing year, month, day, hour, minute, second, nano and time-zone information (there are 3 variants)
    3. Third and final is to parse date/time/zone in String-form to ZonedDateTime using static factory method parse() such as 2007-12-03T10:15:30+01:00[Europe/Paris]
  • The fully qualified package/class name of ZonedDateTime is java.time.ZonedDateTime i.e.; ZonedDateTime is present under java.time package
  • Class declaration for ZonedDateTime is as follows,
package java.time;

public final class ZonedDateTime
        implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
}

3. ZonedDateTime methods or APIs :

Important ZonedDateTime method details,

  • now() – get current date-time to nano-second precision from the system clock in the default time-zone
  • now(ZoneId) – get current date-time to nano-second precision from the system clock in the specific time-zone provided as method-argument
  • of() – get an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanoseconds and time-zone passed
  • parse() – get an instance of ZonedDateTime from a text string in yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region] or yyyy-MM-ddTHH:mm:ss+/-HH:mm[Continent/Region] or yyyy-MM-ddTHH:mm+/-HH:mm[Continent/Region] formats
  • getYear() – get the Year field from ZonedDateTime
  • getMonthValue() – get the month-of-year field from 1 to 12 from ZonedDateTime 
  • getMonth() – get the month-of-year field using the Month enum from ZonedDateTime 
  • getDayOfMonth() – get the day-of-month field from ZonedDateTime 
  • getDayOfWeek() – get the day-of-week field, which is an enum DayOfWeek from ZonedDateTime 
  • getDayOfYear() – get the day-of-year field from ZonedDateTime 
  • getHour() – get the hour-of-day field from ZonedDateTime 
  • getMinute() – get the minute-of-hour field from ZonedDateTime 
  • getSecond() – get the  second-of-minute field from ZonedDateTime 
  • getNano() – get the nano-of-second field from ZonedDateTime 
  • getZone() – get the time-zone field from ZonedDateTime such as ‘Europe/Paris’.
  • getOffset() – get the zone offset, such as ‘+01:00’
  • plusDays() – Returns a copy of invoking ZonedDateTime with the specified number of days added
  • plusWeeks() – Returns a copy of invoking ZonedDateTime with the specified number of weeks added
  • plusMonths() – Returns a copy of invoking ZonedDateTime with the specified number of months added
  • plusYears() – Returns a copy of invoking ZonedDateTime with the specified number of years added
  • plusNanos() – Returns a copy of invoking ZonedDateTime with the specified number of nanoseconds added
  • plusSeconds() – Returns a copy of invoking ZonedDateTime with the specified number of seconds added
  • plusMinutes() – Returns a copy of invoking ZonedDateTime with the specified number of minutes added
  • plusHours() – Returns a copy of invoking ZonedDateTime with the specified number of hours added
  • minusDays() – Returns a copy of invoking ZonedDateTime with the specified number of days subtracted
  • minusWeeks() – Returns a copy of invoking ZonedDateTime with the specified number of weeks subtracted
  • minusMonths() – Returns a copy of invoking ZonedDateTime with the specified number of months subtracted
  • minusYears() – Returns a copy of invoking ZonedDateTime with the specified number of years subtracted
  • minusNanos() – Returns a copy of invoking ZonedDateTime with the specified number of nanoseconds subtracted
  • minusSeconds() – Returns a copy of invoking ZonedDateTime with the specified number of seconds subtracted
  • minusMinutes() – Returns a copy of invoking ZonedDateTime with the specified number of minutes subtracted
  • minusHours() – Returns a copy of invoking ZonedDateTime with the specified number of hours subtracted
  • format() – Formats invoking date-time with time-zone information using the specified date formatter
  • withDayOfMonth() – Returns a copy of this ZonedDateTime with the day-of-month altered
  • withMonth() – Returns a copy of this ZonedDateTime with the month-of-year altered
  • withYear() – Returns a copy of this ZonedDateTime with the year altered
  • withHour() – Returns a copy of this ZonedDateTime with the hour-of-day altered
  • withMinute() – Returns a copy of this ZonedDateTime with the minute-of-hour altered
  • withSecond() – Returns a copy of this ZonedDateTime with the second-of-minute altered
  • withNano() – Returns a copy of this ZonedDateTime with the nano-of-second altered
  • withZoneSameInstant() – Returns a copy of invoking ZonedDateTime with a different time-zone, retaining the instant
  • withZoneSameLocal() – Returns a copy of invoking ZonedDateTime with a different time-zone, retaining the local date-time if possible
  • isBefore(ChronoZonedDateTime) – checks if the invoking ZonedDateTime is before the specified ZonedDateTime
  • isAfter(ChronoZonedDateTime) – checks if invoking ZonedDateTime is after the specified ZonedDateTime
  • toLocalDateTime()- gets the LocalDateTime part from invoking ZonedDateTime
  • toOffsetDateTime() – Converts invoking ZonedDateTime to an OffsetDateTime
  • toInstant() – Converts invoking ZonedDateTime to an Instant
  • toLocalDate()- gets the LocalDate part from invoking ZonedDateTime
  • toLocalTime()- gets the LocalTime part from invoking ZonedDateTime

4. ZonedDateTime examples :

  1. ZonedDateTime.now() – get current date/time with time-zone information from system clock in the default zone
  2. ZonedDateTime.now(ZoneId) – get current date/time with time-zone information from system clock in the specific zone
  3. ZonedDateTime.of() – form ZonedDateTime passing Year, Month, Day, Hour, Minute, Second, Nano and Time-Zone fields
  4. ZonedDateTime.parse() – parse the date/time and time-zone in String-form to ZonedDateTime
  5. Adding day, week, month and year to ZonedDateTime using plusDays(), plusWeeks(), plusMonths() and plusYears() methods respectively
  6. Adding nano, second, minute and hour to ZonedDateTime using plusNanos(), plusSeconds(), plusMinutes() and plusHours() methods respectively
  7. Subtracting day, week, month and year from ZonedDateTime using minusDays(), minusWeeks(), minusMonths() and minusYears() methods respectively
  8. Subtracting nano, second, minute and hour from ZonedDateTime using minusNanos(), minusSeconds(), minusMinutes() and minusHours() methods respectively
  9. Formatting ZonedDateTime in different formats using DateTimeFormatter class
  10. Altering day, month, year, nano, second, minute, hour and time-zone fields with ZonedDateTime using withDayOfMonth(), withMonth(), withYear(), withNano(), withSecond(), withMinute(), withHour() and withZoneSameInstant() methods respectively
  11. Check ZonedDateTime is Before/After another ZonedDateTime using below methods,
    • isBefore(ChronoZonedDateTime) – checks if the invoking ZonedDateTime is before the specified ZonedDateTime 
    • isAfter(ChronoZonedDateTime) – Checks if invoking ZonedDateTime is after the specified ZonedDateTime 
  12. Convert ZonedDateTime to LocalDateTime/OffsetDateTime/Instant/LocalDate/LocalTime

4.1 ZonedDateTime.now() method – get Current System Date/time in default zone

  • ZonedDateTime.now() method helps to get current system date/time in the default zone which will be in the yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region] format
  • We can get year, month, day, hour, minute, second, nano-second and time-zone field/part from ZonedDateTime using different methods mentioned above and then we can form our own format as required like “dd.MM.yyyy HH:mm:ss.nnn
  • Zone used – default zone [Asia/Calcutta]
  • Read Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ? for more details and examples

ZonedDateTimeExampleUsingNowMethod.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeExampleUsingNowMethod {

	public static void main(String[] args) {

		// get current system date along with time in default zone
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Current date/time in default zone is = " + zonedDateTime);


		// 1. Date part
		System.out.println("\n1. Date part from ZonedDateTime : \n");


		// 1.1 get YEAR part from current system date
		int year = zonedDateTime.getYear();
		System.out.println("Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = zonedDateTime.getMonthValue();
		System.out.println("Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = zonedDateTime.getMonth();
		System.out.println("Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = zonedDateTime.getDayOfMonth();
		System.out.println("Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
		System.out.println("Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println("Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n2. Time part from ZonedDateTime : \n");


		// 2.1 get HOUR value from current system time
		int hours = zonedDateTime.getHour();
		System.out.println("Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = zonedDateTime.getMinute();
		System.out.println("Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = zonedDateTime.getSecond();
		System.out.println("Seconds is : " + seconds);


		// 2.4 get NANO SECOND value from current system time
		int nano = zonedDateTime.getNano();
		System.out.println("Nano Seconds is : " + nano);



		// 3. Zone part
		System.out.println("\n3. Zone part from ZonedDateTime : \n");


		// 3.1 get ZONE part from current system zone
		ZoneId zoneId = zonedDateTime.getZone();
		System.out.println("Date/time in which zone ? " + zoneId);


		// 3.2 get OFFSET part from current system zone
		ZoneOffset zoneOffset = zonedDateTime.getOffset();
		System.out.println("Offset  is : " + zoneOffset);
	}
}

Output:

Current date/time in default zone is = 2022-06-25T22:05:23.816746900+05:30[Asia/Calcutta]

1. Date part from ZonedDateTime : 

Year is : 2022
Month is : 6
Month in Words is : JUNE
Day is : 25
Day of Week is : SATURDAY
Day of Year is : 176

2. Time part from ZonedDateTime : 

Hour is : 22
Minutes is : 5
Seconds is : 23
Nano Seconds is : 816746900

3. Zone part from ZonedDateTime : 

Date/time in which zone ? Asia/Calcutta
Offset  is : +05:30

4.2 ZonedDateTime.now(ZoneId) method – get Current System Date/time in specific zone

  • ZonedDateTime.now(ZoneId) method helps to get current system date/time in specific zone which will be in the yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region] format
  • We can get year, month, day, hour, minute, second, nano-second and time-zone field/part from ZonedDateTime using different methods mentioned above and then we can form our own format as required like “dd.MM.yyyy HH:mm:ss.nnn
  • Zone used – [America/Los_Angeles]
  • Read Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ? for more details and examples

ZonedDateTimeExampleUsingNowMethod2.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeExampleUsingNowMethod2 {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("America/Los_Angeles");


		// get current date along with time in specific zone (America/Los_Angeles)
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("Current date/time in specific zone is = " + zonedDateTime);


		// 1. Date part
		System.out.println("\n1. Date part from ZonedDateTime : \n");


		// 1.1 get YEAR part from current system date
		int year = zonedDateTime.getYear();
		System.out.println("Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = zonedDateTime.getMonthValue();
		System.out.println("Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = zonedDateTime.getMonth();
		System.out.println("Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = zonedDateTime.getDayOfMonth();
		System.out.println("Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
		System.out.println("Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println("Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n2. Time part from ZonedDateTime : \n");


		// 2.1 get HOUR value from current system time
		int hours = zonedDateTime.getHour();
		System.out.println("Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = zonedDateTime.getMinute();
		System.out.println("Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = zonedDateTime.getSecond();
		System.out.println("Seconds is : " + seconds);


		// 2.4 get NANO SECOND value from current system time
		int nano = zonedDateTime.getNano();
		System.out.println("Nano Seconds is : " + nano);



		// 3. Zone part
		System.out.println("\n3. Zone part from ZonedDateTime : \n");


		// 3.1 get ZONE part from current system zone
		ZoneId zoneId2 = zonedDateTime.getZone();
		System.out.println("Date/time in which zone ? " + zoneId2);


		// 3.2 get OFFSET part from current system zone
		ZoneOffset zoneOffset = zonedDateTime.getOffset();
		System.out.println("Offset  is : " + zoneOffset);
	}
}

Output:

Current date/time in specific zone is = 2022-06-25T09:35:45.197069300-07:00[America/Los_Angeles]

1. Date part from ZonedDateTime : 

Year is : 2022
Month is : 6
Month in Words is : JUNE
Day is : 25
Day of Week is : SATURDAY
Day of Year is : 176

2. Time part from ZonedDateTime : 

Hour is : 9
Minutes is : 35
Seconds is : 45
Nano Seconds is : 197069300

3. Zone part from ZonedDateTime : 

Date/time in which zone ? America/Los_Angeles
Offset  is : -07:00

4.3 LocalDateTime.of() method – form Date/time

ZonedDateTimeExampleUsingOfMethod.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZonedDateTimeExampleUsingOfMethod {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("Australia/Sydney");


		// for specific date along with time
		ZonedDateTime zonedDateTime = ZonedDateTime.of(1950, 1, 26, 11, 45, 37, 987000000, zoneId);
		System.out.println("Date/time is = " + zonedDateTime);


		// 1. Date part
		System.out.println("\n1. Date part from ZonedDateTime : \n");


		// 1.1 get YEAR part from current system date
		int year = zonedDateTime.getYear();
		System.out.println("Year is : " + year);


		// 1.2 get MONTH part from current system date
		int month = zonedDateTime.getMonthValue();
		System.out.println("Month is : " + month);


		// 1.3 get MONTH part from current system date
		Month monthInWords = zonedDateTime.getMonth();
		System.out.println("Month in Words is : " + monthInWords);


		// 1.4 get DAY part from current system date
		int day = zonedDateTime.getDayOfMonth();
		System.out.println("Day is : " + day);


		// 1.5 get DAY part from current system date
		DayOfWeek dayOfWeek = zonedDateTime.getDayOfWeek();
		System.out.println("Day of Week is : " + dayOfWeek);


		// 1.6 get DAY part from current system date
		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println("Day of Year is : " + dayOfYear);



		// 2. Time part
		System.out.println("\n2. Time part from ZonedDateTime : \n");


		// 2.1 get HOUR value from current system time
		int hours = zonedDateTime.getHour();
		System.out.println("Hour is : " + hours);


		// 2.2 get MINUTE value from current system time
		int minutes = zonedDateTime.getMinute();
		System.out.println("Minutes is : " + minutes);


		// 2.3 get SECOND value from current system time
		int seconds = zonedDateTime.getSecond();
		System.out.println("Seconds is : " + seconds);


		// 2.4 get NANO SECOND value from current system time
		int nano = zonedDateTime.getNano();
		System.out.println("Nano Seconds is : " + nano);



		// 3. Zone part
		System.out.println("\n3. Zone part from ZonedDateTime : \n");


		// 3.1 get ZONE part from current system zone
		ZoneId zoneId2 = zonedDateTime.getZone();
		System.out.println("Date/time in which zone ? " + zoneId2);


		// 3.2 get OFFSET part from current system zone
		ZoneOffset zoneOffset = zonedDateTime.getOffset();
		System.out.println("Offset  is : " + zoneOffset);
	}
}

Output:

Date/time is = 1950-01-26T11:45:37.987+10:00[Australia/Sydney]

1. Date part from ZonedDateTime : 

Year is : 1950
Month is : 1
Month in Words is : JANUARY
Day is : 26
Day of Week is : THURSDAY
Day of Year is : 26

2. Time part from ZonedDateTime : 

Hour is : 11
Minutes is : 45
Seconds is : 37
Nano Seconds is : 987000000

3. Zone part from ZonedDateTime : 

Date/time in which zone ? Australia/Sydney
Offset  is : +10:00

4.4 ZonedDateTime.parse() method – get Date/time/zone in String-form

  • Sometimes, we need to parse date/time with zone passed in String-form to ZonedDateTime, for that we can use ZonedDateTime.parse() method which will return ZonedDateTime in yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region] format
  • While parsing Date/Time with Zone, value in String-form should be in either of the below formats only, otherwise java.time.format.DateTimeParseException will be thrown
    • yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region]
    • yyyy-MM-ddTHH:mm:ss+/-HH:mm[Continent/Region]
    • yyyy-MM-ddTHH:mm+/-HH:mm[Continent/Region]
  • Zone used – [Australia/Sydney]
  • Read below articles for more details and examples,
    1. Java 8 – How to parse ZonedDateTime in String form ?
    2. Java 8 – How to convert String to ZonedDateTime ?

ZonedDateTimeExampleUsingParseMethod.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZonedDateTime;

public class ZonedDateTimeExampleUsingParseMethod {

	public static void main(String[] args) {

		// 1. ZonedDateTime with all fields y, M, d, H, m, s, n and X, V
		String dateTimeZoneInStr1 = "1950-01-26T11:45:37.987654321+10:00[Australia/Sydney]";


		// 1.1 convert/parse dateInString to ZonedDateTime
		ZonedDateTime dateTimeZone1 = ZonedDateTime.parse(dateTimeZoneInStr1);
		System.out.println("1. Parsed Date/time (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region]) "
				+ "in specific zone is - \n" + dateTimeZone1);



		// 2. ZonedDateTime with fields y, M, d, H, m, s and X, V
		String dateTimeZoneInStr2 = "1950-01-26T11:45:37+10:00[Australia/Sydney]";


		// 2.1 convert/parse dateInString to ZonedDateTime
		ZonedDateTime dateTimeZone2 = ZonedDateTime.parse(dateTimeZoneInStr2);
		System.out.println("\n2. Parsed Date/time (yyyy-MM-ddTHH:mm:ss+/-HH:mm[Continent/Region]) "
				+ "in specific zone is - \n" + dateTimeZone2);



		// 3. ZonedDateTime with fields y, M, d, H, m and X, V
		String dateTimeZoneInStr3 = "1950-01-26T11:45+10:00[Australia/Sydney]";


		// 3.1 convert/parse dateInString to ZonedDateTime
		ZonedDateTime dateTimeZone3 = ZonedDateTime.parse(dateTimeZoneInStr3);
		System.out.println("\n3. Parsed Date/time (yyyy-MM-ddTHH:mm+/-HH:mm[Continent/Region]) "
				+ "in specific zone is - \n" + dateTimeZone3);
	}
}

Output:

1. Parsed Date/time (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region]) in specific zone is - 
1950-01-26T11:45:37.987654321+10:00[Australia/Sydney]

2. Parsed Date/time (yyyy-MM-ddTHH:mm:ss+/-HH:mm[Continent/Region]) in specific zone is - 
1950-01-26T11:45:37+10:00[Australia/Sydney]

3. Parsed Date/time (yyyy-MM-ddTHH:mm+/-HH:mm[Continent/Region]) in specific zone is - 
1950-01-26T11:45+10:00[Australia/Sydney]

4.5 Adding Day/Week/Month/Year to ZonedDateTime :

  1. Add 5 Days to ZonedDateTime using plusDays() method
  2. Add 2 Weeks to ZonedDateTime using plusWeeks() method
  3. Add 3 Months to ZonedDateTime using plusMonths() method
  4. Add 1 Year to ZonedDateTime using plusYears() method
    • Zone used – [Africa/Harare]
  5. Read Java 8 – How to add Date and Time fields to ZonedDateTime ? for more details and examples

AddDatePartWithZonedDateTime.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class AddDatePartWithZonedDateTime {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("Africa/Harare");


		// 1. get Zoned date/time with zone info passed
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("Zoned Date/time in specific zone is = " 
				+ zonedDateTime);


		// 1.1 add 5 days with zone date/time
		ZonedDateTime add_5_Days = zonedDateTime.plusDays(5);
		System.out.println("\n1. After adding 5 Days to Zoned Date/time is = " 
				+ add_5_Days);


		// 1.2 add 2 weeks to zone date/time
		ZonedDateTime add_2_Weeks = zonedDateTime.plusWeeks(2);
		System.out.println("2. After adding 2 Weeks to Zoned Date/time is = " 
				+ add_2_Weeks);


		// 1.3 add 3 months to zone date/time
		ZonedDateTime add_3_Months = zonedDateTime.plusMonths(3);
		System.out.println("3. After adding 3 Months to Zoned Date/time is = " 
				+ add_3_Months);


		// 1.4 add 1 year to zone date/time
		ZonedDateTime add_1_Year = zonedDateTime.plusYears(1);
		System.out.print("4. After adding 1 Year to Zoned Date/time is = " 
				+ add_1_Year);
	}
}

Output:

Zoned Date/time in specific zone is = 2022-08-15T09:48:38.218302900+02:00[Africa/Harare]

1. After adding 5 Days to Zoned Date/time is = 2022-08-20T09:48:38.218302900+02:00[Africa/Harare]
2. After adding 2 Weeks to Zoned Date/time is = 2022-08-29T09:48:38.218302900+02:00[Africa/Harare]
3. After adding 3 Months to Zoned Date/time is = 2022-11-15T09:48:38.218302900+02:00[Africa/Harare]
4. After adding 1 Year to Zoned Date/time is = 2023-08-15T09:48:38.218302900+02:00[Africa/Harare]

4.6 Adding Nano/Second/Minute/Hour to ZonedDateTime :

  1. Add 125 Nanos to ZonedDateTime using plusNanos() method
  2. Add 37 Seconds to ZonedDateTime using plusSeconds() method
  3. Add 19 Minutes to ZonedDateTime using plusMinutes() method
  4. Add 5 Hours to ZonedDateTime using plusHours() method
    • Zone used – [Europe/Paris]
  5. Read Java 8 – How to add Date and Time fields to ZonedDateTime ? for more details and examples

AddTimePartWithZonedDateTime.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class AddTimePartWithZonedDateTime {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("Europe/Paris");


		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("Zoned Date/time is - " + zonedDateTime);


		// 1.1 add 125 NanoSeconds to Zoned Date/time
		ZonedDateTime add_125_Nanos = zonedDateTime.plusNanos(125);
		System.out.println("\n1. After adding 125 Nano Seconds to Zoned Date/time is - " 
				+ add_125_Nanos);


		// 1.2 add 37 Seconds to Zoned Date/time
		ZonedDateTime add_37_Seconds = zonedDateTime.plusSeconds(37);
		System.out.println("2. After adding 37 Seconds to Zoned Date/time is - " 
				+ add_37_Seconds);


		// 1.3 add 19 Minutes to Zoned Date/time
		ZonedDateTime add_19_Minutes = zonedDateTime.plusMinutes(19);
		System.out.println("3. After adding 19 Minutes to Zoned Date/time is - " 
				+ add_19_Minutes);


		// 1.4 add 5 Hours to Zoned Date/time
		ZonedDateTime add_5_Hours = zonedDateTime.plusHours(5);
		System.out.print("4. After adding 5 Hours to Zoned Date/time is - " 
				+ add_5_Hours);
	}
}

Output:

Zoned Date/time is - 2022-08-15T09:49:28.633045700+02:00[Europe/Paris]

1. After adding 125 Nano Seconds to Zoned Date/time is - 2022-08-15T09:49:28.633045825+02:00[Europe/Paris]
2. After adding 37 Seconds to Zoned Date/time is - 2022-08-15T09:50:05.633045700+02:00[Europe/Paris]
3. After adding 19 Minutes to Zoned Date/time is - 2022-08-15T10:08:28.633045700+02:00[Europe/Paris]
4. After adding 5 Hours to Zoned Date/time is - 2022-08-15T14:49:28.633045700+02:00[Europe/Paris]

4.7 Subtracting Day/Week/Month/Year from ZonedDateTime :

  1. Subtract 5 Days from ZonedDateTime using minusDays() method
  2. Subtract 2 Weeks from ZonedDateTime using minusWeeks() method
  3. Subtract 3 Months from ZonedDateTime using minusMonths() method
  4. Subtract 1 Year from ZonedDateTime using minusYears() method
    • Zone used – [Asia/Shanghai]
  5. Read Java 8 – How to subtract Date and Time fields from ZonedDateTime ? for more details and examples

SubtractDatePartFromZonedDateTime.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class SubtractDatePartFromZonedDateTime {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("Asia/Shanghai");


		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("Zoned Date/time is - " + zonedDateTime);


		// 1.1 subtract 5 days from Zoned Date/time
		ZonedDateTime subtract_5_Days = zonedDateTime.minusDays(5);
		System.out.println("\n1. After subtracting 5 Days from Zoned Date/time is - " 
				+ subtract_5_Days);


		// 1.2 subtract 2 weeks from Zoned Date/time
		ZonedDateTime subtract_2_Weeks = zonedDateTime.minusWeeks(2);
		System.out.println("2. After subtracting 2 Weeks from Zoned Date/time is - " 
				+ subtract_2_Weeks);


		// 1.3 subtract 3 months from Zoned Date/time
		ZonedDateTime subtract_3_Months = zonedDateTime.minusMonths(3);
		System.out.println("3. After subtracting 3 Months from Zoned Date/time is - " 
				+ subtract_3_Months);


		// 1.4 subtract 1 year from Zoned Date/time
		ZonedDateTime subtract_1_Year = zonedDateTime.minusYears(1);
		System.out.print("4. After subtracting 1 Year from Zoned Date/time is - " 
				+ subtract_1_Year);
	}
}

Output:

Zoned Date/time is - 2022-08-15T15:50:10.905729300+08:00[Asia/Shanghai]

1. After subtracting 5 Days from Zoned Date/time is - 2022-08-10T15:50:10.905729300+08:00[Asia/Shanghai]
2. After subtracting 2 Weeks from Zoned Date/time is - 2022-08-01T15:50:10.905729300+08:00[Asia/Shanghai]
3. After subtracting 3 Months from Zoned Date/time is - 2022-05-15T15:50:10.905729300+08:00[Asia/Shanghai]
4. After subtracting 1 Year from Zoned Date/time is - 2021-08-15T15:50:10.905729300+08:00[Asia/Shanghai]

4.8 Subtracting Nano/Second/Minute/Hour from ZonedDateTime :

  1. Subtract 125 Nanos from ZonedDateTime using minusNanos() method
  2. Subtract 37 Seconds from ZonedDateTime using minusSeconds() method
  3. Subtract 19 Minutes from ZonedDateTime using minusMinutes() method
  4. Subtract 5 Hours from ZonedDateTime using minusHours() method
    • Zone used – [Pacific/Auckland]
  5. Read Java 8 – How to subtract Date and Time fields from ZonedDateTime ? for more details and examples

SubtractTimePartFromZonedDateTime.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class SubtractTimePartFromZonedDateTime {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("Pacific/Auckland");


		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("Zoned Date/time is - " + zonedDateTime);


		// 1.1 subtract 125 NanoSeconds from Zoned Date/time
		ZonedDateTime sub_125_Nanos = zonedDateTime.minusNanos(125);
		System.out.println("\n1. After subtracting 125 Nano Seconds from Zoned Date/time is - \n" 
				+ sub_125_Nanos);


		// 1.2 subtract 37 Seconds from Zoned Date/time
		ZonedDateTime sub_37_Seconds = zonedDateTime.minusSeconds(37);
		System.out.println("\n2. After subtracting 37 Seconds from Zoned Date/time is - \n" 
				+ sub_37_Seconds);


		// 1.3 subtract 19 Minutes from Zoned Date/time
		ZonedDateTime sub_19_Minutes = zonedDateTime.minusMinutes(19);
		System.out.println("\n3. After subtracting 19 Minutes from Zoned Date/time is - \n" 
				+ sub_19_Minutes);


		// 1.4 subtract 5 Hours from Zoned Date/time
		ZonedDateTime sub_5_Hours = zonedDateTime.minusHours(5);
		System.out.print("\n4. After subtracting 5 Hours from Zoned Date/time is - \n" 
				+ sub_5_Hours);
	}
}

Output:

Zoned Date/time is - 2022-08-17T23:04:13.726982600+12:00[Pacific/Auckland]

1. After subtracting 125 Nano Seconds from Zoned Date/time is - 
2022-08-17T23:04:13.726982475+12:00[Pacific/Auckland]

2. After subtracting 37 Seconds from Zoned Date/time is - 
2022-08-17T23:03:36.726982600+12:00[Pacific/Auckland]

3. After subtracting 19 Minutes from Zoned Date/time is - 
2022-08-17T22:45:13.726982600+12:00[Pacific/Auckland]

4. After subtracting 5 Hours from Zoned Date/time is - 
2022-08-17T18:04:13.726982600+12:00[Pacific/Auckland]

4.9 Formatting ZonedDateTime using DateTimeFormatter:

  • We can convert default ISO_LOCAL_DATE_TIME ZonedDateTime format yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm[Continent/Region] to any other formats using ZonedDateTime.format() method by passing DateTimeFormatter class as argument with required pattern in String-form
  • In this illustration, we are using 6 different custom formats as mentioned below,
    1. DateTimeFormatter.ofPattern(“dd.MM.yyyy“)
    2. DateTimeFormatter.ofPattern(“dd-MMM-yyyy“)
    3. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm“)
    4. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss“)
    5. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss.nnn“)
    6. DateTimeFormatter.ofPattern(“dd-MM-yyyy HH:mm:ss.nnn O [VV]“)
  • Zone used – [America/Indiana/Indianapolis]
  • Read below articles for more details and examples,
    1. Java 8 – How to convert ZonedDateTime to String ?
    2. Java 8 – How to convert ZonedDateTime in different formats ?
    3. Java 8 – How to convert ZonedDateTime in different Format Style ?

FormattingZonedDateTimeUsingFormatMethod.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class FormattingZonedDateTimeUsingFormatMethod {

	public static void main(String[] args) {

		// ZoneId
		ZoneId zoneId = ZoneId.of("America/Indiana/Indianapolis");


		// 1. get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
		System.out.println("1. Zoned Date/time in ISO_ZONED_DATE_TIME format is = \n"
				+ zonedDateTime);


		// 1.1 format ZonedDateTime to dd.MM.yyyy
		String formattedDate = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd.MM.yyyy"));
		System.out.println("\n1.1 Zoned Date/time in dd.MM.yyyy format is = "
				+ formattedDate);


		// 1.2 format ZonedDateTime to dd-MMM-yyyy
		String formattedDate2 = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd-MMM-yyyy"));
		System.out.println("\n1.2 Zoned Date/time in dd-MMM-yyyy format is = " 
				+ formattedDate2);


		// 1.3 format ZonedDateTime to dd-MM-yyyy HH:mm
		String formattedDate3 = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm"));
		System.out.println("\n1.3 Zoned Date/time in dd-MM-yyyy HH:mm format is = " 
				+ formattedDate3);


		// 1.4 format ZonedDateTime to dd-MM-yyyy HH:mm:ss
		String formattedDate4 = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm:ss"));
		System.out.println("\n1.4 Zoned Date/time in dd-MM-yyyy HH:mm:ss format is = " 
				+ formattedDate4);


		// 1.5 format ZonedDateTime to dd-MM-yyyy HH:mm:ss.nnn
		String formattedDate5 = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm:ss.nnn"));
		System.out.println("\n1.5 Zoned Date/time in dd-MM-yyyy HH:mm:ss.nnn format is = "
				+ formattedDate5);


		// 1.6 format ZonedDateTime to dd-MM-yyyy HH:mm:ss.nnn O [VV]
		String formattedDate6 = zonedDateTime.format(DateTimeFormatter
				.ofPattern("dd-MM-yyyy HH:mm:ss.nnn O [VV]"));
		System.out.println("\n1.6 Zoned Date/time in dd-MM-yyyy HH:mm:ss.nnn O [VV] format is = \n"
				+ formattedDate6);
	}
}

Output:

1. Zoned Date/time in ISO_ZONED_DATE_TIME format is = 
2022-06-25T12:43:14.544841800-04:00[America/Indiana/Indianapolis]

1.1 Zoned Date/time in dd.MM.yyyy format is = 25.06.2022

1.2 Zoned Date/time in dd-MMM-yyyy format is = 25-Jun-2022

1.3 Zoned Date/time in dd-MM-yyyy HH:mm format is = 25-06-2022 12:43

1.4 Zoned Date/time in dd-MM-yyyy HH:mm:ss format is = 25-06-2022 12:43:14

1.5 Zoned Date/time in dd-MM-yyyy HH:mm:ss.nnn format is = 25-06-2022 12:43:14.544841800

1.6 Zoned Date/time in dd-MM-yyyy HH:mm:ss.nnn O [VV] format is = 
25-06-2022 12:43:14.544841800 GMT-4 America/Indiana/Indianapolis

4.10 Altering Day/Month/Year & Nano/Second/Minute/Hour & ZoneId fields with ZonedDateTime:

  • Altering Day, Month, Year, Hour, Minute, Second, Nano-second and time-zone field/part of ZonedDateTime is possible with the help below methods,
    • withDayOfMonth() – This method alters day-of-month part/field of the invoking ZonedDateTime
    • withMonth() – This method alters month-of-year part/field of the invoking ZonedDateTime
    • withYear() – This method alters year part/field of the invoking ZonedDateTime
    • withHour() – This method alters hour part/field of the invoking ZonedDateTime
    • withMinute() – This method alters minute part/field of the invoking ZonedDateTime
    • withSecond() – This method alters second part/field of the invoking ZonedDateTime
    • withNano() -This method alters nano-second part/field of the invoking ZonedDateTime
    • withZoneSameInstant() – This method alters time-zone part/field of the invoking ZonedDateTime, retaining the instant
  • Zone used – [America/Argentina/Buenos_Aires] and [America/Chicago]
  • Read Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ? for more details and examples

AlterZonedDateTime.java

package in.bench.resources.java8.zoneddatetime.examples;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class AlterZonedDateTime {

	public static void main(String[] args) {

		// get Zoned System Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time in ISO_ZONED_DATE_TIME format is = "
				+ zonedDateTime);


		// 1. Altering Day/Month/Year parts of ZonedDateTime
		System.out.println("\nAltering Day/Month/Year parts of ZonedDateTime :- \n");


		// 1.1 alter/change/modify DAY part of Zoned System Date/time
		ZonedDateTime dateAltered = zonedDateTime.withDayOfMonth(15);
		System.out.println("1. Day (15) altered in Zoned Date/time is = "
				+ dateAltered);


		// 1.2 alter/change/modify MONTH part of Zoned System Date/time
		ZonedDateTime monthAltered = zonedDateTime.withMonth(9);
		System.out.println("2. Month (9) altered in Zoned Date/time is = "
				+ monthAltered);


		// 1.3 alter/change/modify YEAR part of Zoned System Date/time
		ZonedDateTime yearAltered = zonedDateTime.withYear(2023);
		System.out.println("3. Year (2023) altered in Zoned Date/time is = "
				+ yearAltered);



		// 2. Altering Nano/Second/Minute/Hour of ZonedDateTime
		System.out.println("\nAltering Nano/Second/Minute/Hour parts of ZonedDateTime :- \n");


		// 2.1 alter/change/modify HOUR part to Zoned System Date/time
		ZonedDateTime hourAltered = zonedDateTime.withHour(5);
		System.out.println("1. Hours (5) altered in Zoned Date/time is = " 
				+ hourAltered);


		// 2.2 alter/change/modify MINUTE part to Zoned system Date/time
		ZonedDateTime minuteAltered = zonedDateTime.withMinute(19); 
		System.out.println("2. Minutes (19) altered in Zoned Date/time is = " 
				+ minuteAltered);


		// 2.3 alter/change/modify SECOND part to Zoned system Date/time
		ZonedDateTime secondAltered = zonedDateTime.withSecond(47);
		System.out.println("3. Seconds (47) altered in Zoned Date/time is = " 
				+ secondAltered);


		// 2.4 alter/change/modify NANOSECOND part to Zoned system Date/time
		ZonedDateTime nanoAltered = zonedDateTime.withNano(125);
		System.out.println("4. Nanoseconds (125) altered in Zoned Date/time is = "
				+ nanoAltered);



		// 3. Altering Zone from ZonedDateTime
		System.out.println("\nAltering Zone of ZonedDateTime :- \n");

		ZoneId zoneId = ZoneId.of("Europe/Paris");
		System.out.println("Zone is = " + zoneId);


		// 3.1 alter/change/modify ZONE part to Zoned system Date/time
		ZonedDateTime zoneAltered = zonedDateTime.withZoneSameInstant(zoneId);
		System.out.println("1. Zone (Europe/Paris) altered in Zoned Date/time is = " 
				+ zoneAltered);


		// 3.2 alter/change/modify ZONE part to Zoned system Date/time
		ZonedDateTime zoneAlteredLocal = zonedDateTime.withZoneSameLocal(zoneId);
		System.out.print("2. Zone (Europe/Paris) altered in Zoned Date/time is = " 
				+ zoneAlteredLocal);
	}
}

Output:

Zoned Date/time in ISO_ZONED_DATE_TIME format is = 2022-08-15T13:23:57.008110800+05:30[Asia/Calcutta]

Altering Day/Month/Year parts of ZonedDateTime :- 

1. Day (15) altered in Zoned Date/time is = 2022-08-15T13:23:57.008110800+05:30[Asia/Calcutta]
2. Month (9) altered in Zoned Date/time is = 2022-09-15T13:23:57.008110800+05:30[Asia/Calcutta]
3. Year (2023) altered in Zoned Date/time is = 2023-08-15T13:23:57.008110800+05:30[Asia/Calcutta]

Altering Nano/Second/Minute/Hour parts of ZonedDateTime :- 

1. Hours (5) altered in Zoned Date/time is = 2022-08-15T05:23:57.008110800+05:30[Asia/Calcutta]
2. Minutes (19) altered in Zoned Date/time is = 2022-08-15T13:19:57.008110800+05:30[Asia/Calcutta]
3. Seconds (47) altered in Zoned Date/time is = 2022-08-15T13:23:47.008110800+05:30[Asia/Calcutta]
4. Nanoseconds (125) altered in Zoned Date/time is = 2022-08-15T13:23:57.000000125+05:30[Asia/Calcutta]

Altering Zone of ZonedDateTime :- 

Zone is = Europe/Paris
1. Zone (Europe/Paris) altered in Zoned Date/time is = 2022-08-15T09:53:57.008110800+02:00[Europe/Paris]
2. Zone (Europe/Paris) altered in Zoned Date/time is = 2022-08-15T13:23:57.008110800+02:00[Europe/Paris]

4.11 Check ZonedDateTime is Before/After another ZonedDateTime :

CompareTwoZonedDateTime.java

package in.bench.resources.java8.zoneddatetime.examples;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class CompareTwoZonedDateTime {

	public static void main(String[] args) {

		// 1. get Current Zoned Date/time
		ZoneId zoneIdLocal = ZoneId.systemDefault();
		ZonedDateTime currentZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdLocal);
		System.out.println("1. Current Zoned Date/time is = " + currentZonedDateTime);


		// 2. form Past Date/time
		ZoneId zoneIdPast = ZoneId.of("Australia/Sydney");
		ZonedDateTime pastZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdPast);
		System.out.println("2. Past Zoned Date/time is = " + pastZonedDateTime);


		// 3. form Future Date/time
		ZoneId zoneIdFuture = ZoneId.of("America/Chicago");
		ZonedDateTime futureZonedDateTime = ZonedDateTime.of(2022, 8, 15, 12, 30, 30, 500, zoneIdFuture);
		System.out.println("3. Future Zoned Date/time is = " + futureZonedDateTime);



		// 4. isBefore() - ZonedDateTime comparison
		System.out.println("\n\n4. Comparing 2 ZonedDateTime using isBefore() method :- \n");


		// 4.1 check whether currentZonedDateTime isBefore futureZonedDateTime
		boolean isBefore1 = currentZonedDateTime.isBefore(futureZonedDateTime);
		System.out.println("4.1 Current Zoned Date/time (" + currentZonedDateTime 
				+ ") \n\t\t is Before Future Zoned Date/time (" + futureZonedDateTime + ") ? "
				+ isBefore1);


		// 4.2 check whether currentZonedDateTime isBefore pastZonedDateTime
		boolean isBefore2 = currentZonedDateTime.isBefore(pastZonedDateTime);
		System.out.println("4.2 Current Zoned Date/time (" + currentZonedDateTime 
				+ ") \n\t\t is Before Past Zoned Date/time (" + pastZonedDateTime + ") ? "
				+ isBefore2);


		// 5. isAfter() - ZonedDateTime comparison
		System.out.println("\n\n5. Comparing 2 ZonedDateTime using isAfter() method :- \n");


		// 5.1 check whether currentZonedDateTime isAfter futureZonedDateTime
		boolean isAfter1 = currentZonedDateTime.isAfter(futureZonedDateTime);
		System.out.println("5.1 Current Date/time (" + currentZonedDateTime 
				+ ") \n\t\t is After Future Date/time (" + futureZonedDateTime + ") ? "
				+ isAfter1);


		// 5.2 check whether currentZonedDateTime isAfter pastZonedDateTime
		boolean isAfter2 = currentZonedDateTime.isAfter(pastZonedDateTime);
		System.out.print("5.2 Current Date/time (" + currentZonedDateTime 
				+ ") \n\t\t is After Past Date/time (" + pastZonedDateTime + ") ? "
				+ isAfter2);
	}
}

Output:

1. Current Zoned Date/time is = 2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]
2. Past Zoned Date/time is = 2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]
3. Future Zoned Date/time is = 2022-08-15T12:30:30.000000500-05:00[America/Chicago]


4. Comparing 2 ZonedDateTime using isBefore() method :- 

4.1 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) 
		 is Before Future Zoned Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? true
4.2 Current Zoned Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) 
		 is Before Past Zoned Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? false


5. Comparing 2 ZonedDateTime using isAfter() method :- 

5.1 Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) 
		 is After Future Date/time (2022-08-15T12:30:30.000000500-05:00[America/Chicago]) ? false
5.2 Current Date/time (2022-08-15T12:30:30.000000500+05:30[Asia/Calcutta]) 
		 is After Past Date/time (2022-08-15T12:30:30.000000500+10:00[Australia/Sydney]) ? true

4.12 ZonedDateTime to LocalDateTime/OffsetDateTime/Instant or LocalDate/LocalTime :

ConvertZonedDateTime.java

package in.bench.resources.java8.zoneddatetime.examples;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;

public class ConvertZonedDateTime {

	public static void main(String[] args) {

		// get Zoned Date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is = " + zonedDateTime);


		// 1. convert ZonedDateTime to LocalDateTime using toLocalDateTime() method
		LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
		System.out.println("\nConversion of ZonedDateTime to LocalDateTime is :- \n"
				+ localDateTime);


		// 2. convert ZonedDateTime to OffsetDateTime using toOffsetDateTime() method
		OffsetDateTime offsetDateTime = zonedDateTime.toOffsetDateTime();
		System.out.println("\nConversion of ZonedDateTime to OffsetDateTime is :- \n"
				+ offsetDateTime);


		// 3. convert ZonedDateTime to an Instant using toInstant() method
		Instant instant = zonedDateTime.toInstant();
		System.out.println("\nConversion of ZonedDateTime to Instant is :- \n"
				+ instant);


		// 4. get LocalDate from ZonedDateTime using toLocalDate() method
		LocalDate localDate = zonedDateTime.toLocalDate();
		System.out.println("\nConversion of ZonedDateTime to LocalDate is :- \n"
				+ localDate);


		// 5. get LocalTime from ZonedDateTime using toLocalTime() method
		LocalTime localTime = zonedDateTime.toLocalTime();
		System.out.print("\nConversion of ZonedDateTime to LocalTime is :- \n"
				+ localTime);
	}
}

Output:

Zoned Date/time is = 2022-08-15T13:46:58.700243500+05:30[Asia/Calcutta]

Conversion of ZonedDateTime to LocalDateTime is :- 
2022-08-15T13:46:58.700243500

Conversion of ZonedDateTime to OffsetDateTime is :- 
2022-08-15T13:46:58.700243500+05:30

Conversion of ZonedDateTime to Instant is :- 
2022-08-15T08:16:58.700243500Z

Conversion of ZonedDateTime to LocalDate is :- 
2022-08-15

Conversion of ZonedDateTime to LocalTime is :- 
13:46:58.700243500

Q) How to get all zone information ?

AllZoneInformation.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;
import java.util.Map;

public class AllZoneInformation {

	public static void main(String[] args) {

		// get short IDs for zone
		System.out.println("Java 8 Date/Time API - All zone information :- \n");
		Map<String, String> zoneIdMap = ZoneId.SHORT_IDS;


		// iterate through all zones after sorting
		zoneIdMap
		.entrySet()
		.stream()
		.sorted(Map.Entry.comparingByKey())
		.forEach(entry -> System.out.println(
				"Key : " + entry.getKey() + "\t Value : + " + entry.getValue()
				));
	}
}

Output:

Java 8 Date/Time API - All zone information :- 

Key : ACT	 Value : + Australia/Darwin
Key : AET	 Value : + Australia/Sydney
Key : AGT	 Value : + America/Argentina/Buenos_Aires
Key : ART	 Value : + Africa/Cairo
Key : AST	 Value : + America/Anchorage
Key : BET	 Value : + America/Sao_Paulo
Key : BST	 Value : + Asia/Dhaka
Key : CAT	 Value : + Africa/Harare
Key : CNT	 Value : + America/St_Johns
Key : CST	 Value : + America/Chicago
Key : CTT	 Value : + Asia/Shanghai
Key : EAT	 Value : + Africa/Addis_Ababa
Key : ECT	 Value : + Europe/Paris
Key : EST	 Value : + -05:00
Key : HST	 Value : + -10:00
Key : IET	 Value : + America/Indiana/Indianapolis
Key : IST	 Value : + Asia/Kolkata
Key : JST	 Value : + Asia/Tokyo
Key : MIT	 Value : + Pacific/Apia
Key : MST	 Value : + -07:00
Key : NET	 Value : + Asia/Yerevan
Key : NST	 Value : + Pacific/Auckland
Key : PLT	 Value : + Asia/Karachi
Key : PNT	 Value : + America/Phoenix
Key : PRT	 Value : + America/Puerto_Rico
Key : PST	 Value : + America/Los_Angeles
Key : SST	 Value : + Pacific/Guadalcanal
Key : VST	 Value : + Asia/Ho_Chi_Minh

Q) How to get Default zone information ?

  • ZoneId.systemDefault(); – provides default zone information

DefaultZoneInformation.java

package in.bench.resources.zoneddatetime.sorting;

import java.time.ZoneId;

public class DefaultZoneInformation {

	public static void main(String[] args) {

		// get default system zone info
		ZoneId zoneId = ZoneId.systemDefault();


		// print to console
		System.out.println("Defaul system zone is = " + zoneId);
	}
}

Output:

Defaul system zone is = Asia/Calcutta

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - Display all Zones and its Offset using ZoneId and ZoneOffset
Java 8 – LocalDateTime with method details and examples