APIdia Launch Partner
Top Description Fields Constructors Methods
java.time

public final Class ZonedDateTime

extends Object
implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.time.chrono.ChronoZonedDateTime, java.lang.Comparable, java.time.temporal.Temporal, java.time.temporal.TemporalAccessor
Annotations
@ValueBased
Static Imports
java.time.temporal.ChronoField.INSTANT_SECONDS, .ChronoField.NANO_OF_SECOND, .ChronoField.OFFSET_SECONDS

A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone" can be stored in a ZonedDateTime.

This class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by a ZoneOffset.

Converting between the two time-lines involves calculating the offset using the rules accessed from the ZoneId. Obtaining the offset for an instant is simple, as there is exactly one valid offset for each instant. By contrast, obtaining the offset for a local date-time is not straightforward. There are three cases:

Any method that converts directly or implicitly from a local date-time to an instant by obtaining the offset has the potential to be complicated.

For Gaps, the general strategy is that if the local date-time falls in the middle of a Gap, then the resulting zoned date-time will have a local date-time shifted forwards by the length of the Gap, resulting in a date-time in the later offset, typically "summer" time.

For Overlaps, the general strategy is that if the local date-time falls in the middle of an Overlap, then the previous offset will be retained. If there is no previous offset, or the previous offset is invalid, then the earlier offset is used, typically "summer" time. Two additional methods, withEarlierOffsetAtOverlap() and withLaterOffsetAtOverlap(), help manage the case of an overlap.

In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId. The ZoneOffset is a vital, but secondary, piece of information, used to ensure that the class represents an instant, especially during a daylight savings overlap.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail. The equals method should be used for comparisons.

Implementation Specification

A ZonedDateTime holds state equivalent to three separate objects, a LocalDateTime, a ZoneId and the resolved ZoneOffset. The offset and local date-time are used to define an instant when necessary. The zone ID is used to obtain the rules for how and when the offset changes. The offset cannot be freely set, as the zone controls which offsets are valid.

This class is immutable and thread-safe.

Since
1.8

Field Summary

Modifier and TypeField and Description
private final LocalDateTime
private final ZoneOffset
private static final long
serialVersionUID

Serialization version.

private final ZoneId

Constructor Summary

