Java 8 - OffsetDateTime example




The java.time.OffsetDateTime class is an immutable class which represents a date-time with an offset such as '2017-07-03T21:44:49.468+05:30'.

The following examples are provided to demonstrate the usage of the OffsetDateTime in java program.

 

How to create an OffsetDateTime object in Java

You can use the OffsetDateTime.now() and OffsetDateTime.of() methods to create the current and specific date-time object with an offset as follows.

package com.boraji.tutorial.datetime;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      // Current date time with an offset
      OffsetDateTime dateTime = OffsetDateTime.now();
      System.out.println(dateTime);

      // Current date time from specified clock with an offset
      OffsetDateTime dateTime2 = OffsetDateTime.now(Clock.systemDefaultZone());
      System.out.println(dateTime2);

      // Current date time from specified time-zone with an offset
      OffsetDateTime dateTime3 = OffsetDateTime.now(ZoneId.systemDefault());
      System.out.println(dateTime3);

      // Specific date time from LocalDateTime with an offset
      OffsetDateTime dateTime4 = OffsetDateTime.of(LocalDateTime.of(2017, 05, 12, 05, 45),
            ZoneOffset.ofHoursMinutes(6, 30));
      System.out.println(dateTime4);
   }
}

Output

2017-07-03T22:02:56.525+05:30
2017-07-03T22:02:56.525+05:30
2017-07-03T22:02:56.525+05:30
2017-05-12T05:45+06:30

 

How to get LocalDateTime, LocalDate, LocalTime, ZonedDateTime, Instant and OffsetTime from OffsetDateTime

You can use the toXXXX() methods to get LocalDateTime, LocalDate, LocalTime, ZonedDateTime, Instant and OffsetTime from OffsetDateTime.

package com.boraji.tutorial.datetime;

import java.time.Clock;
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.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      // Current date time with an offset
      OffsetDateTime dateTime = OffsetDateTime.now();
      System.out.println(dateTime);

      // To LocalDateTime
      LocalDateTime localDateTime = dateTime.toLocalDateTime();
      System.out.println(localDateTime);

      // To LocalDate
      LocalDate localDate = dateTime.toLocalDate();
      System.out.println(localDate);

      // To LocalTime
      LocalTime localTime = dateTime.toLocalTime();
      System.out.println(localTime);

      // To ZonedDateTime
      ZonedDateTime zonedDateTime = dateTime.toZonedDateTime();
      System.out.println(zonedDateTime);

      // To Instant
      Instant instant=dateTime.toInstant();
      System.out.println(instant);
      
      // To OffsetTime
      OffsetTime offsetTime=dateTime.toOffsetTime();
      System.out.println(offsetTime);
   }
}

Output

2017-07-03T22:35:29.771+05:30
2017-07-03T22:35:29.771
2017-07-03
22:35:29.771
2017-07-03T22:35:29.771+05:30
2017-07-03T17:05:29.771Z
22:35:29.771+05:30

 

How to get year, month, day from OffsetDateTime

You can use the getYear()getMonth()getDayOfMonth() and getOffset() methods of the OffsetDateTime class to get year, month, day etc.

package com.boraji.tutorial.datetime;

import java.time.OffsetDateTime;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      OffsetDateTime dateTime = OffsetDateTime.now();
      
      System.out.println("Year : " + dateTime.getYear());
      System.out.println("Month : " + dateTime.getMonth().getValue());
      System.out.println("Day of Month : " + dateTime.getDayOfMonth());
      System.out.println("Day of Week : " + dateTime.getDayOfWeek());
      System.out.println("Day of Year : " + dateTime.getDayOfYear());
      System.out.println("Offset : " + dateTime.getOffset());
   }
}

Output

Year : 2017
Month : 7
Day of Month : 6
Day of Week : THURSDAY
Day of Year : 187
Offset : +05:30

 

How to get Hour, Minute, Second from OffsetDateTime

You can use the getHour()getMinute() and getSecond() methods of the OffsetDateTime class to get hour, minute and second respectively.

package com.boraji.tutorial.datetime;

