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/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

OffsetVsZonedVsLocalDateTime.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("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. ZonedDateTime  - " + zonedDateTime);


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


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



		// 2. ZonedDateTime, OffsetDateTime and LocalDateTime in Canada/Central Zone 
		ZoneId zoneId = ZoneId.of("Canada/Central");
		System.out.println("\n\nZonedDateTime, OffsetDateTime and LocalDateTime "
				+ "in [Canada/Central] 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
		OffsetDateTime offsetDateTime2 = OffsetDateTime.now(zoneId);
		System.out.println("2. OffsetDateTime - " + offsetDateTime2);


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

Output:

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

1. ZonedDateTime  - 2022-08-17T20:35:35.643178300+05:30[Asia/Calcutta]
2. OffsetDateTime - 2022-08-17T20:35:35.644165600+05:30
3. LocalDateTime  - 2022-08-17T20:35:35.644165600


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

1. ZonedDateTime  - 2022-08-17T10:05:35.645087400-05:00[Canada/Central]
2. OffsetDateTime - 2022-08-17T10:05:35.655087300-05:00
3. LocalDateTime  - 2022-08-17T10:05:35.655087300

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
  • withOffsetSameLocal() – Returns a copy of this OffsetDateTime with the specified offset ensuring that the result has the same local date-time
  • isBefore(OffsetDateTime) – Checks if the invoking OffsetDateTime is before that of the specified OffsetDateTime
  • isAfter(OffsetDateTime) – Checks if the invoking OffsetDateTime is after that of the specified OffsetDateTime
  • toLocalDateTime() – gets the LocalDateTime part of invoking OffsetDateTime
  • toLocalDate() – gets the LocalDate part of invoking OffsetDateTime
  • toLocalTime() – gets the LocalTime part of invoking OffsetDateTime
  • toZonedDateTime() – converts invoking OffsetDateTime to a ZonedDateTime using the offset as the zone ID
  • toInstant()- converts invoking OffsetDateTime to an Instant
  • toOffsetTime() – converts invoking OffsetDateTime to an OffsetTime

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
  11. Check OffsetDateTime is Before/After another OffsetDateTime using below methods,
    • isBefore(OffsetDateTime) – checks if the invoking OffsetDateTime is before the specified OffsetDateTime
    • isAfter(OffsetDateTime) – Checks if invoking OffsetDateTime is after the specified OffsetDateTime
  12. Convert OffsetDateTime to LocalDateTime/ZonedDateTime or Instant/LocalDate or LocalTime/OffsetTime

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
  • Read Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ? for more details and examples

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]
  • Read Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ? for more details and examples

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

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
  • Read below articles for more details and examples,
  • 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
  • Read below articles for more details and examples,
    1. Java 8 – How to parse OffsetDateTime in String form ?
    2. Java 8 – How to convert String to OffsetDateTime ?

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
    • Offset used -> +02:00
  5. Read Java 8 – How to add Date and Time fields to OffsetDateTime ? for more details and examples

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("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. 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("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("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.print("4. After adding 1 Year to Offset Date/time is = " 
				+ add_1_Year);
	}
}

Output:

Offset Date/time in specific ZoneOffset is = 2022-08-17T17:16:31.663416800+02:00

1. After adding 5 Days to Offset Date/time is = 2022-08-22T17:16:31.663416800+02:00
2. After adding 2 Weeks to Offset Date/time is = 2022-08-31T17:16:31.663416800+02:00
3. After adding 3 Months to Offset Date/time is = 2022-11-17T17:16:31.663416800+02:00
4. After adding 1 Year to Offset Date/time is = 2023-08-17T17:16:31.663416800+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
    • Zone used -> -03:30
  5. Read Java 8 – How to add Date and Time fields to OffsetDateTime ? for more details and examples

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("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. 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("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("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.print("4. After adding 5 Hours to Offset Date/time is - " 
				+ add_5_Hours);
	}
}

Output:

Offset Date/time is - 2022-08-17T11:47:10.655658200-03:30

