The ZonedDateTime class in Java 8 represents the date with time and timezone information such as 2017-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.
Lets take a simple example of ZonedDateTime.
1. Simple Example of ZonedDateTime in Java
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
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 amount to the 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 example 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("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 like this:
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
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