The ZonedDateTime class in Java represents a date and time together with a time zone. It stores date and time fields with nanosecond precision and a zone offset, making it useful for working with date and time across different time zones.
- ZonedDateTime is an immutable class that implements Temporal and ChronoZonedDateTime<LocalDate>.
- It provides methods to create, retrieve, format, compare, modify, and convert date-time values.
Example: Program creates a ZonedDateTime object using a date, time, and Asia/Kolkata time zone.
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){
LocalDate date = LocalDate.of(2026, 9, 8);
LocalTime time = LocalTime.of(10, 30);
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime dateTime = ZonedDateTime.of(date, time, zone);
System.out.println("Zoned Date-Time: " + dateTime);
}
}
Output
Zoned Date-Time: 2026-09-08T10:30+05:30[Asia/Kolkata]
Explanation: The of() method combines the specified LocalDate, LocalTime, and ZoneId to create a ZonedDateTime object.
Syntax
public final class ZonedDateTime
extends Object
implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
Ways to Use ZonedDateTime
The ZonedDateTime class provides methods for retrieving date-time fields, creating date-time objects, performing calculations, and converting between different date-time representations.
1. Using parse() Method
The parse() method creates a ZonedDateTime object from a text representation containing a date, time, offset, and time zone.
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]";
ZonedDateTime dateTime = ZonedDateTime.parse(text);
System.out.println(dateTime);
}
}
Output
2011-10-02T14:45:30.123456789+05:30[Asia/Kolkata]
Explanation: The program uses ZonedDateTime.parse() to parse the given text and create a ZonedDateTime object containing the date, time, offset, and time zone.
2. Using of() Method
The of() method creates a ZonedDateTime from a LocalDate, LocalTime, and ZoneId.
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) {
LocalDate date = LocalDate.of(2026, 9, 8);
LocalTime time = LocalTime.of(10, 30);
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime dateTime =
ZonedDateTime.of(date, time, zone);
System.out.println(dateTime);
}
}
Output
2026-09-08T10:30+05:30[Asia/Kolkata]
Explanation: The program creates a date, time, and time zone separately and combines them into a ZonedDateTime object using of().
Commonly Used Methods of ZonedDateTime
| Method | Description |
|---|---|
now() | Returns the current date-time in the default time zone. |
now(ZoneId zone) | Returns the current date-time in the specified time zone. |
getYear() | Returns the year. |
getMonth() | Returns the month as a Month enum. |
getDayOfMonth() | Returns the day of the month. |
getDayOfWeek() | Returns the day of the week. |
getHour() | Returns the hour of the day. |
getMinute() | Returns the minute. |
getSecond() | Returns the second. |
getNano() | Returns the nanosecond. |
getOffset() | Returns the zone offset. |
getZone() | Returns the time zone. |
format() | Formats the date-time using a formatter. |
plusDays() | Adds the specified number of days. |
minusDays() | Subtracts the specified number of days. |
toLocalDate() | Converts to LocalDate. |
toLocalTime() | Converts to LocalTime. |
toOffsetDateTime() | Converts to OffsetDateTime. |
withZoneSameInstant() | Changes the time zone while retaining the same instant. |
Note: ZonedDateTime stores a date, time, time-zone ID, and the corresponding offset. The offset can vary for a time zone because of rules such as daylight saving time.