The ZonedDateTime class represents the date with time and timezone information such as 2021-10-23T11:35:45+01:00 Europe/Paris. In the last post, we discussed LocalDateTime class, which represents the date with time but without timezone information so you can assume that a ZonedDateTime = LocalDateTime + Time Zone information. This is an immutable class. In this guide, we will see the methods of ZonedDateTime class with examples.
java ZonedDateTime:
public final class ZonedDateTime extends Object implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
Java ZonedDateTime – Method Summary
| Method | Description |
|---|---|
| boolean equals(Object obj) | It checks if this date-time is equal to the another date-time. |
| String format(DateTimeFormatter formatter) | Formats the date-time using specified formatter. |
| int get(TemporalField field) | Returns the value of specified field from date-time as an int. |
| int hashCode() | Returns hash code of this date-time. |
| int getDayOfMonth() | Gets day-of-month field. Other similar methods are getDayOfWeek(), getDayOfYear(), getHour(), getMinute(), getSecond() etc. |
| ZoneOffset getOffset() | Gets the zone offset such as ‘+02:00’. |
| ZoneId getZone() | Gets the time- zone such as such as ‘Asia/Kolkata’. |
| ZonedDateTime minusDays(long days) | Returns a copy of this ZonedDateTime after subtracting the specified number of days from it. Other similar methods to subtract date & time from ZonedDateTime are minusMonths(), minusYears(), minusWeeks(), minusHours(), minusMinutes() etc. |
| static ZonedDateTime now() | Returns this date-time obtained from system in a default time zone. |
| ZonedDateTime plusDays(long days) | Returns a copy of this ZonedDateTime after adding the specified number of days to it. Other similar methods to add date & time to ZonedDateTime are plusMonths(), plusYears(), plusWeeks(), plusHours(), plusMinutes() etc. |
| LocalDate toLocalDate() | Gets the local date from date-time. |
| OffsetDateTime toOffsetDateTime() | Converts the date-time to the OffsetDateTime. |
| ZonedDateTime withDayOfMonth(int dayOfMonth) | Returns a copy of this date-time after altering the day-of-month field with the specified value. Other similar method is withDayOfYear() which is used to alter the year. |
| ZonedDateTime withHour(int hour) | Returns a copy of this date-time after altering the hour field with the specified value. Other similar methods are withMinute(), withSecond(), withYear(), withNano() etc. |
1. Java ZonedDateTime : of() method example
The of() method obtains the instance of ZonedDateTime with the specified year, month, day, hour, minute, second, nanosecond and time-zone.
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class Example{
public static void main(String[] args) {
//creating a zone id
ZoneId zoneId = ZoneId.of("Asia/Kolkata");
//ZonedDateTime with the specified date, time and zoneId
ZonedDateTime zonedDateTime =
ZonedDateTime.of(2017, 10, 29, 22, 10, 45, 999, zoneId);
System.out.println("ZonedDateTime: "+zonedDateTime);
}
}
Output:
ZonedDateTime: 2017-10-29T22:10:45.000000999+05:30[Asia/Kolkata]
2. Java ZonedDateTime – current date time with default zone id
Here we are simply fetching the system current date-time in default zone id using the now() method.
import java.time.ZonedDateTime;
public class Example{
public static void main(String[] args) {
//current date time with default zone id
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Current Date Time: "+zonedDateTime);
}
}
Output:
Current Date Time: 2017-10-31T20:18:33.524+05:30[Asia/Kolkata]
3. Java ZonedDateTime get Methods Example
getDayOfWeek() – Returns the Day of the Week such as Monday, Tuesday etc.
getDayOfYear() – Returns the Day of the year. From 1 to 365, 366 when Leap year.
getHour() – Hour of the Day, 0 to 23.
getMinute() – Minute of the Hour, 0 to 59.
getSecond() – Second of the Minute, 0 to 59.
getNano() – Nano-of-second, from 0 to 999,999,999.
getYear() – Returns the Year
getMonth() – Returns Month such as January, February etc.
getDayOfMonth() – Returns Day of the Month, 1 to 31.
import java.time.ZonedDateTime;
public class Example{
public static void main(String[] args) {
//current date time with default zone id
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("getDayOfWeek(): "+zonedDateTime.getDayOfWeek());
System.out.println("getDayOfYear(): "+zonedDateTime.getDayOfYear());
System.out.println("getHour(): "+zonedDateTime.getHour());
System.out.println("getMinute(): "+zonedDateTime.getMinute());
System.out.println("getSecond(): "+zonedDateTime.getSecond());
System.out.println("getNano(): "+zonedDateTime.getNano());
System.out.println("getYear(): "+zonedDateTime.getYear());
System.out.println("getMonth(): "+zonedDateTime.getMonth());
System.out.println("getDayOfMonth(): "+zonedDateTime.getDayOfMonth());
}
}
Output:
getDayOfWeek(): TUESDAY getDayOfYear(): 304 getHour(): 20 getMinute(): 25 getSecond(): 45 getNano(): 814000000 getYear(): 2017 getMonth(): OCTOBER getDayOfMonth(): 31
4. Java ZonedDateTime plus() method
There are several plus methods in the ZonedDateTime class such as plusYears(), plusMonths(), plusDays(), plusHours(), plusMinutes(), plusSeconds(), plusNanos(). These methods are used for adding the specified date-time to this ZonedDateTime, however these methods may not give correct results in case of day-light savings. The better way for such calculation is to use plus() method and Period as shown in the following example.
import java.time.ZonedDateTime;
import java.time.Period;
public class Example{
public static void main(String[] args) {
//current date time with default zone id
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("current date: "+zonedDateTime);
System.out.println("plus 10 days: "+zonedDateTime.plus(Period.ofDays(10)));
System.out.println("plus 3 months: "+zonedDateTime.plus(Period.ofMonths(3)));
System.out.println("plus 2 years: "+zonedDateTime.plus(Period.ofYears(2)));
System.out.println("plus 1 week: "+zonedDateTime.plus(Period.ofWeeks(1)));
}
}
Output:
current date: 2017-11-01T16:18:10.076+05:30[Asia/Kolkata] plus 10 days: 2017-11-11T16:18:10.076+05:30[Asia/Kolkata] plus 3 months: 2018-02-01T16:18:10.076+05:30[Asia/Kolkata] plus 2 years: 2019-11-01T16:18:10.076+05:30[Asia/Kolkata] plus 1 week: 2017-11-08T16:18:10.076+05:30[Asia/Kolkata]
5. ZonedDateTime minus() method example
Similar to the plus methods, we have several minus methods such as minusYears(), minusMonths(), minusDays(), minusHours(), minusMinutes(), minusSeconds(), minusNanos(). For the correct calculations during day light savings use the plus() method with the Period instance as shown below:
import java.time.ZonedDateTime;
import java.time.Period;
public class Example{
public static void main(String[] args) {
//current date time with default zone id
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("current date: "+zonedDateTime);
System.out.println("Minus 5 days: "+zonedDateTime.minus(Period.ofDays(5)));
System.out.println("Minus 1 month: "+zonedDateTime.minus(Period.ofMonths(1)));
System.out.println("Minus 4 years: "+zonedDateTime.minus(Period.ofYears(4)));
System.out.println("Minus 2 weeks: "+zonedDateTime.minus(Period.ofWeeks(2)));
}
}
Output:
current date: 2017-11-01T16:25:46.748+05:30[Asia/Kolkata] Minus 5 days: 2017-10-27T16:25:46.748+05:30[Asia/Kolkata] Minus 1 month: 2017-10-01T16:25:46.748+05:30[Asia/Kolkata] Minus 4 years: 2013-11-01T16:25:46.748+05:30[Asia/Kolkata] Minus 2 weeks: 2017-10-18T16:25:46.748+05:30[Asia/Kolkata]
6. Java Example of getting the current Zone
In the following example, we are using getZone() method to find the time-zone.
import java.time.ZonedDateTime;
public class Example{
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Current Zone: "+zonedDateTime.getZone());
}
}
Output:
Current Zone: Asia/Kolkata
Leave a Reply