The Wayback Machine - https://web.archive.org/web/20241001041120/https://www.geeksforgeeks.org/java-time-zoneddatetime-class-in-java/
Open In App

java.time.ZonedDateTime Class in Java

Last Updated : 19 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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() method
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);
        System.out.println(zone);
    }
}


Output

2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]

Implementation of getZone() method:

Java




// java program to illustrate working of getZone() method
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 the zone of this zonedDateTime object
        System.out.println(zone.getZone());
    }
}


Output

Asia/Kolkata

Implementation of plus() and minus() method:

Java




// java program to illustrate working
// of plus() and minus() method
import 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 subtracting 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)));
    }
}


Output

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() method
import 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);
    }
}


Output

2021-03-20T17:09:56.241560Z[Etc/UTC]


Previous Article
Next Article

Similar Reads

ZonedDateTime minusWeeks() method in Java with Examples
minusWeeks() method of a ZonedDateTime class used to subtract the number of weeks from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method operates on the local time-line, subtracts weeks to the local date-time and after subtracting weeks local date-time is converted back to a ZonedDateTime, using the zone ID to obta
2 min read
ZonedDateTime getZone() method in Java with Examples
The getZone() method of a ZonedDateTime class is used to get the time-zone from this ZonedDateTime. This method returns the ZoneId such as 'Asia/Calcutta'. Syntax: public ZoneId getZone() Parameters: This method does not take any parameters. Return value: This method returns a ZoneId representing the time-zone. Below programs illustrate the getZone
1 min read
ZonedDateTime getDayOfWeek() method in Java with Examples
The getDayOfWeek() method of a ZonedDateTime class is used to get the day-of-week field from this ZonedDateTime, which is an enum DayOfWeek.Additional information can be obtained from the DayOfWeek. This includes textual names of the values. Syntax: public DayOfWeek getDayOfWeek() Parameters: This method does not take any parameters. Return value:
1 min read
ZonedDateTime getNano() method in Java with Examples
The getNano() method of a ZonedDateTime class is used to get nano-of-second field lie between 0 to 999, 999, 999 from this ZonedDateTime. Syntax: public int getNano() Parameters: This method does not take any parameters. Return value: This method returns an integer representing the nano-of-second, from 0 to 999, 999, 999. Below programs illustrate
1 min read
ZonedDateTime hashCode() method in Java with Examples
The hashCode() method of a ZonedDateTime class is used to get the hashcode for this ZonedDateTime. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like hashtable, hashmap etc. An object can also be searched with this unique code. Syntax: public ZoneId h
1 min read
ZonedDateTime getMonth() method in Java with Examples
The getMonth() method of a ZonedDateTime class is used to get month-of-year field using the Month enum from this ZonedDateTime. If you need access to the primitive int value then the enum provides the int value. Syntax: public Month getMonth() Parameters: This method does not take any parameters. Return value: This method returns an enum Month repr
1 min read
ZonedDateTime getSecond() method in Java with Examples
The getSecond() method of a ZonedDateTime class is used to get second-of-minute field from this ZonedDateTime.This method returns the integer value for the second from 0 to 59. Syntax: public int getSecond() Parameters: This method does not take any parameters. Return value: This method returns an integer representing the second-of-minute, from 0 t
1 min read
ZonedDateTime getYear() method in Java with Examples
The getYear() method of a ZonedDateTime class is used to get year field from this ZonedDateTime.This method returns the integer value for the year, from MIN_YEAR to MAX_YEAR. Syntax: public int getYear() Parameters: This method does not take any parameters. Return value: This method returns an integer representing the year, from MIN_YEAR to MAX_YEA
1 min read
ZonedDateTime getDayOfMonth() method in Java with Examples
The getDayOfMonth() method of a ZonedDateTime class used to get the day-of-month field from this ZonedDateTime. This method returns the integer value for the day-of-month from 1 to 31. Syntax: public int getDayOfMonth() Parameters: This method does not take any parameters. Return value: This method returns an integer representing day-of-month, from
1 min read
ZonedDateTime getMonthValue() method in Java with Examples
The getMonthValue() method of a ZonedDateTime class is used to get month-of-year field lie between 1 to 12 from this ZonedDateTime. Syntax: public int getMonthValue() Parameters: This method does not take any parameters. Return value: This method returns an integer representing the month-of-year, from 1 to 12. Below programs illustrate the getMonth
1 min read
ZonedDateTime getMinute() method in Java with Examples
The getMinute() method of a ZonedDateTime class is used to get the minute-of-hour field from this ZonedDateTime. This method returns the integer value for the minute from 0 to 59. Syntax: public int getMinute() Parameters: This method does not take any parameters. Return value: This method returns an integer representing minute, from 0 to 59. Below
1 min read
ZonedDateTime toLocalTime() method in Java with Examples
The toLocalTime() method of a ZonedDateTime class is used to return the LocalTime part of this date-time object with the same hour, minute, second and nanosecond as this date-time. Syntax: public LocalTime toLocalTime() Parameters: This method does not take any parameters. Return value: This method returns a LocalTime representing the time part of
1 min read
ZonedDateTime getHour() method in Java with Examples
The getHour() method of a ZonedDateTime class used to get the hour-of-day field from this ZonedDateTime. This method returns the integer value for the hour from 0 to 23. Syntax: public int getHour() Parameters: This method does not take any parameters. Return value: This method returns an integer representing hour, from 0 to 23. Below programs illu
1 min read
ZonedDateTime toLocalDate() method in Java with Examples
The toLocalDate() method of a ZonedDateTime class is used to return the LocalDate part of this date-time object with the same year, month and day as this date-time. Syntax: public LocalDate toLocalDate() Parameters: This method does not take any parameters. Return value: This method returns a LocalDate representing the date part of this date-time,
1 min read
ZonedDateTime getDayOfYear() method in Java with Examples
The getDayOfYear() method of a ZonedDateTime class is used to get the day-of-year field from this ZonedDateTime. This method returns the integer value for the day-of-year from 1 to 365, or 366 in a leap year. Syntax: public int getDayOfYear() Parameters: This method does not take any parameters. Return value: This method returns an integer represen
1 min read
ZonedDateTime toOffsetDateTime() method in Java with Examples
The toOffsetDateTime() method of a ZonedDateTime class is used to convert this date-time to an OffsetDateTime and the zone ID is ignored. Syntax: public OffsetDateTime toOffsetDateTime() Parameters: This method does not take any parameters. Return value: This method returns an offset date-time representing the same local date-time and offset. Below
1 min read
ZonedDateTime toString() method in Java with Examples
The toString() method of a ZonedDateTime class is used to return this ZonedDateTime as a String. The string consists of the LocalDateTime followed by the ZoneOffset. If the ZoneId is not the same as the offset, then the ID is output. Syntax: public String toString() Parameters: This method does not take any parameters. Return value: This method ret
1 min read
ZonedDateTime toLocalDateTime() method in Java with Examples
The toLocalDateTime() method of a ZonedDateTime class is used to return the LocalDateTime part of this date-time object with the same year, month, day and time as this date-time. Syntax: public LocalDateTime toLocalDateTime() Parameters: This method does not take any parameters. Return value: This method returns a LocalDateTime representing the loc
1 min read
ZonedDateTime plusDays() method in Java with Examples
The plusDays() method of ZonedDateTime class in Java is used to add the number of specified days in this ZonedDateTime instance and return a copy of ZonedDateTime. This method adds no of days passed as a parameter to the local date-time and then it is converted back to a ZonedDateTime, using the zone ID to obtain the offset. When converting back to
2 min read
ZonedDateTime minusSeconds() method in Java with Examples
minusSeconds() method of a ZonedDateTime class used to subtract the number of seconds from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method works on the instant time-line and subtracting one-second returns the time-line of one second earlier. Note that this method is a different approach to that used by days, mont
2 min read
ZonedDateTime minusYears() method in Java with Examples
The minusYears() method of a ZonedDateTime class used to subtract the number of years from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method operates on the local time-line, subtract years from the local date-time and after subtracting years local date-time is converted back to a ZonedDateTime, using the zone ID to
2 min read
ZonedDateTime plusYears() method in Java with Examples
The plusYears() method of a ZonedDateTime class used to add the number of years in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method operates on the local time-line, adds years to the local date-time and after adding years local date-time is converted back to a ZonedDateTime, using the zone ID to obtain the offset. Wh
2 min read
ZonedDateTime plusWeeks() method in Java with Examples
plusWeeks() method of a ZonedDateTime class used to add the number of weeks in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method operates on the local time-line, adds weeks to the local date-time and after adding weeks local date-time is converted back to a ZonedDateTime, using the zone ID to obtain the offset. When c
2 min read
ZonedDateTime minusNanos() method in Java with Examples
minusNanos() method of a ZonedDateTime class used to subtract the number of nanoseconds from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method works on the instant timeline and subtracting one nanosecond return the timeline of one nanosecond earlier. Note that this method is a different approach to that used by day
2 min read
ZonedDateTime minusMinutes() method in Java with Examples
minusMinutes() method of a ZonedDateTime class used to subtract the number of minutes from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This method operates on the instant time-line, such that subtracting one minute will always be a duration of one minute earlier. This instance is immutable and unaffected by this method c
2 min read
ZonedDateTime minusDays() method in Java with Examples
minusDays() method of a ZonedDateTime class used to subtract the number of specified days from this ZonedDateTime and return a copy of ZonedDateTime.This method subtracts no of days passed as a parameter from the local date-time and then it is converted back to a ZonedDateTime, using the zone ID to obtain the offset. When converting back to ZonedDa
2 min read
ZonedDateTime plusMonths() method in Java with Examples
plusMonths() method of a ZonedDateTime class used to add the number of months in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method operates on the local time-line, adds months to the local date-time and after adding months local date-time is converted back to a ZonedDateTime, using the zone ID to obtain the offset. Wh
2 min read
ZonedDateTime minusHours() method in Java with Examples
minusHours() method of a ZonedDateTime class used to subtract the number of hours from this ZonedDateTime and return a copy of ZonedDateTime after subtraction.This operates on the instant time-line, such that subtracting one hour will always be a duration of one hour earlier. subtracting one hour may cause the local date-time to change by an amount
2 min read
ZonedDateTime plusMinutes() method in Java with Examples
plusMinutes() method of a ZonedDateTime class used to add the number of minutes in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method operates on the instant time-line, such that adding one minute will always be a duration of one minute later. This instance is immutable and unaffected by this method call. Syntax: publi
2 min read
ZonedDateTime plusHours() method in Java with Examples
plusHours() method of a ZonedDateTime class used to add the number of hours in this ZonedDateTime and return a copy of ZonedDateTime after addition.This operates on the instant time-line, such that adding one hour will always be a duration of one hour later. Adding one hour may cause the local date-time to change by an amount other than one hour. F
2 min read
Practice Tags :