1. After adding 125 Nano Seconds to Offset Date/time is - 2022-08-17T11:47:10.655658325-03:30
2. After adding 37 Seconds to Offset Date/time is - 2022-08-17T11:47:47.655658200-03:30
3. After adding 19 Minutes to Offset Date/time is - 2022-08-17T12:06:10.655658200-03:30
4. After adding 5 Hours to Offset Date/time is - 2022-08-17T16:47:10.655658200-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
    • Zone used -> +06:00
  5. Read Java 8 – How to subtract Date and Time fields from OffsetDateTime ? for more details and examples

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("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. 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("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("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.print("4. After subtracting 1 Year from Offset Date/time is - " 
				+ subtract_1_Year);
	}
}

Output:

Offset Date/time is - 2022-08-17T21:18:50.562954200+06:00

1. After subtracting 5 Days from Offset Date/time is - 2022-08-12T21:18:50.562954200+06:00
2. After subtracting 2 Weeks from Offset Date/time is - 2022-08-03T21:18:50.562954200+06:00
3. After subtracting 3 Months from Offset Date/time is - 2022-05-17T21:18:50.562954200+06:00
4. After subtracting 1 Year from Offset Date/time is - 2021-08-17T21:18:50.562954200+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
    • Zone used -> -07:00
  5. Read Java 8 – How to subtract Date and Time fields from OffsetDateTime ? for more details and examples

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("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. 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("\n2. 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("\n3. 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.print("\n4. After subtracting 5 Hours from Offset Date/time is - \n" 
				+ sub_5_Hours);
	}
}

Output:

Offset Date/time is - 2022-08-17T08:20:05.962815100-07:00

1. After subtracting 125 Nano Seconds from Offset Date/time is - 
2022-08-17T08:20:05.962814975-07:00

2. After subtracting 37 Seconds from Offset Date/time is - 
2022-08-17T08:19:28.962815100-07:00

3. After subtracting 19 Minutes from Offset Date/time is - 
2022-08-17T08:01:05.962815100-07:00

4. After subtracting 5 Hours from Offset Date/time is - 
2022-08-17T03:20:05.962815100-07:00

4.9 Formatting OffsetDateTime using DateTimeFormatter:

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
    • withOffsetSameLocal() – This method alters ZoneOffset part/field of the invoking OffsetDateTime, retaining the local date-time
  • Offset used -> -08:00 and -04:30
  • Read Java 8 – How to alter Date, Time and Offset fields of OffsetDateTime ? for more details and examples

AlterOffsetDateTime.java

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

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

public class AlterOffsetDateTime {

	public static void main(String[] args) {

		// get Offset System Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Offset Date/time in ISO_Offset_DATE_TIME format is = "
				+ offsetDateTime);


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


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


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


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



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


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


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


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


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



		// 3. Altering Offset of OffsetDateTime
		System.out.println("\nAltering Offset of OffsetDateTime :- \n");

		ZoneOffset zoneOffset = ZoneOffset.of("+02:00");
		System.out.println("ZoneOffset is = " + zoneOffset);


		// 3.1 alter/change/modify ZONE part to Offset system Date/time
		OffsetDateTime offsetAltered = offsetDateTime.withOffsetSameInstant(zoneOffset);
		System.out.println("1. Offset (+02:00) altered in Offset Date/time is = " 
				+ offsetAltered);


		// 3.2 alter/change/modify ZONE part to Offset system Date/time
		OffsetDateTime offsetAlteredLocal = offsetDateTime.withOffsetSameLocal(zoneOffset);
		System.out.print("2. Offset (+02:00) altered in Offset Date/time is = " 
				+ offsetAlteredLocal);
	}
}

Output:

Offset Date/time in ISO_Offset_DATE_TIME format is = 2022-08-17T22:01:48.047120400+05:30

Altering Day/Month/Year parts of OffsetDateTime :- 

1. Day (15) altered in Offset Date/time is = 2022-08-15T22:01:48.047120400+05:30
2. Month (9) altered in Offset Date/time is = 2022-09-17T22:01:48.047120400+05:30
3. Year (2023) altered in Offset Date/time is = 2023-08-17T22:01:48.047120400+05:30

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

1. Hours (5) altered in Offset Date/time is = 2022-08-17T05:01:48.047120400+05:30
2. Minutes (19) altered in Offset Date/time is = 2022-08-17T22:19:48.047120400+05:30
3. Seconds (47) altered in Offset Date/time is = 2022-08-17T22:01:47.047120400+05:30
4. Nanoseconds (125) altered in Offset Date/time is = 2022-08-17T22:01:48.000000125+05:30

