Java 8 – OffsetDateTime with method details and examples

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

Prior to introducing LocalDateLocalTimeLocalDateTime, ZonedDateTime and OffsetDateTime 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 handling along with Offset/Zone 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.OffsetDateTime introduced in Java 1.8 version

1. ZonedDateTime v/s OffsetDateTime v/s LocalDateTime:

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

  • LocalDateTime provides date/time without Offset/Zone information in the below format,
    • yyyy-MM-ddTHH:mm:ss.nnn
    • For example, 2022-06-27T19:51:10.951052
  • OffsetDateTime provides date/time with Offset but no Zone information in the below format,
    • yyyy-MM-ddTHH:mm:ss.nnn+HH:mm
    • So, OffsetDateTime is LocalDateTime plus Offset from GMT/UTC information like,
    • For example 2022-06-27T19:51:10.951052+05:30
  • ZonedDateTime provides date/time with Offset from GMT/UTC & ZoneId information in the below format,
    • yyyy-MM-ddTHH:mm:ss.nnn+HH:mm[region/city]
    • So, ZonedDateTime is OffsetDateTime plus ZoneId information in Square-bracket like,
    • For example 2022-06-27T19:51:10.951052+05:30[Asia/Calcutta]
  • In the below illustration, we are printing ZonedDateTime, OffsetDateTime & LocalDateTime twice
    • First in the system default zone (i.e., Asia/Calcutta)
    • Second, after providing ZoneId (Canada/Central) as argument to overloaded now() method
  • ZonedDateTime, OffsetDateTime & LocalDateTime prints current Date/time in their specific Zone
    • LocalDateTime provides date-time to nano-second precision
    • OffsetDateTime provides date-time to nano-second precision and Offset from GMT/UTC information
    • ZonedDateTime provides date-time to nano-second precision and Offset from GMT/UTC & ZoneId information
  • Note: Time-difference from Greenwich Mean Time (GMT) or Universal Time Coordinated (UTC) is known as Offset

ZonedVsOffsetVsLocalDateTime.java

package in.bench.resources.offsetdatetime;

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

public class OffsetVsZonedVsLocalDateTime {

	public static void main(String[] args) {

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


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


		// 1.2 OffsetDateTime - current date/time in default zone
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("\n1.2 OffsetDateTime - " + offsetDateTime);


		// 1.3 ZonedDateTime - current date/time in default zone
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("\n1.3 LocalDateTime  - " + localDateTime);



		// 2. ZonedDateTime, OffsetDateTime and LocalDateTime in Canada/Central Zone 
		ZoneId zoneId = ZoneId.of("Canada/Central");
		System.out.println("\n\n2. ZonedDateTime, OffsetDateTime and LocalDateTime "
				+ "in [Canada/Central] Zone");


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


		// 2.2 current date/time in specific/passed zone
		OffsetDateTime offsetDateTime2 = OffsetDateTime.now(zoneId);
		System.out.println("\n2.2 OffsetDateTime - " + offsetDateTime2);


		// 2.3 ZonedDateTime - current date/time in default zone
		LocalDateTime localDateTime2 = LocalDateTime.now(zoneId);
		System.out.println("\n2.3 LocalDateTime  - " + localDateTime2);
	}
}

Output:

1. ZonedDateTime, OffsetDateTime and LocalDateTime in system default zone - [Asia/Calcutta]

1.1 ZonedDateTime  - 2022-06-28T12:08:05.765823600+05:30[Asia/Calcutta]

1.2 OffsetDateTime - 2022-06-28T12:08:05.765823600+05:30

1.3 LocalDateTime  - 2022-06-28T12:08:05.765823600


2. ZonedDateTime, OffsetDateTime and LocalDateTime in [Canada/Central] Zone

2.1 ZonedDateTime  - 2022-06-28T01:38:05.766799100-05:00[Canada/Central]

2.2 OffsetDateTime - 2022-06-28T01:38:05.777799100-05:00

2.3 LocalDateTime  - 2022-06-28T01:38:05.777799100

2. OffsetDateTime :

  • There are 3 ways to get/form an OffsetDateTime,
    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 an OffsetDateTime using static factory method of() passing year, month, day, hour, minute, second, nano and ZoneOffset information (there are 3 variants)
    3. Third and final is to parse date/time/ZoneOffset in String-form to OffsetDateTime using static factory method parse() such as 13:45.30.123456789+02:00
  • The fully qualified package/class name of OffsetDateTime is java.time.OffsetDateTime i.e.; OffsetDateTime is present under java.time package
  • Class declaration for OffsetDateTime is as follows,
package java.time;

public final class OffsetDateTime
        implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable {
}

3. OffsetDateTime methods or APIs :

  • Important OffsetDateTime 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 OffsetDateTime from a year, month, day, hour, minute, second, nanoseconds and ZoneOffset passed
    • parse() – get an instance of OffsetDateTime from a text string in yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm or yyyy-MM-ddTHH:mm:ss+/-HH:mm or yyyy-MM-ddTHH:mm+/-HH:mm formats
    • getYear() – get the Year field from OffsetDateTime
    • getMonthValue() – get the month-of-year field from 1 to 12 from OffsetDateTime
    • getMonth() – get the month-of-year field using the Month enum from OffsetDateTime
    • getDayOfMonth() – get the day-of-month field from OffsetDateTime
    • getDayOfWeek() – get the day-of-week field, which is an enum DayOfWeek from OffsetDateTime
    • getDayOfYear() – get the day-of-year field from OffsetDateTime
    • getHour() – get the hour-of-day field from OffsetDateTime
    • getMinute() – get the minute-of-hour field from OffsetDateTime
    • getSecond() – get the  second-of-minute field from OffsetDateTime
    • getNano() – get the nano-of-second field from OffsetDateTime
    • getOffset() – get the zone offset, such as ‘+01:00’
    • plusDays() – Returns a copy of this OffsetDateTime with the specified number of days added
    • plusWeeks() – Returns a copy of this OffsetDateTime with the specified number of weeks added
    • plusMonths() – Returns a copy of this OffsetDateTime with the specified number of months added
    • plusYears() – Returns a copy of this OffsetDateTime with the specified number of years added
    • plusNanos() – Returns a copy of this OffsetDateTime with the specified number of nanoseconds added
    • plusSeconds() – Returns a copy of this OffsetDateTime with the specified number of seconds added
    • plusMinutes() – Returns a copy of this OffsetDateTime with the specified number of minutes added
    • plusHours() – Returns a copy of this OffsetDateTime with the specified number of hours added
    • minusDays() – Returns a copy of this OffsetDateTime with the specified number of days subtracted
    • minusWeeks() – Returns a copy of this OffsetDateTime with the specified number of weeks subtracted
    • minusMonths() – Returns a copy of this OffsetDateTime with the specified number of months subtracted
    • minusYears() – Returns a copy of this OffsetDateTime with the specified number of years subtracted
    • minusNanos() – Returns a copy of this OffsetDateTime with the specified number of nanoseconds subtracted
    • minusSeconds() – Returns a copy of this OffsetDateTime with the specified number of seconds subtracted
    • minusMinutes() – Returns a copy of this OffsetDateTime with the specified number of minutes subtracted
    • minusHours() – Returns a copy of this OffsetDateTime with the specified number of hours subtracted
    • format() – Formats invoking date-time with ZoneOffset information using the specified date formatter
    • withDayOfMonth() – Returns a copy of this OffsetDateTime with the day-of-month altered
    • withMonth() – Returns a copy of this OffsetDateTime with the month-of-year altered
    • withYear() – Returns a copy of this OffsetDateTime with the year altered
    • withHour() – Returns a copy of this OffsetDateTime with the hour-of-day altered
    • withMinute() – Returns a copy of this OffsetDateTime with the minute-of-hour altered
    • withSecond() – Returns a copy of this OffsetDateTime with the second-of-minute altered
    • withNano() – Returns a copy of this OffsetDateTime with the nano-of-second altered
    • withOffsetSameInstant() – Returns a copy of this OffsetDateTime with the specified offset ensuring that the result is at the same instant

