December 31, 2022

ZonedDateTime in Java With Examples

The java.time.ZonedDateTime class is part of new date and time API added in Java 8 that represents a date-time with a time-zone in the ISO-8601 calendar system, such as 2019-11-02T09:27:07+05:30[Asia/Calcutta].

Java ZonedDateTime class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset. This class is immutable thus thread-safe. Since it is marked as final so can't be extended. In this post we’ll see some examples demonstrating usage of Java ZonedDateTime class.

Creating instances of ZonedDateTime

ZonedDateTime class in Java doesn't have any public constructors to obtain an instance, you will use a factory method to get an instance.

1. Using now() method you can obtain the current date-time from the system clock in the default time-zone.

ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt);//2019-11-02T09:27:07.083270100+05:30[Asia/Calcutta]

You can also pass a ZoneId to obtain the current date-time from the system clock in the specified time-zone.

ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(zdt);//2019-11-02T05:05:31.583917800+01:00[Europe/Paris]

2. You can obtain an instance of ZonedDateTime using of() method by passing a year, month, day, hour, minute, second, nanosecond and time-zone.

ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
System.out.println(zdt);//2019-10-25T15:10:21.000000252-05:00[America/Chicago]
You can also pass instances of LocalDate, LocalTime and ZoneId to get a ZonedDateTime
of(LocalDate date, LocalTime time, ZoneId zone)

You can also pass instance of LocalDateTime and ZoneId to get a ZonedDateTime

of(LocalDateTime localDateTime, ZoneId zone)

Formatting ZonedDateTime (Converting to String)

For converting ZonedDateTime to String you can use DateTimeFormatter class which specifies the pattern for conversion.

public class FormatDate {
  public static void main(String[] args) {	
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    // Z - Time Zone offset
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a Z");
    System.out.println(zdt.format(dtf));
    
    // z - time zone name
    dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z");
    System.out.println(zdt.format(dtf));
    
    //V - time-zone ID
    dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss VV");
    System.out.println(zdt.format(dtf)); 
  }
}
Output
2019-10-25 03:10:21 PM -0500
10/25/2019 15:10:21 GMT-05:00
10/25/2019 - 15:10:21 America/Chicago

Converting String to ZonedDateTime

Using parse() method you can convert String to ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {	
    String dateWithZone = "10/25/2019 15:10:21 GMT-05:00";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z");
    ZonedDateTime zdt = ZonedDateTime.parse(dateWithZone, dtf);
    System.out.println(zdt); 
  }
}
Output
2019-10-25T15:10:21-05:00[GMT-05:00]

Getting date, time and zone values from ZonedDateTime

ZonedDateTime class has methods to get year, month, day, hour, minute, second values as well as zone information.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    
    System.out.println("Year- " + zdt.getYear());
    System.out.println("Month- " + zdt.getMonthValue());
    System.out.println("Day- " + zdt.getDayOfMonth());
    
    System.out.println("Hour- " + zdt.getHour());
    System.out.println("Minute- " + zdt.getMinute());
    System.out.println("Second- " + zdt.getSecond());
    System.out.println("NanoSecond- " + zdt.getNano());
    
    System.out.println("Zone- " + zdt.getZone()); 
  }
}
Output
Year- 2019
Month- 10
Day- 25
Hour- 15
Minute- 10
Second- 21
NanoSecond- 252
Zone- America/Chicago

Adding or subtracting years, months, days to ZonedDateTime