AccessConstructor and Description
private
ZonedDateTime(LocalDateTime
the date-time, validated as not null
dateTime
,
ZoneOffset
the zone offset, validated as not null
offset
,
ZoneId
the time-zone, validated as not null
zone
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private static ZonedDateTime

Returns:

the zoned date-time, not null
create
(long
the number of seconds from the epoch of 1970-01-01T00:00:00Z
epochSecond
,
int
the nanosecond within the second, from 0 to 999,999,999
nanoOfSecond
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime using seconds from the epoch of 1970-01-01T00:00:00Z.

public boolean

Returns:

true if this is equal to the other date-time
equals
(Object
the object to check, null returns false
obj
)

Overrides java.lang.Object.equals.

Implements java.time.chrono.ChronoZonedDateTime.equals.

Checks if this date-time is equal to another date-time.

public String

Returns:

the formatted date-time string, not null
format
(DateTimeFormatter
the formatter to use, not null
formatter
)

Overrides default java.time.chrono.ChronoZonedDateTime.format.

Formats this date-time using the specified formatter.

public static ZonedDateTime

Returns:

the zoned date-time, not null
from
(TemporalAccessor
the temporal object to convert, not null
temporal
)

Hides java.time.chrono.ChronoZonedDateTime.from.

Obtains an instance of ZonedDateTime from a temporal object.

public int

Returns:

the value for the field
get
(TemporalField
the field to get, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.get, java.time.chrono.ChronoZonedDateTime.get.

Gets the value of the specified field from this date-time as an int.

public int

Returns:

the day-of-month, from 1 to 31
getDayOfMonth
()

Gets the day-of-month field.

public DayOfWeek

Returns:

the day-of-week, not null
getDayOfWeek
()

Gets the day-of-week field, which is an enum DayOfWeek.

public int

Returns:

the day-of-year, from 1 to 365, or 366 in a leap year
getDayOfYear
()

Gets the day-of-year field.

public int

Returns:

the hour-of-day, from 0 to 23
getHour
()

Gets the hour-of-day field.

public long

Returns:

the value for the field
getLong
(TemporalField
the field to get, not null
field
)

Overrides default java.time.chrono.ChronoZonedDateTime.getLong.

Implements java.time.temporal.TemporalAccessor.getLong.

Gets the value of the specified field from this date-time as a long.

public int

Returns:

the minute-of-hour, from 0 to 59
getMinute
()

Gets the minute-of-hour field.

public Month

Returns:

the month-of-year, not null
getMonth
()

Gets the month-of-year field using the Month enum.

public int

Returns:

the month-of-year, from 1 to 12
getMonthValue
()

Gets the month-of-year field from 1 to 12.

public int

Returns:

the nano-of-second, from 0 to 999,999,999
getNano
()

Gets the nano-of-second field.

public ZoneOffset

Returns:

the zone offset, not null
getOffset
()

Implements java.time.chrono.ChronoZonedDateTime.getOffset.

Gets the zone offset, such as '+01:00'.

public int

Returns:

the second-of-minute, from 0 to 59
getSecond
()

Gets the second-of-minute field.

public int

Returns:

the year, from MIN_YEAR to MAX_YEAR
getYear
()

Gets the year field.

public ZoneId

Returns:

the time-zone, not null
getZone
()

Implements java.time.chrono.ChronoZonedDateTime.getZone.

Gets the time-zone, such as 'Europe/Paris'.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.time.chrono.ChronoZonedDateTime.hashCode.

A hash code for this date-time.

public boolean

Returns:

true if the field is supported on this date-time, false if not
isSupported
(TemporalField
the field to check, null returns false
field
)

Implements java.time.temporal.TemporalAccessor.isSupported, java.time.chrono.ChronoZonedDateTime.isSupported.

Checks if the specified field is supported.

public boolean

Returns:

true if the unit can be added/subtracted, false if not
isSupported
(TemporalUnit
the unit to check, null returns false
unit
)

Overrides default java.time.chrono.ChronoZonedDateTime.isSupported.

Implements java.time.temporal.Temporal.isSupported.

Checks if the specified unit is supported.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the subtraction made, not null
minus
(TemporalAmount
the amount to subtract, not null
amountToSubtract
)

Overrides default java.time.temporal.Temporal.minus, java.time.chrono.ChronoZonedDateTime.minus.

Returns a copy of this date-time with the specified amount subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the specified amount subtracted, not null
minus
(long
the amount of the unit to subtract from the result, may be negative
amountToSubtract
,
TemporalUnit
the unit of the amount to subtract, not null
unit
)

Overrides default java.time.temporal.Temporal.minus, java.time.chrono.ChronoZonedDateTime.minus.

Returns a copy of this date-time with the specified amount subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the days subtracted, not null
minusDays
(long
the days to subtract, may be negative
days
)

Returns a copy of this ZonedDateTime with the specified number of days subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the hours subtracted, not null
minusHours
(long
the hours to subtract, may be negative
hours
)

Returns a copy of this ZonedDateTime with the specified number of hours subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the minutes subtracted, not null
minusMinutes
(long
the minutes to subtract, may be negative
minutes
)

Returns a copy of this ZonedDateTime with the specified number of minutes subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the months subtracted, not null
minusMonths
(long
the months to subtract, may be negative
months
)

Returns a copy of this ZonedDateTime with the specified number of months subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the nanoseconds subtracted, not null
minusNanos
(long
the nanos to subtract, may be negative
nanos
)

Returns a copy of this ZonedDateTime with the specified number of nanoseconds subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the seconds subtracted, not null
minusSeconds
(long
the seconds to subtract, may be negative
seconds
)

Returns a copy of this ZonedDateTime with the specified number of seconds subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the weeks subtracted, not null
minusWeeks
(long
the weeks to subtract, may be negative
weeks
)

Returns a copy of this ZonedDateTime with the specified number of weeks subtracted.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the years subtracted, not null
minusYears
(long
the years to subtract, may be negative
years
)

Returns a copy of this ZonedDateTime with the specified number of years subtracted.

public static ZonedDateTime

Returns:

the current date-time using the system clock, not null
now
()

Obtains the current date-time from the system clock in the default time-zone.

public static ZonedDateTime

Returns:

the current date-time using the system clock, not null
now
(ZoneId
the zone ID to use, not null
zone
)

Obtains the current date-time from the system clock in the specified time-zone.

public static ZonedDateTime

Returns:

the current date-time, not null
now
(Clock
the clock to use, not null
clock
)

Obtains the current date-time from the specified clock.

public static ZonedDateTime

Returns:

the zoned date-time, not null
of
(LocalDate
the local date, not null
date
,
LocalTime
the local time, not null
time
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime from a local date and time.

public static ZonedDateTime

Returns:

the zoned date-time, not null
of
(LocalDateTime
the local date-time, not null
localDateTime
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime from a local date-time.

public static ZonedDateTime

Returns:

the zoned date-time, not null
of
(int
the year to represent, from MIN_YEAR to MAX_YEAR
year
,
int
the month-of-year to represent, from 1 (January) to 12 (December)
month
,
int
the day-of-month to represent, from 1 to 31
dayOfMonth
,
int
the hour-of-day to represent, from 0 to 23
hour
,
int
the minute-of-hour to represent, from 0 to 59
minute
,
int
the second-of-minute to represent, from 0 to 59
second
,
int
the nano-of-second to represent, from 0 to 999,999,999
nanoOfSecond
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.

public static ZonedDateTime

Returns:

the zoned date-time, not null
ofInstant
(Instant
the instant to create the date-time from, not null
instant
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime from an Instant.

public static ZonedDateTime

Returns:

the zoned date-time, not null
ofInstant
(LocalDateTime
the local date-time, not null
localDateTime
,
ZoneOffset
the zone offset, not null
offset
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime from the instant formed by combining the local date-time and offset.

private static ZonedDateTime

Returns:

the zoned date-time, not null
ofLenient
(LocalDateTime
the local date-time, not null
localDateTime
,
ZoneOffset
the zone offset, not null
offset
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime leniently, for advanced use cases, allowing any combination of local date-time, offset and zone ID.

This creates a zoned date-time with no checks other than no nulls.

public static ZonedDateTime

Returns:

the zoned date-time, not null
ofLocal
(LocalDateTime
the local date-time, not null
localDateTime
,
ZoneId
the time-zone, not null
zone
,
ZoneOffset
the zone offset, null if no preference
preferredOffset
)

Obtains an instance of ZonedDateTime from a local date-time using the preferred offset if possible.

public static ZonedDateTime

Returns:

the zoned date-time, not null
ofStrict
(LocalDateTime
the local date-time, not null
localDateTime
,
ZoneOffset
the zone offset, not null
offset
,
ZoneId
the time-zone, not null
zone
)

Obtains an instance of ZonedDateTime strictly validating the combination of local date-time, offset and zone ID.

This creates a zoned date-time ensuring that the offset is valid for the local date-time according to the rules of the specified zone.

public static ZonedDateTime

Returns:

the parsed zoned date-time, not null
parse
(CharSequence
the text to parse such as "2007-12-03T10:15:30+01:00[Europe/Paris]", not null
text
)

Obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris].

public static ZonedDateTime

Returns:

the parsed zoned date-time, not null
parse
(CharSequence
the text to parse, not null
text
,
DateTimeFormatter
the formatter to use, not null
formatter
)

Obtains an instance of ZonedDateTime from a text string using a specific formatter.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the addition made, not null
plus
(TemporalAmount
the amount to add, not null
amountToAdd
)

Overrides default java.time.temporal.Temporal.plus, java.time.chrono.ChronoZonedDateTime.plus.

Returns a copy of this date-time with the specified amount added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the specified amount added, not null
plus
(long
the amount of the unit to add to the result, may be negative
amountToAdd
,
TemporalUnit
the unit of the amount to add, not null
unit
)

Implements java.time.temporal.Temporal.plus, java.time.chrono.ChronoZonedDateTime.plus.

Returns a copy of this date-time with the specified amount added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the days added, not null
plusDays
(long
the days to add, may be negative
days
)

Returns a copy of this ZonedDateTime with the specified number of days added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the hours added, not null
plusHours
(long
the hours to add, may be negative
hours
)

Returns a copy of this ZonedDateTime with the specified number of hours added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the minutes added, not null
plusMinutes
(long
the minutes to add, may be negative
minutes
)

Returns a copy of this ZonedDateTime with the specified number of minutes added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the months added, not null
plusMonths
(long
the months to add, may be negative
months
)

Returns a copy of this ZonedDateTime with the specified number of months added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the nanoseconds added, not null
plusNanos
(long
the nanos to add, may be negative
nanos
)

Returns a copy of this ZonedDateTime with the specified number of nanoseconds added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the seconds added, not null
plusSeconds
(long
the seconds to add, may be negative
seconds
)

Returns a copy of this ZonedDateTime with the specified number of seconds added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the weeks added, not null
plusWeeks
(long
the weeks to add, may be negative
weeks
)

Returns a copy of this ZonedDateTime with the specified number of weeks added.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the years added, not null
plusYears
(long
the years to add, may be negative
years
)

Returns a copy of this ZonedDateTime with the specified number of years added.

public <
the type of the result
R
>
R

Returns:

the query result, null may be returned (defined by the query)
query
(TemporalQuery<R>
the query to invoke, not null
query
)

Overrides default java.time.temporal.TemporalAccessor.query, java.time.chrono.ChronoZonedDateTime.query.

Queries this date-time using the specified query.

public ValueRange

Returns:

the range of valid values for the field, not null
range
(TemporalField
the field to query the range for, not null
field
)

Overrides default java.time.temporal.TemporalAccessor.range, java.time.chrono.ChronoZonedDateTime.range.

Gets the range of valid values for the specified field.

pack-priv static ZonedDateTime
private void
readObject(ObjectInputStream
the stream to read
s
)

Defend against malicious streams.

private ZonedDateTime

Returns:

the zoned date-time, not null
resolveInstant
(LocalDateTime
the new local date-time, not null
newDateTime
)

Resolves the new local date-time using the offset to identify the instant.

private ZonedDateTime

Returns:

the zoned date-time, not null
resolveLocal
(LocalDateTime
the new local date-time, not null
newDateTime
)

Resolves the new local date-time using this zone ID, retaining the offset if possible.

private ZonedDateTime

Returns:

the zoned date-time, not null
resolveOffset
(ZoneOffset
the offset, not null
offset
)

Resolves the offset into this zoned date-time for the with methods.

public LocalDate

Returns:

the date part of this date-time, not null
toLocalDate
()

Overrides default java.time.chrono.ChronoZonedDateTime.toLocalDate.

Gets the LocalDate part of this date-time.

public LocalDateTime

Returns:

the local date-time part of this date-time, not null
toLocalDateTime
()

Implements java.time.chrono.ChronoZonedDateTime.toLocalDateTime.

Gets the LocalDateTime part of this date-time.

public LocalTime

Returns:

the time part of this date-time, not null
toLocalTime
()

Overrides default java.time.chrono.ChronoZonedDateTime.toLocalTime.

Gets the LocalTime part of this date-time.

public OffsetDateTime

Returns:

an offset date-time representing the same local date-time and offset, not null
toOffsetDateTime
()

Converts this date-time to an OffsetDateTime.

public String

Returns:

a string representation of this date-time, not null
toString
()

Overrides java.lang.Object.toString.

Implements java.time.chrono.ChronoZonedDateTime.toString.

Outputs this date-time as a String, such as 2007-12-03T10:15:30+01:00[Europe/Paris].

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the time truncated, not null
truncatedTo
(TemporalUnit
the unit to truncate to, not null
unit
)

Returns a copy of this ZonedDateTime with the time truncated.

public long

Returns:

the amount of time between this date-time and the end date-time
until
(Temporal
the end date, exclusive, which is converted to a ZonedDateTime, not null
endExclusive
,
TemporalUnit
the unit to measure the amount in, not null
unit
)

Implements java.time.temporal.Temporal.until.

Calculates the amount of time until another date-time in terms of the specified unit.

public ZonedDateTime

Returns:

a ZonedDateTime based on this with the adjustment made, not null
with
(TemporalAdjuster
the adjuster to use, not null
adjuster
)

Overrides default java.time.temporal.Temporal.with, java.time.chrono.ChronoZonedDateTime.with.

Returns an adjusted copy of this date-time.

public ZonedDateTime

Returns:

a ZonedDateTime based on this with the specified field set, not null
with
(TemporalField
the field to set in the result, not null
field
,
long
the new value of the field in the result
newValue
)

Implements java.time.temporal.Temporal.with, java.time.chrono.ChronoZonedDateTime.with.

Returns a copy of this date-time with the specified field set to a new value.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested day, not null
withDayOfMonth
(int
the day-of-month to set in the result, from 1 to 28-31
dayOfMonth
)

Returns a copy of this ZonedDateTime with the day-of-month altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date with the requested day, not null
withDayOfYear
(int
the day-of-year to set in the result, from 1 to 365-366
dayOfYear
)

Returns a copy of this ZonedDateTime with the day-of-year altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the earlier offset, not null
withEarlierOffsetAtOverlap
()

Implements java.time.chrono.ChronoZonedDateTime.withEarlierOffsetAtOverlap.

Returns a copy of this date-time changing the zone offset to the earlier of the two valid offsets at a local time-line overlap.

public ZonedDateTime

Returns:

a ZonedDateTime with the zone ID set to the offset, not null
withFixedOffsetZone
()

Returns a copy of this date-time with the zone ID set to the offset.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested hour, not null
withHour
(int
the hour-of-day to set in the result, from 0 to 23
hour
)

Returns a copy of this ZonedDateTime with the hour-of-day altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the later offset, not null
withLaterOffsetAtOverlap
()

Implements java.time.chrono.ChronoZonedDateTime.withLaterOffsetAtOverlap.

Returns a copy of this date-time changing the zone offset to the later of the two valid offsets at a local time-line overlap.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested minute, not null
withMinute
(int
the minute-of-hour to set in the result, from 0 to 59
minute
)

Returns a copy of this ZonedDateTime with the minute-of-hour altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested month, not null
withMonth
(int
the month-of-year to set in the result, from 1 (January) to 12 (December)
month
)

Returns a copy of this ZonedDateTime with the month-of-year altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested nanosecond, not null
withNano
(int
the nano-of-second to set in the result, from 0 to 999,999,999
nanoOfSecond
)

Returns a copy of this ZonedDateTime with the nano-of-second altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested second, not null
withSecond
(int
the second-of-minute to set in the result, from 0 to 59
second
)

Returns a copy of this ZonedDateTime with the second-of-minute altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested year, not null
withYear
(int
the year to set in the result, from MIN_YEAR to MAX_YEAR
year
)

Returns a copy of this ZonedDateTime with the year altered.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested zone, not null
withZoneSameInstant
(ZoneId
the time-zone to change to, not null
zone
)

Implements java.time.chrono.ChronoZonedDateTime.withZoneSameInstant.

Returns a copy of this date-time with a different time-zone, retaining the instant.

public ZonedDateTime

Returns:

a ZonedDateTime based on this date-time with the requested zone, not null
withZoneSameLocal
(ZoneId
the time-zone to change to, not null
zone
)

Implements java.time.chrono.ChronoZonedDateTime.withZoneSameLocal.

Returns a copy of this date-time with a different time-zone, retaining the local date-time if possible.

pack-priv void
private Object

Returns:

the instance of Ser, not null
writeReplace
()

Writes the object using a dedicated serialized form.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait