java.time.ZonedDateTime Class in Java
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 |
---|---|
This method checks if this date-time is equal to another date-time. |
|
This method formats this date-time using the specified formatter. |
|
This method obtains an instance of ZonedDateTime from a temporal object. |
|
This method gets the value of the specified field from this date-time as an int. |
|
This method gets the day-of-month field. |
|
This method gets the day-of-week field, which is an enum DayOfWeek. |
|
This method gets the day-of-year field. |
|
This method gets the hour-of-day field. |
|
This method gets the value of the specified field from this date-time as a long. |
|
This method gets the minute-of-hour field. |
|
This method gets the month-of-year field using the Month enum. |
|
This method gets the month-of-year field from 1 to 12. |
|
This method gets the nano-of-second field. |
|
getOffset() |
This method gets the zone offset, such as ‘+01:00’. |
This method gets the second-of-minute field. |
|
This method gets the year field. |
|
This method gets the time-zone, such as ‘Europe/Paris’. |
|
A hash code for this date-time. |
|
This method checks if the specified field is supported. |
|
This method checks if the specified unit is supported. |
|
This method returns a copy of this date-time with the specified amount subtracted. |
|
This method returns a copy of this date-time with the specified amount subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of days subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of hours subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of minutes subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of months subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of nanoseconds subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of seconds subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of weeks subtracted. |
|
This method returns a copy of this ZonedDateTime with the specified number of years subtracted. |
|
This method obtains the current date-time from the system clock in the default time-zone. |
|
This method obtains the current date-time from the specified clock. |
|
This method obtains the current date-time from the system clock in the specified time-zone. |
|
This method obtains an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone. |
|
This method obtains an instance of ZonedDateTime from a local date and time. |
|
This method obtains an instance of ZonedDateTime from a local date-time. |
|
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. |
This method obtains an instance of ZonedDateTime from a text string such as 2007-12-03T10:15:30+01:00[Europe/Paris] |
|
This method obtains an instance of ZonedDateTime from a text string using a specific formatter. |
|
This method returns a copy of this date-time with the specified amount added. |
|
This method returns a copy of this date-time with the specified amount added. |
|
This method returns a copy of this ZonedDateTime with the specified number of days added. |
|
This method returns a copy of this ZonedDateTime with the specified number of hours added. |
|
This method returns a copy of this ZonedDateTime with the specified number of minutes added. |
|
This method returns a copy of this ZonedDateTime with the specified number of months added. |
|
This method returns a copy of this ZonedDateTime with the specified number of nanoseconds added. |
|
This method returns a copy of this ZonedDateTime with the specified number of seconds added. |
|
This method returns a copy of this ZonedDateTime with the specified number of weeks added. |
|
This method returns a copy of this ZonedDateTime with the specified number of years added. |
|
This method queries this date-time using the specified query. |
|
This method gets the range of valid values for the specified field. |
|
This method gets the LocalDate part of this date-time. |
|
This method gets the LocalDateTime part of this date-time. |
|
This method gets the LocalTime part of this date-time. |
|
This method converts this date-time to an OffsetDateTime. |
|
This method outputs this date-time as a String, such as 2007-12-03T10:15:30+01:00[Europe/Paris]. |
|
This method returns a copy of this ZonedDateTime with the time truncated. |
|
This method calculates the amount of time until another date-time in terms of the specified unit. |
|
This method returns an adjusted copy of this date-time. |
|
This method returns a copy of this date-time with the specified field set to a new value. |
|
This method returns a copy of this ZonedDateTime with the day-of-month altered. |
|
This method returns a copy of this ZonedDateTime with the day-of-year altered. |
|
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. |
|
This method returns a copy of this date-time with the zone ID set to the offset. |
|
This method returns a copy of this ZonedDateTime with the hour-of-day altered. |
|
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. |
|
This method returns a copy of this ZonedDateTime with the minute-of-hour altered. |
|
This method returns a copy of this ZonedDateTime with the month-of-year altered. |
|
This method returns a copy of this ZonedDateTime with the nano-of-second altered. |
|
This method returns a copy of this ZonedDateTime with the second-of-minute altered. |
|
This method returns a copy of this ZonedDateTime with the year altered. |
|
This method returns a copy of this date-time with a different time-zone, retaining the instant. |
|
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); } } |
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()); } } |
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 ))); } } |
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); } } |
2021-03-20T17:09:56.241560Z[Etc/UTC]