import java.time.OffsetDateTime;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      OffsetDateTime dateTime = OffsetDateTime.now();
      
      System.out.println("Hour : " + dateTime.getHour());
      System.out.println("Minute : " + dateTime.getMinute());
      System.out.println("Second : " + dateTime.getSecond());
      System.out.println("Nano : "+dateTime.getNano());
   }
}

Output

Hour : 21
Minute : 41
Second : 10
Nano : 941000000

 

How to add or subtract years, months, days, hours, minutes and seconds to OffsetDateTime

You can use the plus and minus methods of the OffsetDateTime class to add or subtract days, months, hours, minutes etc.

package com.boraji.tutorial.datetime;

import java.time.OffsetDateTime;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      OffsetDateTime dateTime = OffsetDateTime.now();

      // LocalDateTime's plus methods
      System.out.println("Addition of days : " + dateTime.plusDays(5));
      System.out.println("Addition of months : " + dateTime.plusMonths(15));
      System.out.println("Addition of years : " + dateTime.plusYears(5));
      System.out.println("Addition of Hours : " + dateTime.plusHours(2));
      System.out.println("Addition of Minutes : " + dateTime.plusMinutes(30));
      System.out.println("Addition of Seconds : " + dateTime.plusSeconds(20));

      // LocalDateTime's minus methods
      System.out.println("Subtraction of days : " + dateTime.minusDays(5));
      System.out.println("Subtraction of months : " + dateTime.minusMonths(15));
      System.out.println("Subtraction of years : " + dateTime.minusYears(5));
      System.out.println("Subtraction of Hours : " + dateTime.minusHours(2));
      System.out.println("Subtraction of Minutes : " + dateTime.minusMinutes(30));
      System.out.println("Subtraction of Seconds : " + dateTime.minusSeconds(20));
   }
}

Output

Addition of days : 2017-07-11T21:43:47.910+05:30
Addition of months : 2018-10-06T21:43:47.910+05:30
Addition of years : 2022-07-06T21:43:47.910+05:30
Addition of Hours : 2017-07-06T23:43:47.910+05:30
Addition of Minutes : 2017-07-06T22:13:47.910+05:30
Addition of Seconds : 2017-07-06T21:44:07.910+05:30
Subtraction of days : 2017-07-01T21:43:47.910+05:30
Subtraction of months : 2016-04-06T21:43:47.910+05:30
Subtraction of years : 2012-07-06T21:43:47.910+05:30
Subtraction of Hours : 2017-07-06T19:43:47.910+05:30
Subtraction of Minutes : 2017-07-06T21:13:47.910+05:30
Subtraction of Seconds : 2017-07-06T21:43:27.910+05:30

 

How to compare OffsetDateTime objects

You can use the isEqual()compareTo()isAfter() and isBefore() methods of the OffsetDateTime class to compare the OffsetDateTime objects.

package com.boraji.tutorial.datetime;

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

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      LocalDateTime localDateTime = LocalDateTime.now();

      OffsetDateTime offsetDateTime1 = OffsetDateTime.of(localDateTime,
            ZoneOffset.ofHoursMinutes(5, 30));

      OffsetDateTime offsetDateTime2 = OffsetDateTime.of(localDateTime,
            ZoneOffset.ofHoursMinutes(5, 30));

      OffsetDateTime offsetDateTime3 = OffsetDateTime.of(localDateTime,
            ZoneOffset.ofHoursMinutes(6, 30));

      // Using isEqual()
      if (offsetDateTime1.isEqual(offsetDateTime2)) {
         System.out.println("offsetDateTime1 and offsetDateTime2 are equal.");
      } else {
         System.out.println("offsetDateTime1 and offsetDateTime2 are not equal.");
      }

      // Using compareTo()
      if (offsetDateTime1.compareTo(offsetDateTime2) == 0) {
         System.out.println("offsetDateTime1 and offsetDateTime2 are equal.");
      } else {
         System.out.println("offsetDateTime1 and offsetDateTime2 are not equal.");
      }

      // Using isAfter()
      if (offsetDateTime1.isAfter(offsetDateTime3)) {
         System.out.println("offsetDateTime1 is after offsetDateTime3");
      }

      // Using isBefore()
      if (offsetDateTime3.isBefore(offsetDateTime2)) {
         System.out.println("offsetDateTime3 is before offsetDateTime2");
      }
   }
}