Altering Offset of OffsetDateTime :- 

ZoneOffset is = +02:00
1. Offset (+02:00) altered in Offset Date/time is = 2022-08-17T18:31:48.047120400+02:00
2. Offset (+02:00) altered in Offset Date/time is = 2022-08-17T22:01:48.047120400+02:00

4.11 Check OffsetDateTime is Before/After another OffsetDateTime :

CompareTwoOffsetDateTime.java

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

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

public class CompareTwoOffsetDateTime {

	public static void main(String[] args) {

		// 1. get Local Offset Date/time
		ZoneOffset zoneOffsetLocal = ZoneOffset.of("+05:30");
		OffsetDateTime currentOffsetDateTime = OffsetDateTime
				.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetLocal);
		System.out.println("1. Current Offset Date/time is = " + currentOffsetDateTime);


		// 2. form Past Offset Date/time
		ZoneOffset zoneOffsetPast = ZoneOffset.of("+10:30");
		OffsetDateTime pastOffsetDateTime = OffsetDateTime
				.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetPast);
		System.out.println("2. Past Offset Date/time is = " + pastOffsetDateTime);


		// 3. form Future Offset Date/time
		ZoneOffset zoneOffsetFuture = ZoneOffset.of("-09:30");
		OffsetDateTime futureOffsetDateTime = OffsetDateTime
				.of(2022, 8, 15, 12, 30, 30, 500, zoneOffsetFuture);
		System.out.println("3. Future Offset Date/time is = " + futureOffsetDateTime);



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


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


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


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


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


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

Output:

1. Current Offset Date/time is = 2022-08-15T12:30:30.000000500+05:30
2. Past Offset Date/time is = 2022-08-15T12:30:30.000000500+10:30
3. Future Offset Date/time is = 2022-08-15T12:30:30.000000500-09:30


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

4.1 Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30) 
		 is Before Future Offset Date/time (2022-08-15T12:30:30.000000500-09:30) ? true
4.2 Current Offset Date/time (2022-08-15T12:30:30.000000500+05:30) 
		 is Before Past Offset Date/time (2022-08-15T12:30:30.000000500+10:30) ? false


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

5.1 Current Date/time (2022-08-15T12:30:30.000000500+05:30) 
		 is After Future Date/time (2022-08-15T12:30:30.000000500-09:30) ? false
5.2 Current Date/time (2022-08-15T12:30:30.000000500+05:30) 
		 is After Past Date/time (2022-08-15T12:30:30.000000500+10:30) ? true

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

Below methods helps to convert OffsetDateTime to different classes as listed,

ConvertOffsetDateTime.java

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

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

public class ConvertOffsetDateTime {

	public static void main(String[] args) {

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


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


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


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


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


		// 5. get LocalTime from OffsetDateTime using toLocalTime() method
		LocalTime localTime = offsetDateTime.toLocalTime();
		System.out.println("\nConversion of OffsetDateTime to LocalTime is :- \n"
				+ localTime);


		// 6. get OffsetTime from OffsetDateTime using toOffsetTime() method
		OffsetTime offsetTime = offsetDateTime.toOffsetTime();
		System.out.print("\nConversion of OffsetDateTime to LocalTime is :- \n"
				+ offsetTime);
	}
}

Output:

Offset Date/time is = 2022-08-17T21:56:49.651423500+05:30

Conversion of OffsetDateTime to LocalDateTime is :- 
2022-08-17T21:56:49.651423500

Conversion of OffsetDateTime to ZonedDateTime is :- 
2022-08-17T21:56:49.651423500+05:30

Conversion of OffsetDateTime to an Instant is :- 
2022-08-17T16:26:49.651423500Z

Conversion of OffsetDateTime to LocalDate is :- 
2022-08-17

Conversion of OffsetDateTime to LocalTime is :- 
21:56:49.651423500

Conversion of OffsetDateTime to LocalTime is :- 
21:56:49.651423500+05: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 of ZonedDateTime 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
  • Read full list of Zones and its corresponding Offset for details and examples for both Key-sorted & Value-sorted

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