4. OffsetDateTime examples :

  1. OffsetDateTime.now() – get current date/time with ZoneOffset information from system clock in the default zone
  2. OffsetDateTime.now(ZoneId) – get current date/time with ZoneOffset information from system clock in the specified ZoneId
  3. OffsetDateTime.of() – form OffsetDateTime passing Year, Month, Day, Hour, Minute, Second, Nano and ZoneOffset fields
  4. OffsetDateTime.parse() – parse the date/time and ZoneOffset in String-form to OffsetDateTime
  5. Adding day, week, month and year to OffsetDateTime using plusDays(), plusWeeks(), plusMonths() and plusYears() methods respectively
  6. Adding nano, second, minute and hour to OffsetDateTime using plusNanos(), plusSeconds(), plusMinutes() and plusHours() methods respectively
  7. Subtracting day, week, month and year from OffsetDateTime using minusDays(), minusWeeks(), minusMonths() and minusYears() methods respectively
  8. Subtracting nano, second, minute and hour from OffsetDateTime using minusNanos(), minusSeconds(), minusMinutes() and minusHours() methods respectively
  9. Formatting OffsetDateTime in different formats using DateTimeFormatter class
  10. Altering day, month, year, nano, second, minute, hour and ZoneOffset fields with OffsetDateTime using withDayOfMonth(), withMonth(), withYear(), withNano(), withSecond(), withMinute(), withHour() and withOffsetSameInstant() methods respectively

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

  • OffsetDateTime.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 format
  • We can get year, month, day, hour, minute, second, nano-second and ZoneOffset field/part from OffsetDateTime using different methods mentioned above and then we can form our own format as required like “dd.MM.yyyy HH:mm:ss.nnn +HH:mm”
  • Offset used -> system default +05:30

OffsetDateTimeExampleUsingNowMethod.java