There are methods to add or subtract days, months and years from a Java ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    System.out.println("Year after subtraction- " + zdt.minusYears(2));
    System.out.println("Month after subtraction- " + zdt.minusMonths(4));
    System.out.println("Day after subtraction- " + zdt.minusDays(35));

    System.out.println("Year after addition- " + zdt.plusYears(2));
    System.out.println("Month after addition- " + zdt.plusMonths(4));
    System.out.println("Day after addition- " + zdt.plusDays(35));
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Year after subtraction- 2017-10-25T15:10:21.000000252-05:00[America/Chicago]
Month after subtraction- 2019-06-25T15:10:21.000000252-05:00[America/Chicago]
Day after subtraction- 2019-09-20T15:10:21.000000252-05:00[America/Chicago]
Year after addition- 2021-10-25T15:10:21.000000252-05:00[America/Chicago]
Month after addition- 2020-02-25T15:10:21.000000252-06:00[America/Chicago]
Day after addition- 2019-11-29T15:10:21.000000252-06:00[America/Chicago]

Adding or subtracting hours, minute, second to ZonedDateTime

There are methods to add or subtract hours, minutes, seconds, nano seconds from a ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    System.out.println("Hour after subtraction- " + zdt.minusHours(2));
    System.out.println("Minute after subtraction- " + zdt.minusMinutes(25));
    System.out.println("Second after subtraction- " + zdt.minusSeconds(35));

    System.out.println("Hour after addition- " + zdt.plusHours(2));
    System.out.println("Minute after addition- " + zdt.plusMinutes(4));
    System.out.println("Second after addition- " + zdt.plusSeconds(35));
    System.out.println("NanoSecond after addition- " + zdt.plusNanos(250));
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Hour after subtraction- 2019-10-25T13:10:21.000000252-05:00[America/Chicago]
Minute after subtraction- 2019-10-25T14:45:21.000000252-05:00[America/Chicago]
Second after subtraction- 2019-10-25T15:09:46.000000252-05:00[America/Chicago]
Hour after addition- 2019-10-25T17:10:21.000000252-05:00[America/Chicago]
Minute after addition- 2019-10-25T15:14:21.000000252-05:00[America/Chicago]
Second after addition- 2019-10-25T15:10:56.000000252-05:00[America/Chicago]
NanoSecond after addition- 2019-10-25T15:10:21.000000502-05:00[America/Chicago]

Getting LocalDateTime, LocalDate, LocalTime from ZonedDateTime

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    LocalDateTime ldt = zdt.toLocalDateTime();
    System.out.println("Extracted LocalDateTime: " + ldt); 

    LocalDate ld = zdt.toLocalDate();
    System.out.println("Extracted LocalDate: " + ld);

    LocalTime lt = zdt.toLocalTime();
    System.out.println("Extracted LocalTime: " + lt); 
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Extracted LocalDateTime: 2019-10-25T15:10:21.000000252
Extracted LocalDate: 2019-10-25
Extracted LocalTime: 15:10:21.000000252

Comparing ZonedDateTimes in Java

For comparing two ZonedDateTime instances there are the following methods-

  • compareTo(ChronoLocalDateTime<?> other)- Compares this date-time to another date-time. Returns negative value if less than tha passed LocalDateTime instance, positive if greater.
  • isAfter(ChronoLocalDateTime<?> other)- Checks if this date-time is after the specified date-time.
  • isBefore(ChronoLocalDateTime<?> other)- Checks if this date-time is before the specified date-time.
  • isEqual(ChronoLocalDateTime<?> other)- Checks if this date-time is equal to the specified date-time.
public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt1 = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    ZonedDateTime zdt2 = ZonedDateTime.of(2018, 8, 5, 4, 15, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time1: " + zdt1); 
    System.out.println("Created Zoned Date-Time2: " + zdt2); 


    System.out.println(zdt1.compareTo(zdt2));
    System.out.println(zdt2.compareTo(zdt1));

    System.out.println(zdt1.isAfter(zdt2));
    System.out.println(zdt1.isBefore(zdt2));
    System.out.println(zdt1.isEqual(zdt2));
  }
}
Output
Created Zoned Date-Time1: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Created Zoned Date-Time2: 2018-08-05T04:15:21.000000252-05:00[America/Chicago]
1
-1
true
false
false

That's all for the topic ZonedDateTime in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment