java.time.ZonedDateTime Class in Java

28 Mar, 2021
ZonedDateTime is an immutable object representing a date-time along with the time zone. This class stores all date and time fields.This class stores time to a precision of nanoseconds and a time-zone, with a zone Offset used to handle local date-times. For example, the value “2nd October 2011 at 14:45.30.123456789 +05:30 in the Asia/Kolkata time-zone” can be stored in a ZonedDateTime.This class is also used to convert the local time-line of LocalDateTime to the instant time-line of Instant.
Class Declaration:
public final class ZonedDateTime
extends Object
implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
Methods:
Method
Description
equals(Object obj)
This method checks if this date-time is equal to another date-time.
format(DateTimeFormatter formatter)
This method formats this date-time using the specified formatter.
from(TemporalAccessor temporal)
This method obtains an instance of ZonedDateTime from a temporal object.
get(TemporalField field)
This method gets the value of the specified field from this date-time as an int.
getDayOfMonth()
This method gets the day-of-month field.
getDayOfWeek()
This method gets the day-of-week field, which is an enum DayOfWeek.
getDayOfYear()
This method gets the day-of-year field.
getHour()
This method gets the hour-of-day field.
getLong(TemporalField field)
This method gets the value of the specified field from this date-time as a long.
getMinute()
This method gets the minute-of-hour field.
getMonth()
This method gets the month-of-year field using the Month enum.
getMonthValue()
This method gets the month-of-year field from 1 to 12.
getNano()
This method gets the nano-of-second field.
getOffset()
This method gets the zone offset, such as ‘+01:00’.
getSecond()
This method gets the second-of-minute field.
getYear()
This method gets the year field.
getZone()
This method gets the time-zone, such as ‘Europe/Paris’.
hashCode()
A hash code for this date-time.
isSupported(TemporalField field)
This method checks if the specified field is supported.
isSupported(TemporalUnit unit)
This method checks if the specified unit is supported.
minus(long amountToSubtract, TemporalUnit unit)
This method returns a copy of this date-time with the specified amount subtracted.
minus(TemporalAmount amountToSubtract)
This method returns a copy of this date-time with the specified amount subtracted.
minusDays(long days)
This method returns a copy of this ZonedDateTime with the specified number of days subtracted.
minusHours(long hours)
This method returns a copy of this ZonedDateTime with the specified number of hours subtracted.
minusMinutes(long minutes)
This method returns a copy of this ZonedDateTime with the specified number of minutes subtracted.
minusMonths(long months)
This method returns a copy of this ZonedDateTime with the specified number of months subtracted.
minusNanos(long nanos)
This method returns a copy of this ZonedDateTime with the specified number of nanoseconds subtracted.
minusSeconds(long seconds)
This method returns a copy of this ZonedDateTime with the specified number of seconds subtracted.
minusWeeks(long weeks)
This method returns a copy of this ZonedDateTime with the specified number of weeks subtracted.
minusYears(long years)
This method returns a copy of this ZonedDateTime with the specified number of years subtracted.
now()
This method obtains the current date-time from the system clock in the default time-zone.
now(Clock clock)
This method obtains the current date-time from the specified clock.
now(ZoneId zone)
This method obtains the current date-time from the system clock in the specified time-zone.
of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
This method obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.
of(LocalDate date, LocalTime time, ZoneId zone)
This method obtains an instance of ZonedDateTime from a local date and time.
of(LocalDateTime localDateTime, ZoneId zone)
This method obtains an instance of ZonedDateTime from a local date-time.
ofInstant(Instant instant, ZoneId zone)
This method obtains an instance of ZonedDateTime from an Instant.
ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
This method obtains an instance of ZonedDateTime from the instant formed by combining the local date-time and offset.
ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset)
This method obtains an instance of ZonedDateTime from a local date-time using the preferred offset if possible.
ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)
This method obtains an instance of ZonedDateTime strictly validating the combination of local date-time, offset and zone ID.
parse(CharSequence text)
This method obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris]
parse(CharSequence text, DateTimeFormatter formatter)
This method obtains an instance of ZonedDateTime from a text string using a specific formatter.
plus(long amountToAdd, TemporalUnit unit)
This method returns a copy of this date-time with the specified amount added.
plus(TemporalAmount amountToAdd)
This method returns a copy of this date-time with the specified amount added.
plusDays(long days)
This method returns a copy of this ZonedDateTime with the specified number of days added.
plusHours(long hours)
This method returns a copy of this ZonedDateTime with the specified number of hours added.
plusMinutes(long minutes)
This method returns a copy of this ZonedDateTime with the specified number of minutes added.
plusMonths(long months)
This method returns a copy of this ZonedDateTime with the specified number of months added.
plusNanos(long nanos)
This method returns a copy of this ZonedDateTime with the specified number of nanoseconds added.
plusSeconds(long seconds)
This method returns a copy of this ZonedDateTime with the specified number of seconds added.
plusWeeks(long weeks)
This method returns a copy of this ZonedDateTime with the specified number of weeks added.
plusYears(long years)
This method returns a copy of this ZonedDateTime with the specified number of years added.
query(TemporalQuery<R> query)
This method queries this date-time using the specified query.
range(TemporalField field)
This method gets the range of valid values for the specified field.
toLocalDate()
This method gets the LocalDate part of this date-time.
toLocalDateTime()
This method gets the LocalDateTime part of this date-time.
toLocalTime()
This method gets the LocalTime part of this date-time.
toOffsetDateTime()
This method converts this date-time to an OffsetDateTime.
toString()
This method outputs this date-time as a String, such as 2007-12-03T10:15:30+01:00[Europe/Paris].
truncatedTo(TemporalUnit unit)
This method returns a copy of this ZonedDateTime with the time truncated.
until(Temporal endExclusive, TemporalUnit unit)
This method calculates the amount of time until another date-time in terms of the specified unit.
with(TemporalAdjuster adjuster)
This method returns an adjusted copy of this date-time.
with(TemporalField field, long newValue)
This method returns a copy of this date-time with the specified field set to a new value.
withDayOfMonth(int dayOfMonth)
This method returns a copy of this ZonedDateTime with the day-of-month altered.
withDayOfYear(int dayOfYear)
This method returns a copy of this ZonedDateTime with the day-of-year altered.
withEarlierOffsetAtOverlap()
This method 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.
withFixedOffsetZone()
This method returns a copy of this date-time with the zone ID set to the offset.
withHour(int hour)
This method returns a copy of this ZonedDateTime with the hour-of-day altered.
withLaterOffsetAtOverlap()
This method 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.
withMinute(int minute)
This method returns a copy of this ZonedDateTime with the minute-of-hour altered.
withMonth(int month)
This method returns a copy of this ZonedDateTime with the month-of-year altered.
withNano(int nanoOfSecond)
This method returns a copy of this ZonedDateTime with the nano-of-second altered.
withSecond(int second)
This method returns a copy of this ZonedDateTime with the second-of-minute altered.
withYear(int year)
This method returns a copy of this ZonedDateTime with the year altered.
withZoneSameInstant(ZoneId zone)
This method returns a copy of this date-time with a different time-zone, retaining the instant.
withZoneSameLocal(ZoneId zone)
This method returns a copy of this date-time with a different time-zone, retaining the local date-time if possible.
Implementation of parse() method:
Java
// java program to create a new// ZonedDateTime using parse() methodimport java.time.ZonedDateTime;  public class GFG {      public static void main(String[] args)    {        String text            = "2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]";        // creating a new ZonedDateTime object        ZonedDateTime zone = ZonedDateTime.parse(text);        System.out.println(zone);    }}
2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]