package in.bench.resources.offsetdatetime;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeExampleUsingNowMethod {

	public static void main(String[] args) {

		// get current system date/time with Offset in default Zone
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Current date/time in default zone is = " + offsetDateTime);


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


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


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


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


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


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


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



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


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


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


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


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



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


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

Output:

Current date/time in default zone is = 2022-06-28T12:09:14.002743500+05:30

1. Date part from OffsetDateTime : 

Year is : 2022
Month is : 6
Month in Words is : JUNE
Day is : 28
Day of Week is : TUESDAY
Day of Year is : 179

2. Time part from OffsetDateTime : 

Hour is : 12
Minutes is : 9
Seconds is : 14
Nano Seconds is : 2743500

3. ZoneOffset part from OffsetDateTime : 

Offset  is : +05:30

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

  • OffsetDateTime.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 format
  • We can get year, month, day, hour, minute, second, nano-second and ZoneOffset field/part from OffsetDateTime using different methods mentioned above and then we can form our own format as required like “dd.MM.yyyy HH:mm:ss.nnn -HH:mm”
  • Offset/Zone used -> [America/Los_Angeles]

OffsetDateTimeExampleUsingNowMethod2.java

package in.bench.resources.offsetdatetime;

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

public class OffsetDateTimeExampleUsingNowMethod2 {

	public static void main(String[] args) {

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


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


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


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


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


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


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


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


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



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


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


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


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


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



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


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

Output:

Current date/time in specific zone is = 2022-06-27T23:42:17.381509-07:00

1. Date part from OffsetDateTime : 

Year is : 2022
Month is : 6
Month in Words is : JUNE
Day is : 27
Day of Week is : MONDAY
Day of Year is : 178

2. Time part from OffsetDateTime : 

Hour is : 23
Minutes is : 42
Seconds is : 17
Nano Seconds is : 381509000

3. Zone part from OffsetDateTime : 

Offset  is : -07:00

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

  • Passing year, month, day, hour, minute, second, nano-second and ZoneOffset field/part to OffsetDateTime.of() method returns OffsetDateTime which will be in the yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm format
  • We can extract different fields like year, month, day, hour, minute, second, nano-second and ZoneOffset from OffsetDateTime and then form our own format as required like “dd/MM/yyyy HH:mm:ss.nnn +HH:mm”
  • Offset used -> +04:00

OffsetDateTimeExampleUsingOfMethod.java

package in.bench.resources.offsetdatetime;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeExampleUsingOfMethod {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("+04:00");


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


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


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


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


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


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


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


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



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


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


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


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


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



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


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

Output:

Date/time is = 1950-01-26T11:45:37.987+04:00

1. Date part from OffsetDateTime : 

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 OffsetDateTime : 

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

3. Zone part from OffsetDateTime : 

Offset  is : +04:00

4.4 OffsetDateTime.parse() method – get Date/time/Offset in String-form

  • Sometimes, we need to parse date/time with ZoneOffset passed in String-form to OffsetDateTime, for that we can use OffsetDateTime.parse() method which will return OffsetDateTime in yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm format
  • While parsing Date/Time with ZoneOffset, 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
    • yyyy-MM-ddTHH:mm:ss+/-HH:mm
    • yyyy-MM-ddTHH:mm+/-HH:mm
  • Offset used -> +10:00

OffsetDateTimeExampleUsingParseMethod.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;

public class OffsetDateTimeExampleUsingParseMethod {

	public static void main(String[] args) {

		// 1. OffsetDateTime with all fields y, M, d, H, m, s, n and O
		String dateTimeOffsetInStr1 = "1950-01-26T11:45:37.987654321+10:00";


		// 1.1 convert/parse dateInString to OffsetDateTime
		OffsetDateTime dateTimeOffset1 = OffsetDateTime.parse(dateTimeOffsetInStr1);
		System.out.println("1. Parsed Date/time (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm) "
				+ "in specific Offset is - \n" + dateTimeOffset1);



		// 2. OffsetDateTime with fields y, M, d, H, m, s and O
		String dateTimeOffsetInStr2 = "1950-01-26T11:45:37+10:00";


		// 2.1 convert/parse dateInString to OffsetDateTime
		OffsetDateTime dateTimeZone2 = OffsetDateTime.parse(dateTimeOffsetInStr2);
		System.out.println("\n2. Parsed Date/time (yyyy-MM-ddTHH:mm:ss+/-HH:mm) "
				+ "in specific Offset is - \n" + dateTimeZone2);



		// 3. OffsetDateTime with fields y, M, d, H, m and O
		String dateTimeOffsetInStr3 = "1950-01-26T11:45+10:00";


		// 3.1 convert/parse dateInString to OffsetDateTime
		OffsetDateTime dateTimeZone3 = OffsetDateTime.parse(dateTimeOffsetInStr3);
		System.out.println("\n3. Parsed Date/time (yyyy-MM-ddTHH:mm+/-HH:mm) "
				+ "in specific Offset is - \n" + dateTimeZone3);
	}
}

Output:

1. Parsed Date/time (yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm) in specific Offset is - 
1950-01-26T11:45:37.987654321+10:00

2. Parsed Date/time (yyyy-MM-ddTHH:mm:ss+/-HH:mm) in specific Offset is - 
1950-01-26T11:45:37+10:00

3. Parsed Date/time (yyyy-MM-ddTHH:mm+/-HH:mm) in specific Offset is - 
1950-01-26T11:45+10:00

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

  1. Add 5 Days to OffsetDateTime using plusDays() method
  2. Add 2 Weeks to OffsetDateTime using plusWeeks() method
  3. Add 3 Months to OffsetDateTime using plusMonths() method
  4. Add 1 Year to OffsetDateTime using plusYears() method
  5. Offset used -> +02:00

AddDatePartWithOffsetDateTime.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class AddDatePartWithOffsetDateTime {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("+02:00");


		// 1. get Offset date/time with zoneOffset info passed
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time in specific ZoneOffset is = " 
				+ offsetDateTime);


		// 1.1 add 5 days with Offset date/time
		OffsetDateTime add_5_Days = offsetDateTime.plusDays(5);
		System.out.println("\n1.1 After adding 5 Days to Offset Date/time is = " 
				+ add_5_Days);


		// 1.2 add 2 weeks to Offset date/time
		OffsetDateTime add_2_Weeks = offsetDateTime.plusWeeks(2);
		System.out.println("\n1.2 After adding 2 Weeks to Offset Date/time is = " 
				+ add_2_Weeks);


		// 1.3 add 3 months to Offset date/time
		OffsetDateTime add_3_Months = offsetDateTime.plusMonths(3);
		System.out.println("\n1.3 After adding 3 Months to Offset Date/time is = " 
				+ add_3_Months);


		// 1.4 add 1 year to Offset date/time
		OffsetDateTime add_1_Year = offsetDateTime.plusYears(1);
		System.out.println("\n1.4 After adding 1 Year to Offset Date/time is = " 
				+ add_1_Year);
	}
}

Output:

1. Offset Date/time in specific ZoneOffset is = 2022-06-28T08:46:08.590267500+02:00

1.1 After adding 5 Days to Offset Date/time is = 2022-07-03T08:46:08.590267500+02:00

1.2 After adding 2 Weeks to Offset Date/time is = 2022-07-12T08:46:08.590267500+02:00

1.3 After adding 3 Months to Offset Date/time is = 2022-09-28T08:46:08.590267500+02:00

1.4 After adding 1 Year to Offset Date/time is = 2023-06-28T08:46:08.590267500+02:00

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

  1. Add 125 Nanos to OffsetDateTime using plusNanos() method
  2. Add 37 Seconds to OffsetDateTime using plusSeconds() method
  3. Add 19 Minutes to OffsetDateTime using plusMinutes() method
  4. Add 5 Hours to OffsetDateTime using plusHours() method
  5. Zone used -> -03:30

AddTimePartWithOffsetDateTime.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class AddTimePartWithOffsetDateTime {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("-03:30");


		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time is - " + offsetDateTime);


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


		// 1.2 add 37 Seconds to Offset Date/time
		OffsetDateTime add_37_Seconds = offsetDateTime.plusSeconds(37);
		System.out.println("\n1.2 After adding 37 Seconds to Offset Date/time is - " 
				+ add_37_Seconds);


		// 1.3 add 19 Minutes to Offset Date/time
		OffsetDateTime add_19_Minutes = offsetDateTime.plusMinutes(19);
		System.out.println("\n1.3 After adding 19 Minutes to Offset Date/time is - " 
				+ add_19_Minutes);


		// 1.4 add 5 Hours to Offset Date/time
		OffsetDateTime add_5_Hours = offsetDateTime.plusHours(5);
		System.out.println("\n1.4 After adding 5 Hours to Offset Date/time is - " 
				+ add_5_Hours);
	}
}

Output:

1. Offset Date/time is - 2022-06-28T03:16:54.944295200-03:30

1.1 After adding 125 Nano Seconds to Offset Date/time is - 2022-06-28T03:16:54.944295325-03:30

1.2 After adding 37 Seconds to Offset Date/time is - 2022-06-28T03:17:31.944295200-03:30

1.3 After adding 19 Minutes to Offset Date/time is - 2022-06-28T03:35:54.944295200-03:30

1.4 After adding 5 Hours to Offset Date/time is - 2022-06-28T08:16:54.944295200-03:30

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

  1. Subtract 5 Days from OffsetDateTime using minusDays() method
  2. Subtract 2 Weeks from OffsetDateTime using minusWeeks() method
  3. Subtract 3 Months from OffsetDateTime using minusMonths() method
  4. Subtract 1 Year from OffsetDateTime using minusYears() method
  5. Zone used -> +06:00

SubtractDatePartFromOffsetDateTime.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class SubtractDatePartFromOffsetDateTime {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("+06:00");


		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time is - " + offsetDateTime);


		// 1.1 subtract 5 days from Offset Date/time
		OffsetDateTime subtract_5_Days = offsetDateTime.minusDays(5);
		System.out.println("\n1.1 After subtracting 5 Days from Offset Date/time is - " 
				+ subtract_5_Days);


		// 1.2 subtract 2 weeks from Offset Date/time
		OffsetDateTime subtract_2_Weeks = offsetDateTime.minusWeeks(2);
		System.out.println("\n1.2 After subtracting 2 Weeks from Offset Date/time is - " 
				+ subtract_2_Weeks);


		// 1.3 subtract 3 months from Offset Date/time
		OffsetDateTime subtract_3_Months = offsetDateTime.minusMonths(3);
		System.out.println("\n1.3 After subtracting 3 Months from Offset Date/time is - " 
				+ subtract_3_Months);


		// 1.4 subtract 1 year from Offset Date/time
		OffsetDateTime subtract_1_Year = offsetDateTime.minusYears(1);
		System.out.println("\n1.4 After subtracting 1 Year from Offset Date/time is - " 
				+ subtract_1_Year);
	}
}

Output:

1. Offset Date/time is - 2022-06-28T12:49:01.845312100+06:00

1.1 After subtracting 5 Days from Offset Date/time is - 2022-06-23T12:49:01.845312100+06:00

1.2 After subtracting 2 Weeks from Offset Date/time is - 2022-06-14T12:49:01.845312100+06:00

1.3 After subtracting 3 Months from Offset Date/time is - 2022-03-28T12:49:01.845312100+06:00

1.4 After subtracting 1 Year from Offset Date/time is - 2021-06-28T12:49:01.845312100+06:00

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

  1. Subtract 125 Nanos from OffsetDateTime using minusNanos() method
  2. Subtract 37 Seconds from OffsetDateTime using minusSeconds() method
  3. Subtract 19 Minutes from OffsetDateTime using minusMinutes() method
  4. Subtract 5 Hours from OffsetDateTime using minusHours() method
  5. Zone used -> -07:00

SubtractTimePartFromOffsetDateTime.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class SubtractTimePartFromOffsetDateTime {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("-07:00");


		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time is - " + offsetDateTime);


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


		// 1.2 subtract 37 Seconds from Offset Date/time
		OffsetDateTime sub_37_Seconds = offsetDateTime.minusSeconds(37);
		System.out.println("\n1.2 After subtracting 37 Seconds from Offset Date/time is - \n" 
				+ sub_37_Seconds);


		// 1.3 subtract 19 Minutes from Offset Date/time
		OffsetDateTime sub_19_Minutes = offsetDateTime.minusMinutes(19);
		System.out.println("\n1.3 After subtracting 19 Minutes from Offset Date/time is - \n" 
				+ sub_19_Minutes);


		// 1.4 subtract 5 Hours from Offset Date/time
		OffsetDateTime sub_5_Hours = offsetDateTime.minusHours(5);
		System.out.println("\n1.4 After subtracting 5 Hours from Offset Date/time is - \n" 
				+ sub_5_Hours);
	}
}