Output

offsetDateTime1 and offsetDateTime2 are equal.
offsetDateTime1 and offsetDateTime2 are equal.
offsetDateTime1 is after offsetDateTime3
offsetDateTime3 is before offsetDateTime2

 

How to convert or parse String to OffsetDateTime in java

You can use the parse() method of the OffsetDateTime class to convert the String to OffsetDateTime.

package com.boraji.tutorial.datetime;

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

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      // Parsing ISO offset date time
      OffsetDateTime dateTime = OffsetDateTime.parse("2017-07-07T21:36:10.598+05:30",
            DateTimeFormatter.ISO_OFFSET_DATE_TIME);
      System.out.println(dateTime);

      // Parsing 'yyyy-MMM-dd HH:mm:ss Z' pattern
      OffsetDateTime dateTime1 = OffsetDateTime.parse("2017-May-02 23:35:05 +0530",
            DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss Z"));
      System.out.println(dateTime1);

      // Parsing 'yyyy-MM-dd KK:mm:ss a x' pattern
      OffsetDateTime dateTime2 = OffsetDateTime.parse("2017-05-30 10:20:30 AM +0530",
            DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a x"));
      System.out.println(dateTime2);

      // Formatting 'cccc, MMMM dd, yyyy KK:mm a X' pattern
      OffsetDateTime dateTime3 = OffsetDateTime.parse("Wednesday, May 31, 2017 10:21 PM +0530",
            DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a X"));
      System.out.println(dateTime3);
   }
}

Output

2017-07-07T21:36:10.598+05:30
2017-05-02T23:35:05+05:30
2017-05-30T10:20:30+05:30
2017-05-31T22:21+05:30

 

How to convert or format OffsetDateTime to String in java

You can use the format() method of the OffsetDateTime class to convert the OffsetDateTime to String.

package com.boraji.tutorial.datetime;

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

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      OffsetDateTime dateTime = OffsetDateTime.now();
      
      // Formatting ISO offset date time
      System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));

      // Formatting 'yyyy-MMM-dd HH:mm:ss Z' pattern
      System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss Z")));

      // Formatting 'yyyy-MM-dd KK:mm:ss a x' pattern
      System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd KK:mm:ss a x")));

      // Formatting 'cccc, MMMM dd, yyyy KK:mm a X' pattern
      System.out.println(dateTime.format(DateTimeFormatter.ofPattern("cccc, MMMM dd, yyyy KK:mm a X")));
   }
}

Output

2017-07-07T22:06:41.825+05:30
2017-Jul-07 22:06:41 +0530
2017-07-07 10:06:41 PM +0530
Friday, July 07, 2017 10:06 PM +0530

 

 

How to get OffsetDateTime from LocalDateTime, ZonedDateTime and Instant

The following example demonstrates how to get the OffsetDateTime time from the LocalDateTimeZonedDateTime and Instant.

package com.boraji.tutorial.datetime;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class OffsetDateTimeExample {
   public static void main(String[] args) {

      // LocalDateTime to OffsetDateTime
      LocalDateTime localDateTime = LocalDateTime.now();
      OffsetDateTime offsetDateTime = localDateTime
            .atOffset(ZoneOffset.ofHoursMinutes(5, 30));
      System.out.println(offsetDateTime);

      // ZonedDateTime to OffsetDateTime
      ZonedDateTime zonedDateTime = ZonedDateTime.now();
      OffsetDateTime offsetDateTime1 = zonedDateTime.toOffsetDateTime();
      System.out.println(offsetDateTime1);

      // Instant to OffsetDateTime
      Instant instant = Instant.now();
      OffsetDateTime offsetDateTime2 = instant.atOffset(ZoneOffset.ofHours(7));
      System.out.println(offsetDateTime2);

   }
}

Output

2017-07-07T22:15:35.709+05:30
2017-07-07T22:15:35.710+05:30
2017-07-07T23:45:35.710+07:00