Implementation of getZone() method:
Java
// java program to illustrate working of getZone() methodimport java.time.ZonedDateTime;  public class GFG {      public static void main(String[] args)    {        String text            = "2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]";          // Creating a new ZonedDateTime object        ZonedDateTime zone = ZonedDateTime.parse(text);          // printing the zone of this zonedDateTime object        System.out.println(zone.getZone());    }}
Asia/Kolkata

Implementation of plus() and minus() method:
Java
// java program to illustrate working// of plus() and minus() methodimport java.time.Period;import java.time.ZonedDateTime;  public class GFG {      public static void main(String[] args)    {        String text            = "2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]";          // Creating a new ZonedDateTime object        ZonedDateTime zone = ZonedDateTime.parse(text);          // printing original value of        // this ZonedDateTime object        System.out.println(zone);        // printing value of this ZonedDateTime        // object after substracing two months        System.out.println(zone.minus(Period.ofMonths(2)));        // printing value of this ZonedDateTime        // object after adding two months        System.out.println(zone.plus(Period.ofMonths(2)));    }}
2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]
2011-08-02T14:45:30.123456789+05:30[Asia/Kolkata]
2011-12-02T14:45:30.123456789+05:30[Asia/Kolkata]

Implementation of of() method:
Java
// Java program to illustrate working of of() methodimport java.time.LocalDate;import java.time.LocalTime;import java.time.ZoneId;import java.time.ZonedDateTime;  public class GFG {      public static void main(String[] args)    {        // getting current date        LocalDate date = LocalDate.now();          // getting current time        LocalTime time = LocalTime.now();          // getting system default zone id        ZoneId zoneId = ZoneId.systemDefault();          // creating a new ZonedDateTime object        ZonedDateTime zonedatetime            = ZonedDateTime.of(date, time, zoneId);        System.out.println(zonedatetime);    }}
2021-03-20T17:09:56.241560Z[Etc/UTC]

Java-time package
Java-ZonedDateTime
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Strings in Java
Java Programming Examples
Abstraction in Java
HashSet in Java