- java.lang.Object
-
- java.time.ZonedDateTime
-
- All Implemented Interfaces:
Serializable,Comparable<ChronoZonedDateTime<?>>,ChronoZonedDateTime<LocalDate>,Temporal,TemporalAccessor
public final class ZonedDateTime extends Object implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
A date-time with a time-zone in the ISO-8601 calendar system, such as2007-12-03T10:15:30+01:00 Europe/Paris.ZonedDateTimeis 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 aZonedDateTime.This class handles conversion from the local time-line of
LocalDateTimeto the instant time-line ofInstant. The difference between the two time-lines is the offset from UTC/Greenwich, represented by aZoneOffset.Converting between the two time-lines involves calculating the offset using the
rulesaccessed from theZoneId. 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:- Normal, with one valid offset. For the vast majority of the year, the normal case applies, where there is a single valid offset for the local date-time.
- Gap, with zero valid offsets. This is when clocks jump forward typically due to the spring daylight savings change from "winter" to "summer". In a gap there are local date-time values with no valid offset.
- Overlap, with two valid offsets. This is when clocks are set back typically due to the autumn daylight savings change from "summer" to "winter". In an overlap there are local date-time values with two valid offsets.
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()andwithLaterOffsetAtOverlap(), help manage the case of an overlap.In terms of design, this class should be viewed primarily as the combination of a
LocalDateTimeand aZoneId. TheZoneOffsetis 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; use of identity-sensitive operations (including reference equality (
==), identity hash code, or synchronization) on instances ofZonedDateTimemay have unpredictable results and should be avoided. Theequalsmethod should be used for comparisons.- Implementation Requirements:
- A
ZonedDateTimeholds state equivalent to three separate objects, aLocalDateTime, aZoneIdand the resolvedZoneOffset. 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
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)Checks if this date-time is equal to another date-time.Stringformat(DateTimeFormatter formatter)Formats this date-time using the specified formatter.static ZonedDateTimefrom(TemporalAccessor temporal)Obtains an instance ofZonedDateTimefrom a temporal object.intget(TemporalField field)Gets the value of the specified field from this date-time as anint.intgetDayOfMonth()Gets the day-of-month field.DayOfWeekgetDayOfWeek()Gets the day-of-week field, which is an enumDayOfWeek.intgetDayOfYear()Gets the day-of-year field.intgetHour()Gets the hour-of-day field.longgetLong(TemporalField field)Gets the value of the specified field from this date-time as along.intgetMinute()Gets the minute-of-hour field.MonthgetMonth()Gets the month-of-year field using theMonthenum.intgetMonthValue()Gets the month-of-year field from 1 to 12.intgetNano()Gets the nano-of-second field.ZoneOffsetgetOffset()Gets the zone offset, such as '+01:00'.intgetSecond()Gets the second-of-minute field.intgetYear()Gets the year field.ZoneIdgetZone()Gets the time-zone, such as 'Europe/Paris'.inthashCode()A hash code for this date-time.booleanisSupported(TemporalField field)Checks if the specified field is supported.booleanisSupported(TemporalUnit unit)Checks if the specified unit is supported.ZonedDateTimeminus(long amountToSubtract, TemporalUnit unit)Returns a copy of this date-time with the specified amount subtracted.ZonedDateTimeminus(TemporalAmount amountToSubtract)Returns a copy of this date-time with the specified amount subtracted.ZonedDateTimeminusDays(long days)Returns a copy of thisZonedDateTimewith the specified number of days subtracted.ZonedDateTimeminusHours(long hours)Returns a copy of thisZonedDateTimewith the specified number of hours subtracted.ZonedDateTimeminusMinutes(long minutes)Returns a copy of thisZonedDateTimewith the specified number of minutes subtracted.ZonedDateTimeminusMonths(long months)Returns a copy of thisZonedDateTimewith the specified number of months subtracted.ZonedDateTimeminusNanos(long nanos)Returns a copy of thisZonedDateTimewith the specified number of nanoseconds subtracted.ZonedDateTimeminusSeconds(long seconds)Returns a copy of thisZonedDateTimewith the specified number of seconds subtracted.ZonedDateTimeminusWeeks(long weeks)Returns a copy of thisZonedDateTimewith the specified number of weeks subtracted.ZonedDateTimeminusYears(long years)Returns a copy of thisZonedDateTimewith the specified number of years subtracted.static ZonedDateTimenow()Obtains the current date-time from the system clock in the default time-zone.static ZonedDateTimenow(Clock clock)Obtains the current date-time from the specified clock.static ZonedDateTimenow(ZoneId zone)Obtains the current date-time from the system clock in the specified time-zone.static ZonedDateTimeof(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)Obtains an instance ofZonedDateTimefrom a year, month, day, hour, minute, second, nanosecond and time-zone.static ZonedDateTimeof(LocalDate date, LocalTime time, ZoneId zone)Obtains an instance ofZonedDateTimefrom a local date and time.static ZonedDateTimeof(LocalDateTime localDateTime, ZoneId zone)Obtains an instance ofZonedDateTimefrom a local date-time.static ZonedDateTimeofInstant(Instant instant, ZoneId zone)Obtains an instance ofZonedDateTimefrom anInstant.static ZonedDateTimeofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)Obtains an instance ofZonedDateTimefrom the instant formed by combining the local date-time and offset.static ZonedDateTimeofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)Obtains an instance ofZonedDateTimefrom a local date-time using the preferred offset if possible.static ZonedDateTimeofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)Obtains an instance ofZonedDateTimestrictly validating the combination of local date-time, offset and zone ID.static ZonedDateTimeparse(CharSequence text)Obtains an instance ofZonedDateTimefrom a text string such as2007-12-03T10:15:30+01:00[Europe/Paris].static ZonedDateTimeparse(CharSequence text, DateTimeFormatter formatter)Obtains an instance ofZonedDateTimefrom a text string using a specific formatter.ZonedDateTimeplus(long amountToAdd, TemporalUnit unit)Returns a copy of this date-time with the specified amount added.ZonedDateTimeplus(TemporalAmount amountToAdd)Returns a copy of this date-time with the specified amount added.ZonedDateTimeplusDays(long days)Returns a copy of thisZonedDateTimewith the specified number of days added.ZonedDateTimeplusHours(long hours)Returns a copy of thisZonedDateTimewith the specified number of hours added.ZonedDateTimeplusMinutes(long minutes)Returns a copy of thisZonedDateTimewith the specified number of minutes added.ZonedDateTimeplusMonths(long months)Returns a copy of thisZonedDateTimewith the specified number of months added.ZonedDateTimeplusNanos(long nanos)Returns a copy of thisZonedDateTimewith the specified number of nanoseconds added.ZonedDateTimeplusSeconds(long seconds)Returns a copy of thisZonedDateTimewith the specified number of seconds added.ZonedDateTimeplusWeeks(long weeks)Returns a copy of thisZonedDateTimewith the specified number of weeks added.ZonedDateTimeplusYears(long years)Returns a copy of thisZonedDateTimewith the specified number of years added.<R> Rquery(TemporalQuery<R> query)Queries this date-time using the specified query.ValueRangerange(TemporalField field)Gets the range of valid values for the specified field.LocalDatetoLocalDate()Gets theLocalDatepart of this date-time.LocalDateTimetoLocalDateTime()Gets theLocalDateTimepart of this date-time.LocalTimetoLocalTime()Gets theLocalTimepart of this date-time.OffsetDateTimetoOffsetDateTime()Converts this date-time to anOffsetDateTime.StringtoString()Outputs this date-time as aString, such as2007-12-03T10:15:30+01:00[Europe/Paris].ZonedDateTimetruncatedTo(TemporalUnit unit)Returns a copy of thisZonedDateTimewith the time truncated.longuntil(Temporal endExclusive, TemporalUnit unit)Calculates the amount of time until another date-time in terms of the specified unit.ZonedDateTimewith(TemporalAdjuster adjuster)Returns an adjusted copy of this date-time.ZonedDateTimewith(TemporalField field, long newValue)Returns a copy of this date-time with the specified field set to a new value.ZonedDateTimewithDayOfMonth(int dayOfMonth)Returns a copy of thisZonedDateTimewith the day-of-month altered.ZonedDateTimewithDayOfYear(int dayOfYear)Returns a copy of thisZonedDateTimewith the day-of-year altered.ZonedDateTimewithEarlierOffsetAtOverlap()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.ZonedDateTimewithFixedOffsetZone()Returns a copy of this date-time with the zone ID set to the offset.ZonedDateTimewithHour(int hour)Returns a copy of thisZonedDateTimewith the hour-of-day altered.ZonedDateTimewithLaterOffsetAtOverlap()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.ZonedDateTimewithMinute(int minute)Returns a copy of thisZonedDateTimewith the minute-of-hour altered.ZonedDateTimewithMonth(int month)Returns a copy of thisZonedDateTimewith the month-of-year altered.ZonedDateTimewithNano(int nanoOfSecond)Returns a copy of thisZonedDateTimewith the nano-of-second altered.ZonedDateTimewithSecond(int second)Returns a copy of thisZonedDateTimewith the second-of-minute altered.ZonedDateTimewithYear(int year)Returns a copy of thisZonedDateTimewith the year altered.ZonedDateTimewithZoneSameInstant(ZoneId zone)Returns a copy of this date-time with a different time-zone, retaining the instant.ZonedDateTimewithZoneSameLocal(ZoneId zone)Returns a copy of this date-time with a different time-zone, retaining the local date-time if possible.-
Methods declared in class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods declared in interface java.time.chrono.ChronoZonedDateTime
compareTo, getChronology, isAfter, isBefore, isEqual, toEpochSecond, toInstant
-
-