Home » Java OffsetDateTime

Java OffsetDateTime

by Online Tutorials Library

Java OffsetDateTime Class

Java OffsetDateTime class is an immutable representation of a date-time with an offset. It inherits Object class and implements the Comparable interface.

OffsetDateTime class is used to store the date and time fields, to a precision of nanoseconds.

Java OffsetDateTime Class Declaration

Let’s see the declaration of java.time.OffsetDateTime class.

Methods of Java OffsetDateTime Class

Method Description
int get(TemporalField field) It is used to get the value of the specified field from this date-time as an int.
int getDayOfMonth() It is used to get the day-of-month field.
iint getDayOfYear() It is used to get the day-of-year field.
DayOfWeek getDayOfWeek() It is used to get the day-of-week field, which is an enum DayOfWeek.
OffsetDateTime minusDays(long days) It is used to return a copy of this OffsetDateTime with the specified number of days subtracted.
static OffsetDateTimenow() It is used to obtain the current date-time from the system clock in the default time-zone.
OffsetDateTime plusDays(long days) It is used to return a copy of this OffsetDateTime with the specified number of days added.
LocalDate toLocalDate() It is used to get the LocalDate part of this date-time.
Temporal adjustInto(Temporal temporal) It adjusts the specified temporal object to have the same date and time as this object.
TZonedDateTime atZoneSameInstant(ZoneId zone) It combines this date-time with a time-zone to create a ZonedDateTime ensuring that the result has the same instant.
TZonedDateTime atZoneSimilarLocal(ZoneId zone) It combines this date-time with a time-zone to create a ZonedDateTime trying to keep the same local date and time.
Tint compareTo(OffsetDateTime other) It compares this date-time to another date-time.
Tboolean equals(Object obj) It checks if this date-time is equal to another date-time.
TString format(DateTimeFormatter formatter) It formats this date-time using the specified formatter.
Tstatic OffsetDateTime from(TemporalAccessor temporal) It obtains an instance of OffsetDateTime from a temporal object.
Tint getHour() It gets the hour-of-day field.
Tlong getLong(TemporalField field) It gets the value of the specified field from this date-time as a long.
TMonth getMinute() It gets the minute-of-hour field.
TMonth getMonth() It gets the month-of-year field using the Month enum.
Tint getMonthValue() It gets the month-of-year field from 1 to 12.
Tint getNano() It gets the nano-of-second field.
TZoneOffset getOffset() It gets the zone offset, such as ‘+01:00’.
Tint getSecond() It gets the second-of-minute field.
Tint getYear() It gets the year field.
Tint hashCode() It returns hash code for this date-time.
Tboolean isAfter(OffsetDateTime other) It checks if this date-time is after the specified date-time.
Tboolean isBefore(OffsetDateTime other) It checks if this date-time is before the specified date-time.
Tboolean isEqual(OffsetDateTime other) It checks if this date-time is equal to the specified date-time.
Tboolean isSupported(TemporalField field) It checks if the specified field is supported.
Tboolean isSupported(TemporalUnit unit) It checks if the specified unit is supported.
TOffsetDateTime minus(long amountToSubtract, TemporalUnit unit) It returns a copy of this date-time with the specified amount subtracted.
TOffsetDateTime minus(TemporalAmount amountToSubtract) It returns a copy of this date-time with the specified amount subtracted.
TOffsetDateTime minusMonths(long monthsToSubtract) It returns a copy of this OffsetDateTime with the specified number of months subtracted.
TOffsetDateTime minusHours(long hoursToSubtract) It returns a copy of this OffsetDateTime with the specified number of hours subtracted.
TOffsetDateTime minusMinutes(long minutesToSubtract) It returns a copy of this OffsetDateTime with the specified number of minutes subtracted.
TOffsetDateTime minusNanos(long nanos) It returns a copy of this OffsetDateTime with the specified number of nanoseconds subtracted.
Tstatic OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) It obtains an instance of OffsetDateTime from a date, time and offset.
Tstatic OffsetDateTime ofInstant(Instant instant, ZoneId zone) It obtains an instance of OffsetDateTime from an Instant and zone ID.
Tstatic OffsetDateTime parse(CharSequence text) It obtains an instance of OffsetDateTime from a text string such as 2007-12-03T10:15:30.
TValueRange range(TemporalField field) It gets the range of valid values for the specified field.
Tlong toEpochSecond() It converts this date-time to the number of seconds from the epoch of 1970-01-01T00:00:00Z.
TOffsetDateTime truncatedTo(TemporalUnit unit) It returns a copy of this OffsetDateTime with the time truncated.
TOffsetDateTime with(TemporalAdjuster adjuster) It returns an adjusted copy of this date-time.

Java OffsetDateTime Class Example: getDayOfMonth()

OffsetDateTimeExample1.java

Test it Now

Output:

18  

Java OffsetDateTime Class Example: getDayOfYear()

OffsetDateTimeExample2.java

Test it Now

Output:

18  

Java OffsetDateTime Class Example: getDayOfWeek()

OffsetDateTimeExample3.java

Test it Now

Output:

WEDNESDAY  

Java OffsetDateTime Class Example: toLocalDate()

OffsetDateTimeExample4.java

Test it Now

Output:

2017-01-18  

Java OffsetDateTime Class Example: minusDays()

OffsetDateTimeExample5.java

Test it Now

Output:

2016-05-23T12:12:31.642+05:30  

Java OffsetDateTime Class Example: plusDays()

OffsetDateTimeExample6.java

Test it Now

Output:

2017-09-15T13:50:30.526+05:30  

Next TopicJava Clock

You may also like