Output:

1. Offset Date/time is - 2022-06-27T23:49:35.727037900-07:00

1.1 After subtracting 125 Nano Seconds from Offset Date/time is - 
2022-06-27T23:49:35.727037775-07:00

1.2 After subtracting 37 Seconds from Offset Date/time is - 
2022-06-27T23:48:58.727037900-07:00

1.3 After subtracting 19 Minutes from Offset Date/time is - 
2022-06-27T23:30:35.727037900-07:00

1.4 After subtracting 5 Hours from Offset Date/time is - 
2022-06-27T18:49:35.727037900-07:00

4.9 Formatting OffsetDateTime using DateTimeFormatter:

  • We can convert default ISO_OFFSET_DATE_TIME OffsetDateTime format yyyy-MM-ddTHH:mm:ss.nnn+/-HH:mm to any other formats using OffsetDateTime.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“)
  • Zone used -> -10:30

FormattingOffsetDateTimeUsingFormatMethod.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class FormattingOffsetDateTimeUsingFormatMethod {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("-10:30");


		// 1. get Zoned Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time in ISO_OFFSET_DATE_TIME format is = \n"
				+ offsetDateTime);


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


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


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


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


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


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

Output:

1. Offset Date/time in ISO_OFFSET_DATE_TIME format is = 
2022-06-27T20:30:25.078605400-10:30

1.1 Offset Date/time in dd.MM.yyyy format is = 27.06.2022

1.2 Offset Date/time in dd-MMM-yyyy format is = 27-Jun-2022

1.3 Offset Date/time in dd-MM-yyyy HH:mm format is = 27-06-2022 20:30

1.4 Offset Date/time in dd-MM-yyyy HH:mm:ss format is = 27-06-2022 20:30:25

1.5 Offset Date/time in dd-MM-yyyy HH:mm:ss.nnn format is = 27-06-2022 20:30:25.78605400

1.6 Offset Date/time in dd-MM-yyyy HH:mm:ss.nnn O format is = 
27-06-2022 20:30:25.78605400 GMT-10:30

4.10 Altering Day/Month/Year & Nano/Second/Minute/Hour & ZoneOffset fields with OffsetDateTime:

  • Altering Day, Month, Year, Hour, Minute, Second, Nano-second and ZoneOffset field/part of OffsetDateTime is possible with the help below methods,
    • withDayOfMonth() – This method alters day-of-month part/field of the invoking OffsetDateTime
    • withMonth() – This method alters month-of-year part/field of the invoking OffsetDateTime
    • withYear() – This method alters year part/field of the invoking OffsetDateTime
    • withHour() – This method alters hour part/field of the invoking OffsetDateTime
    • withMinute() – This method alters minute part/field of the invoking OffsetDateTime
    • withSecond() – This method alters second part/field of the invoking OffsetDateTime
    • withNano() -This method alters nano-second part/field of the invoking OffsetDateTime
    • withOffsetSameInstant() – This method alters ZoneOffset part/field of the invoking OffsetDateTime, retaining the instant
  • Offset used -> -08:00 and -04:30

AlterOffsetDateTime.java

