OffsetDateTime represents the date-time with an offset from UTC time such as 2022-06-12T11:25:45+02:00. This class stores date-time with all date & time fields upto precision of nanoseconds, as well as offset from UTC timezone. For example, the value “12th June 2022 at 10:33.49.123456789 +01:00” can be stored in an OffsetDateTime.
Java OffsetDateTime class:
public final class OffsetDateTime extends Object implements Temporal, TemporalAdjuster, Comparable<OffsetDateTime>, Serializable
Java OffsetDateTime – Method Summary
| Method | Description |
|---|---|
| static OffsetDateTime now() | Returns the current date-time from system in default timezone. |
| int compareTo(OffsetDateTime other) | It compares the date-time with another date-time. Returns negative if this date-time less than specified date-time else returns positive. |
| booelan equals(Object obj) | Checks if this date-time is equal to other specified date-time. |
| String format(DateTimeFormatter formatter) | It formats the date-time using specified formatter. |
| int getDayOfMonth() | Returns day-of-month field as an int value. |
| DayOfWeek getDayOfWeek() | Returns day-of-week field as an enum value. |
| int getDayOfYear() | Returns day-of-year field. |
| int getHour() | Returns hour-of-day field. Other similar get methods are getMinute(), getSecond(), getNano() & getYear() |
| boolean isAfter(OffsetDateTime other) | Checks if this date-time is after the specified date-time. |
| boolean isBefore(OffsetDateTime other) | Checks if this date-time is before the specified date-time. |
| boolean isEqual(OffsetDateTime other) | Checks if this date-time is equal to the specified date-time. |
| OffsetDateTime minusDays(long days) | Returns a copy of this OffsetDateTime after subtracting the specified number of days from it. Similar methods are minusHours(), minusMinutes(), minusSeconds(), minusNanos(), minusWeeks(), minusMonths() & minusYears(). |
| OffsetDateTime plusDays(long days) | Returns a copy of this OffsetDateTime after adding the specified number of days to it. Similar methods are plusHours(), plusMinutes(), plusSeconds(), plusNanos(), plusWeeks(), plusMonths() & plusYears(). |
| LocalDateTime toLocalDateTime() | Gets LocalDateTime field from this date-time. |
| OffsetDateTime withDayOfMonth(int dayOfMonth) | Returns a copy of this OffsetDateTime after altering the day-of-month field with the specified value. Similar methods are withDayOfYear(), withHour(), withMinute(), withSecond(), withNano() etc. |
Java OffsetDateTime – now & get methods example
Here we are getting current date-time info from the system using now() method and then we are using get methods to obtain the value of various fields from the current OffsetDateTime.
import java.time.OffsetDateTime;
public class JavaExample {
public static void main(String[] args) {
OffsetDateTime offDateTime = OffsetDateTime.now();
System.out.println("Current OffsetDateTime: "+offDateTime);
System.out.println("getMonth(): "+offDateTime.getMonth());
System.out.println("getDayOfWeek(): "+offDateTime.getDayOfWeek());
System.out.println("getHour(): "+offDateTime.getHour());
System.out.println("getMinute(): "+offDateTime.getMinute());
System.out.println("getSecond(): "+offDateTime.getSecond());
}
}
Output:
Current OffsetDateTime: 2022-06-12T11:36:32.571473+05:30 getMonth(): JUNE getDayOfWeek(): SUNDAY getHour(): 11 getMinute(): 36 getSecond(): 32
Java OffsetDateTime – isEqual(), isAfter(), isBefore() & with methods example
In this example, we are demonstrating the use of several important methods of OffsetDateTime class. The withHour() method is used to alter the hour in date-time. Methods isEqual(), isBefore() and isAfter() are used to compare two date-time.
import java.time.OffsetDateTime;
public class JavaExample {
public static void main(String[] args) {
OffsetDateTime offDateTime = OffsetDateTime.now();
System.out.println("Current OffsetDateTime: "+offDateTime);
OffsetDateTime offDateTime2 = offDateTime.withHour(9);
System.out.println("Altered OffsetDateTime: "+offDateTime2);
//Is current date-time equal to altered date time?
System.out.println(offDateTime.isEqual(offDateTime2));
//Is current date-time before altered date time?
System.out.println(offDateTime.isBefore(offDateTime2));
//Is current date-time after altered date time?
System.out.println(offDateTime.isAfter(offDateTime2));
}
}
Output:
Current OffsetDateTime: 2022-06-12T11:40:51.959723+05:30 Altered OffsetDateTime: 2022-06-12T09:40:51.959723+05:30 false false true
Java OffsetDateTime – plus & minus methods example
Here we will learn how to use various variants of plus and minus methods that are used to add or subtract date-time from this OffsetDateTime.
import java.time.OffsetDateTime;
public class JavaExample {
public static void main(String[] args) {
OffsetDateTime offDateTime = OffsetDateTime.now();
System.out.println("Current OffsetDateTime: "+offDateTime);
System.out.println("Minus 2 Hours: "+offDateTime.minusHours(2));
System.out.println("Minus 3 Days: "+offDateTime.minusDays(3));
System.out.println("Plus 5 minutes: "+offDateTime.plusMinutes(5));
System.out.println("Plus 2 months: "+offDateTime.plusMonths(2));
}
}
Output:
Current OffsetDateTime: 2022-06-12T11:44:32.129831+05:30 Minus 2 Hours: 2022-06-12T09:44:32.129831+05:30 Minus 3 Days: 2022-06-09T11:44:32.129831+05:30 Plus 5 minutes: 2022-06-12T11:49:32.129831+05:30 Plus 2 months: 2022-08-12T11:44:32.129831+05:30
Java OffsetDateTime – toLocalDate() example
Methods toLocalDate() and toLocalDateTime() are used to obtain local date and local date-time from offset time.
import java.time.OffsetDateTime;
public class JavaExample {
public static void main(String[] args) {
OffsetDateTime offDateTime = OffsetDateTime.now();
System.out.println("LocalDate: "+offDateTime.toLocalDate());
System.out.println("LocalDate: "+offDateTime.toLocalDateTime());
}
}
Output:
LocalDate: 2022-06-12 LocalDate: 2022-06-12T13:04:13.274151
Leave a Reply