package in.bench.resources.offsetdatetime;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class AlterOffsetDateTime {

	public static void main(String[] args) {

		// ZoneOffset
		ZoneOffset zoneOffset = ZoneOffset.of("-08:30");


		// 1. get Offset Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffset);
		System.out.println("1. Offset Date/time in ISO_OFFSET_DATE_TIME format is = \n" 
				+ offsetDateTime);


		// 1.1 alter day field/part in Offset Date/time
		OffsetDateTime dateAltered = offsetDateTime.withDayOfMonth(27);
		System.out.println("\n1.1 Day (27) altered in Offset Date/time is = \n" 
				+ dateAltered);


		// 1.2 alter month field/part in Offset Date/time
		OffsetDateTime monthAltered = offsetDateTime.withMonth(7);
		System.out.println("\n1.2 Month (7) altered in Offset Date/time is = \n" 
				+ monthAltered);


		// 1.3 alter year field/part in Offset Date/time
		OffsetDateTime yearAltered = offsetDateTime.withYear(2023);
		System.out.println("\n1.3 Year (2023) altered in Offset Date/time is = \n" 
				+ yearAltered);


		// 1.4 alter hour field/part in Offset Date/time
		OffsetDateTime hourAltered = offsetDateTime.withHour(5);
		System.out.println("\n1.4 Hour (5) altered in Offset Date/time is = \n" 
				+ hourAltered);


		// 1.5 alter minute field/part in Offset Date/time
		OffsetDateTime minuteAltered = offsetDateTime.withMinute(19);
		System.out.println("\n1.5 Minute (19) altered in Offset Date/time is = \n" 
				+ minuteAltered);


		// 1.6 alter second field/part in Offset Date/time
		OffsetDateTime secondAltered = offsetDateTime.withSecond(47);
		System.out.println("\n1.6 Second (47) altered in Offset Date/time is = \n" 
				+ secondAltered);


		// 1.7 alter nano second field/part in Offset Date/time
		OffsetDateTime nanoAltered = offsetDateTime.withNano(125);
		System.out.println("\n1.7 Nano Second (125) altered in Offset Date/time is = \n" 
				+ nanoAltered);


		// 1.8 alter ZoneOffset field/part in Offset Date/time
		OffsetDateTime zoneOffsetAltered = offsetDateTime.withOffsetSameInstant(ZoneOffset.of("-04:30"));
		System.out.println("\n1.8 ZoneOffset (-04:30) altered in Offset Date/time is = \n" 
				+ zoneOffsetAltered);
	}
}

Output:

1. Offset Date/time in ISO_OFFSET_DATE_TIME format is = 
2022-06-27T22:31:38.671408200-08:30

1.1 Day (27) altered in Offset Date/time is = 
2022-06-27T22:31:38.671408200-08:30

1.2 Month (7) altered in Offset Date/time is = 
2022-07-27T22:31:38.671408200-08:30

1.3 Year (2023) altered in Offset Date/time is = 
2023-06-27T22:31:38.671408200-08:30

1.4 Hour (5) altered in Offset Date/time is = 
2022-06-27T05:31:38.671408200-08:30

1.5 Minute (19) altered in Offset Date/time is = 
2022-06-27T22:19:38.671408200-08:30

1.6 Second (47) altered in Offset Date/time is = 
2022-06-27T22:31:47.671408200-08:30

1.7 Nano Second (125) altered in Offset Date/time is = 
2022-06-27T22:31:38.000000125-08:30

1.8 ZoneOffset (-04:30) altered in Offset Date/time is = 
2022-06-28T02:31:38.671408200-04:30

Q) How to get all ZoneOffset information ?

  • There are no direct methods available in ZoneOffset class for getting all ZoneOffset information
  • Therefore, we have to get all zones using ZoneId.getAvailableZoneIds() method of ZoneId class in the form of Map of String/String
  • Pass these ZoneId to ZonedDateTime.now(zone) method and use getOffset() method to get its corresponding Offset
  • Store Zones and Offset information into HashMap with,
    • Key being ZoneId
    • Value being Offset
  • Sort HashMap and store result in LinkedHashMap which maintains insertion-order
    • First sort according to Values (Offset) in reverse-order
    • And then sort Keys (Zones) in alphabetical order
  • Finally, print LinkedHashMap to console
  • Check for full list of Zones and its corresponding Offset

DisplayAllZoneOffset.java

package in.bench.resources.offsetdatetime;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class DisplayAllZoneOffset {

	// main() method
	public static void main(String[] args) {

		// 1. All Offset with its Zones information
		System.out.println("Reverse-order sorted Offset and "
				+ "its corresponding Alphabetically Sorted Zones  :- \n");


		// 2. get all Zone information using ZoneId
		Set<String> zones = ZoneId.getAvailableZoneIds();


		// 3. get Zone with it Offset in LinkedHashMap
		Map<String, String> linkedHashMap = getZonesAndItsOffsetInMap(zones);


		// 3.1 print to console
		linkedHashMap.forEach((key, value) -> System.out.println(value + "\t" + key));
	}


	/**
	 * This method is used to get Zones and its Offset as Key-Value pair in LinkedHashMap
	 * to maintain insertion-order
	 * 
	 * Offset sorted in reverse-ordering and then Zones are sorted alphabetically
	 * 
	 * @param zones
	 * @return
	 */
	private static Map<String, String> getZonesAndItsOffsetInMap(Set<String> zones) {

		// 1. Create HashMap object to store Zone and Offset
		Map<String, String> hashMap = new HashMap<>();


		// 2. Iterate all Zones and get Offset Id
		for(String zone : zones) {

			// 2.1 form ZoneId
			ZoneId zoneId = ZoneId.of(zone);


			// 2.2 ZonedDateTime with Zones
			ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);


			// 2.3 put Zone and Offset into hashMap
			hashMap.put(zone, zonedDateTime.getOffset().toString().replaceAll("Z", "+00:00"));
		}


		// 3. Create LinkedhashMap for storing reverse sorted values of OffSet
		Map<String, String> linkedHashMap = new LinkedHashMap<>();


		// 3.1 First, Reverse Sort hashMap on the basis of Value (Offset) 
		// and then Alphabetically Sorted Key (Zone)
		hashMap // original source
		.entrySet() // get entry set
		.stream() // get stream
		.sorted(
				Map.Entry.<String, String>comparingByValue().reversed()
				.thenComparing(Map.Entry.comparingByKey())
				)
		.forEachOrdered(c -> linkedHashMap.put(c.getKey(), c.getValue())); // maintains insertion-order


		// 3.2 return linkedHashMap;
		return linkedHashMap;
	}
}

Output:

Reverse-order sorted Offset and its corresponding Alphabetically Sorted Zones  :- 

-12:00	Etc/GMT+12
-11:00	Etc/GMT+11
-11:00	Pacific/Midway
-11:00	Pacific/Niue
-11:00	Pacific/Pago_Pago
-11:00	Pacific/Samoa
-11:00	US/Samoa
-10:00	Etc/GMT+10
-10:00	Pacific/Honolulu
-10:00	Pacific/Johnston
-10:00	Pacific/Rarotonga
-10:00	Pacific/Tahiti
-10:00	SystemV/HST10
-10:00	US/Hawaii
-09:30	Pacific/Marquesas
-09:00	America/Adak
-09:00	America/Atka
-09:00	Etc/GMT+9
-09:00	Pacific/Gambier
-09:00	SystemV/YST9
-09:00	US/Aleutian
-08:00	America/Anchorage
-08:00	America/Juneau
-08:00	America/Metlakatla
-08:00	America/Nome
-08:00	America/Sitka
-08:00	America/Yakutat
-08:00	Etc/GMT+8
-08:00	Pacific/Pitcairn
-08:00	SystemV/PST8
-08:00	SystemV/YST9YDT
-08:00	US/Alaska
-07:00	America/Creston
-07:00	America/Dawson
-07:00	America/Dawson_Creek
-07:00	America/Ensenada
-07:00	America/Fort_Nelson
-07:00	America/Hermosillo
-07:00	America/Los_Angeles
-07:00	America/Phoenix
-07:00	America/Santa_Isabel
-07:00	America/Tijuana
-07:00	America/Vancouver
-07:00	America/Whitehorse
-07:00	Canada/Pacific
-07:00	Canada/Yukon
-07:00	Etc/GMT+7
-07:00	Mexico/BajaNorte
-07:00	PST8PDT
-07:00	SystemV/MST7
-07:00	SystemV/PST8PDT
-07:00	US/Arizona
-07:00	US/Pacific
-06:00	America/Belize
-06:00	America/Boise
-06:00	America/Cambridge_Bay
-06:00	America/Chihuahua
-06:00	America/Costa_Rica
-06:00	America/Denver
-06:00	America/Edmonton
-06:00	America/El_Salvador
-06:00	America/Guatemala
-06:00	America/Inuvik
-06:00	America/Managua
-06:00	America/Mazatlan
-06:00	America/Ojinaga
-06:00	America/Regina
-06:00	America/Shiprock
-06:00	America/Swift_Current
-06:00	America/Tegucigalpa
-06:00	America/Yellowknife
-06:00	Canada/Mountain
-06:00	Canada/Saskatchewan
-06:00	Chile/EasterIsland
-06:00	Etc/GMT+6
-06:00	MST7MDT
-06:00	Mexico/BajaSur
-06:00	Navajo
-06:00	Pacific/Easter
-06:00	Pacific/Galapagos
-06:00	SystemV/CST6
-06:00	SystemV/MST7MDT
-06:00	US/Mountain
-05:00	America/Atikokan
-05:00	America/Bahia_Banderas
-05:00	America/Bogota
-05:00	America/Cancun
-05:00	America/Cayman
-05:00	America/Chicago
-05:00	America/Coral_Harbour
-05:00	America/Eirunepe
-05:00	America/Guayaquil
-05:00	America/Indiana/Knox
-05:00	America/Indiana/Tell_City
-05:00	America/Jamaica
-05:00	America/Knox_IN
-05:00	America/Lima
-05:00	America/Matamoros
-05:00	America/Menominee
-05:00	America/Merida
-05:00	America/Mexico_City
-05:00	America/Monterrey
-05:00	America/North_Dakota/Beulah
-05:00	America/North_Dakota/Center
-05:00	America/North_Dakota/New_Salem
-05:00	America/Panama
-05:00	America/Porto_Acre
-05:00	America/Rainy_River
-05:00	America/Rankin_Inlet
-05:00	America/Resolute
-05:00	America/Rio_Branco
-05:00	America/Winnipeg
-05:00	Brazil/Acre
-05:00	CST6CDT
-05:00	Canada/Central
-05:00	Etc/GMT+5
-05:00	Jamaica
-05:00	Mexico/General
-05:00	SystemV/CST6CDT
-05:00	SystemV/EST5
-05:00	US/Central
-05:00	US/Indiana-Starke
-04:00	America/Anguilla
-04:00	America/Antigua
-04:00	America/Aruba
-04:00	America/Asuncion
-04:00	America/Barbados
-04:00	America/Blanc-Sablon
-04:00	America/Boa_Vista
-04:00	America/Campo_Grande
-04:00	America/Caracas
-04:00	America/Cuiaba
-04:00	America/Curacao
-04:00	America/Detroit
-04:00	America/Dominica
-04:00	America/Fort_Wayne
-04:00	America/Grand_Turk
-04:00	America/Grenada
-04:00	America/Guadeloupe
-04:00	America/Guyana
-04:00	America/Havana
-04:00	America/Indiana/Indianapolis
-04:00	America/Indiana/Marengo
-04:00	America/Indiana/Petersburg
-04:00	America/Indiana/Vevay
-04:00	America/Indiana/Vincennes
-04:00	America/Indiana/Winamac
-04:00	America/Indianapolis
-04:00	America/Iqaluit
-04:00	America/Kentucky/Louisville
-04:00	America/Kentucky/Monticello
-04:00	America/Kralendijk
-04:00	America/La_Paz
-04:00	America/Louisville
-04:00	America/Lower_Princes
-04:00	America/Manaus
-04:00	America/Marigot
-04:00	America/Martinique
-04:00	America/Montreal
-04:00	America/Montserrat
-04:00	America/Nassau
-04:00	America/New_York
-04:00	America/Nipigon
-04:00	America/Pangnirtung
-04:00	America/Port-au-Prince
-04:00	America/Port_of_Spain
-04:00	America/Porto_Velho
-04:00	America/Puerto_Rico
-04:00	America/Santiago
-04:00	America/Santo_Domingo
-04:00	America/St_Barthelemy
-04:00	America/St_Kitts
-04:00	America/St_Lucia
-04:00	America/St_Thomas
-04:00	America/St_Vincent
-04:00	America/Thunder_Bay
-04:00	America/Toronto
-04:00	America/Tortola
-04:00	America/Virgin
-04:00	Brazil/West
-04:00	Canada/Eastern
-04:00	Chile/Continental
-04:00	Cuba
-04:00	EST5EDT
-04:00	Etc/GMT+4
-04:00	SystemV/AST4
-04:00	SystemV/EST5EDT
-04:00	US/East-Indiana
-04:00	US/Eastern
-04:00	US/Michigan
-03:00	America/Araguaina
-03:00	America/Argentina/Buenos_Aires
-03:00	America/Argentina/Catamarca
-03:00	America/Argentina/ComodRivadavia
-03:00	America/Argentina/Cordoba
-03:00	America/Argentina/Jujuy
-03:00	America/Argentina/La_Rioja
-03:00	America/Argentina/Mendoza
-03:00	America/Argentina/Rio_Gallegos
-03:00	America/Argentina/Salta
-03:00	America/Argentina/San_Juan
-03:00	America/Argentina/San_Luis
-03:00	America/Argentina/Tucuman
-03:00	America/Argentina/Ushuaia
-03:00	America/Bahia
-03:00	America/Belem
-03:00	America/Buenos_Aires
-03:00	America/Catamarca
-03:00	America/Cayenne
-03:00	America/Cordoba
-03:00	America/Fortaleza
-03:00	America/Glace_Bay
-03:00	America/Goose_Bay
-03:00	America/Halifax
-03:00	America/Jujuy
-03:00	America/Maceio
-03:00	America/Mendoza
-03:00	America/Moncton
-03:00	America/Montevideo
-03:00	America/Paramaribo
-03:00	America/Punta_Arenas
-03:00	America/Recife
-03:00	America/Rosario
-03:00	America/Santarem
-03:00	America/Sao_Paulo
-03:00	America/Thule
-03:00	Antarctica/Palmer
-03:00	Antarctica/Rothera
-03:00	Atlantic/Bermuda
-03:00	Atlantic/Stanley
-03:00	Brazil/East
-03:00	Canada/Atlantic
-03:00	Etc/GMT+3
-03:00	SystemV/AST4ADT
-02:30	America/St_Johns
-02:30	Canada/Newfoundland
-02:00	America/Godthab
-02:00	America/Miquelon
-02:00	America/Noronha
-02:00	America/Nuuk
-02:00	Atlantic/South_Georgia
-02:00	Brazil/DeNoronha
-02:00	Etc/GMT+2
-01:00	Atlantic/Cape_Verde
-01:00	Etc/GMT+1
+14:00	Etc/GMT-14
+14:00	Pacific/Kiritimati
+13:00	Etc/GMT-13
+13:00	Pacific/Apia
+13:00	Pacific/Enderbury
+13:00	Pacific/Fakaofo
+13:00	Pacific/Kanton
+13:00	Pacific/Tongatapu
+12:45	NZ-CHAT
+12:45	Pacific/Chatham
+12:00	Antarctica/McMurdo
+12:00	Antarctica/South_Pole
+12:00	Asia/Anadyr
+12:00	Asia/Kamchatka
+12:00	Etc/GMT-12
+12:00	Kwajalein
+12:00	NZ
+12:00	Pacific/Auckland
+12:00	Pacific/Fiji
+12:00	Pacific/Funafuti
+12:00	Pacific/Kwajalein
+12:00	Pacific/Majuro
+12:00	Pacific/Nauru
+12:00	Pacific/Tarawa
+12:00	Pacific/Wake
+12:00	Pacific/Wallis
+11:00	Antarctica/Casey
+11:00	Asia/Magadan
+11:00	Asia/Sakhalin
+11:00	Asia/Srednekolymsk
+11:00	Etc/GMT-11
+11:00	Pacific/Bougainville
+11:00	Pacific/Efate
+11:00	Pacific/Guadalcanal
+11:00	Pacific/Kosrae
+11:00	Pacific/Norfolk
+11:00	Pacific/Noumea
+11:00	Pacific/Pohnpei
+11:00	Pacific/Ponape
+10:30	Australia/LHI
+10:30	Australia/Lord_Howe
+10:00	Antarctica/DumontDUrville
+10:00	Antarctica/Macquarie
+10:00	Asia/Ust-Nera
+10:00	Asia/Vladivostok
+10:00	Australia/ACT
+10:00	Australia/Brisbane
+10:00	Australia/Canberra
+10:00	Australia/Currie
+10:00	Australia/Hobart
+10:00	Australia/Lindeman
+10:00	Australia/Melbourne
+10:00	Australia/NSW
+10:00	Australia/Queensland
+10:00	Australia/Sydney
+10:00	Australia/Tasmania
+10:00	Australia/Victoria
+10:00	Etc/GMT-10
+10:00	Pacific/Chuuk
+10:00	Pacific/Guam
+10:00	Pacific/Port_Moresby
+10:00	Pacific/Saipan
+10:00	Pacific/Truk
+10:00	Pacific/Yap
+09:30	Australia/Adelaide
+09:30	Australia/Broken_Hill
+09:30	Australia/Darwin
+09:30	Australia/North
+09:30	Australia/South
+09:30	Australia/Yancowinna
+09:00	Asia/Chita
+09:00	Asia/Dili
+09:00	Asia/Jayapura
+09:00	Asia/Khandyga
+09:00	Asia/Pyongyang
+09:00	Asia/Seoul
+09:00	Asia/Tokyo
+09:00	Asia/Yakutsk
+09:00	Etc/GMT-9
+09:00	Japan
+09:00	Pacific/Palau
+09:00	ROK
+08:45	Australia/Eucla
+08:00	Asia/Brunei
+08:00	Asia/Choibalsan
+08:00	Asia/Chongqing
+08:00	Asia/Chungking
+08:00	Asia/Harbin
+08:00	Asia/Hong_Kong
+08:00	Asia/Irkutsk
+08:00	Asia/Kuala_Lumpur
+08:00	Asia/Kuching
+08:00	Asia/Macao
+08:00	Asia/Macau
+08:00	Asia/Makassar
+08:00	Asia/Manila
+08:00	Asia/Shanghai
+08:00	Asia/Singapore
+08:00	Asia/Taipei
+08:00	Asia/Ujung_Pandang
+08:00	Asia/Ulaanbaatar
+08:00	Asia/Ulan_Bator
+08:00	Australia/Perth
+08:00	Australia/West
+08:00	Etc/GMT-8
+08:00	Hongkong
+08:00	PRC
+08:00	Singapore
+07:00	Antarctica/Davis
+07:00	Asia/Bangkok
+07:00	Asia/Barnaul
+07:00	Asia/Ho_Chi_Minh
+07:00	Asia/Hovd
+07:00	Asia/Jakarta
+07:00	Asia/Krasnoyarsk
+07:00	Asia/Novokuznetsk
+07:00	Asia/Novosibirsk
+07:00	Asia/Phnom_Penh
+07:00	Asia/Pontianak
+07:00	Asia/Saigon
+07:00	Asia/Tomsk
+07:00	Asia/Vientiane
+07:00	Etc/GMT-7
+07:00	Indian/Christmas
+06:30	Asia/Rangoon
+06:30	Asia/Yangon
+06:30	Indian/Cocos
+06:00	Antarctica/Vostok
+06:00	Asia/Almaty
+06:00	Asia/Bishkek
+06:00	Asia/Dacca
+06:00	Asia/Dhaka
+06:00	Asia/Kashgar
+06:00	Asia/Omsk
+06:00	Asia/Qostanay
+06:00	Asia/Thimbu
+06:00	Asia/Thimphu
+06:00	Asia/Urumqi
+06:00	Etc/GMT-6
+06:00	Indian/Chagos
+05:45	Asia/Kathmandu
+05:45	Asia/Katmandu
+05:30	Asia/Calcutta
+05:30	Asia/Colombo
+05:30	Asia/Kolkata
+05:00	Antarctica/Mawson
+05:00	Asia/Aqtau
+05:00	Asia/Aqtobe
+05:00	Asia/Ashgabat
+05:00	Asia/Ashkhabad
+05:00	Asia/Atyrau
+05:00	Asia/Dushanbe
+05:00	Asia/Karachi
+05:00	Asia/Oral
+05:00	Asia/Qyzylorda
+05:00	Asia/Samarkand
+05:00	Asia/Tashkent
+05:00	Asia/Yekaterinburg
+05:00	Etc/GMT-5
+05:00	Indian/Kerguelen
+05:00	Indian/Maldives
+04:30	Asia/Kabul
+04:30	Asia/Tehran
+04:30	Iran
+04:00	Asia/Baku
+04:00	Asia/Dubai
+04:00	Asia/Muscat
+04:00	Asia/Tbilisi
+04:00	Asia/Yerevan
+04:00	Etc/GMT-4
+04:00	Europe/Astrakhan
+04:00	Europe/Samara
+04:00	Europe/Saratov
+04:00	Europe/Ulyanovsk
+04:00	Indian/Mahe
+04:00	Indian/Mauritius
+04:00	Indian/Reunion
+03:00	Africa/Addis_Ababa
+03:00	Africa/Asmara
+03:00	Africa/Asmera
+03:00	Africa/Dar_es_Salaam
+03:00	Africa/Djibouti
+03:00	Africa/Kampala
+03:00	Africa/Mogadishu
+03:00	Africa/Nairobi
+03:00	Antarctica/Syowa
+03:00	Asia/Aden
+03:00	Asia/Amman
+03:00	Asia/Baghdad
+03:00	Asia/Bahrain
+03:00	Asia/Beirut
+03:00	Asia/Damascus
+03:00	Asia/Famagusta
+03:00	Asia/Gaza
+03:00	Asia/Hebron
+03:00	Asia/Istanbul
+03:00	Asia/Jerusalem
+03:00	Asia/Kuwait
+03:00	Asia/Nicosia
+03:00	Asia/Qatar
+03:00	Asia/Riyadh
+03:00	Asia/Tel_Aviv
+03:00	EET
+03:00	Etc/GMT-3
+03:00	Europe/Athens
+03:00	Europe/Bucharest
+03:00	Europe/Chisinau
+03:00	Europe/Helsinki
+03:00	Europe/Istanbul
+03:00	Europe/Kiev
+03:00	Europe/Kirov
+03:00	Europe/Mariehamn
+03:00	Europe/Minsk
+03:00	Europe/Moscow
+03:00	Europe/Nicosia
+03:00	Europe/Riga
+03:00	Europe/Simferopol
+03:00	Europe/Sofia
+03:00	Europe/Tallinn
+03:00	Europe/Tiraspol
+03:00	Europe/Uzhgorod
+03:00	Europe/Vilnius
+03:00	Europe/Volgograd
+03:00	Europe/Zaporozhye
+03:00	Indian/Antananarivo
+03:00	Indian/Comoro
+03:00	Indian/Mayotte
+03:00	Israel
+03:00	Turkey
+03:00	W-SU
+02:00	Africa/Blantyre
+02:00	Africa/Bujumbura
+02:00	Africa/Cairo
+02:00	Africa/Ceuta
+02:00	Africa/Gaborone
+02:00	Africa/Harare
+02:00	Africa/Johannesburg
+02:00	Africa/Juba
+02:00	Africa/Khartoum
+02:00	Africa/Kigali
+02:00	Africa/Lubumbashi
+02:00	Africa/Lusaka
+02:00	Africa/Maputo
+02:00	Africa/Maseru
+02:00	Africa/Mbabane
+02:00	Africa/Tripoli
+02:00	Africa/Windhoek
+02:00	Antarctica/Troll
+02:00	Arctic/Longyearbyen
+02:00	Atlantic/Jan_Mayen
+02:00	CET
+02:00	Egypt
+02:00	Etc/GMT-2
+02:00	Europe/Amsterdam
+02:00	Europe/Andorra
+02:00	Europe/Belgrade
+02:00	Europe/Berlin
+02:00	Europe/Bratislava
+02:00	Europe/Brussels
+02:00	Europe/Budapest
+02:00	Europe/Busingen
+02:00	Europe/Copenhagen
+02:00	Europe/Gibraltar
+02:00	Europe/Kaliningrad
+02:00	Europe/Ljubljana
+02:00	Europe/Luxembourg
+02:00	Europe/Madrid
+02:00	Europe/Malta
+02:00	Europe/Monaco
+02:00	Europe/Oslo
+02:00	Europe/Paris
+02:00	Europe/Podgorica
+02:00	Europe/Prague
+02:00	Europe/Rome
+02:00	Europe/San_Marino
+02:00	Europe/Sarajevo
+02:00	Europe/Skopje
+02:00	Europe/Stockholm
+02:00	Europe/Tirane
+02:00	Europe/Vaduz
+02:00	Europe/Vatican
+02:00	Europe/Vienna
+02:00	Europe/Warsaw
+02:00	Europe/Zagreb
+02:00	Europe/Zurich
+02:00	Libya
+02:00	MET
+02:00	Poland
+01:00	Africa/Algiers
+01:00	Africa/Bangui
+01:00	Africa/Brazzaville
+01:00	Africa/Casablanca
+01:00	Africa/Douala
+01:00	Africa/El_Aaiun
+01:00	Africa/Kinshasa
+01:00	Africa/Lagos
+01:00	Africa/Libreville
+01:00	Africa/Luanda
+01:00	Africa/Malabo
+01:00	Africa/Ndjamena
+01:00	Africa/Niamey
+01:00	Africa/Porto-Novo
+01:00	Africa/Tunis
+01:00	Atlantic/Canary
+01:00	Atlantic/Faeroe
+01:00	Atlantic/Faroe
+01:00	Atlantic/Madeira
+01:00	Eire
+01:00	Etc/GMT-1
+01:00	Europe/Belfast
+01:00	Europe/Dublin
+01:00	Europe/Guernsey
+01:00	Europe/Isle_of_Man
+01:00	Europe/Jersey
+01:00	Europe/Lisbon
+01:00	Europe/London
+01:00	GB
+01:00	GB-Eire
+01:00	Portugal
+01:00	WET
+00:00	Africa/Abidjan
+00:00	Africa/Accra
+00:00	Africa/Bamako
+00:00	Africa/Banjul
+00:00	Africa/Bissau
+00:00	Africa/Conakry
+00:00	Africa/Dakar
+00:00	Africa/Freetown
+00:00	Africa/Lome
+00:00	Africa/Monrovia
+00:00	Africa/Nouakchott
+00:00	Africa/Ouagadougou
+00:00	Africa/Sao_Tome
+00:00	Africa/Timbuktu
+00:00	America/Danmarkshavn
+00:00	America/Scoresbysund
+00:00	Atlantic/Azores
+00:00	Atlantic/Reykjavik
+00:00	Atlantic/St_Helena
+00:00	Etc/GMT
+00:00	Etc/GMT+0
+00:00	Etc/GMT-0
+00:00	Etc/GMT0
+00:00	Etc/Greenwich
+00:00	Etc/UCT
+00:00	Etc/UTC
+00:00	Etc/Universal
+00:00	Etc/Zulu
+00:00	GMT
+00:00	GMT0
+00:00	Greenwich
+00:00	Iceland
+00:00	UCT
+00:00	UTC
+00:00	Universal
+00:00	Zulu

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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