Let’s see some examples with Java’s ZonedDateTime class. First to understand different java date and time classes from the Java 8 new Date time API lets look at them:
This answer from stackoverflow point us to:
- LocalDate – date only
- LocalTime – time only
- LocalDateTime – date + time
- OffsetTime – time + offset
- OffsetDateTime – date + time + offset
- ZonedDateTime – date + time + offset + zone
So if we need automatic calculation of the zones and also differences between summer and winter time we can use ZonedDateTime.
First let’s create two dates that are in the same timezone but with different times. Actually one of time is today (dateTimeSystemTimezone) 2023-03-18 15:35:56 and the other 30 days from now (dateTimePlusDays) 2023-04-17 15:35:56. We can see that although they are in the same timezone [Europe/Paris] they end up with different hour difference to the UTC because dateTimeSystemTimezone is in the winter time and the dateTimePlusDays is in the summer time.
dateTimeSystemTimezone: 2023-03-18T15:35:56.142895327+01:00[Europe/Paris]
dateTimePlusDays: 2023-04-17T15:35:56.142895327+02:00[Europe/Paris]
Let’s also create two different zoned date times in different zones. zonedDateTimeRome and also zonedDateTimeSaoPaulo. If we know that airplane travel is 12 hours and 15 min we can calculate for a given time in Rome when we will arrive and what it will be the time in São Paulo.
Then we have example comparing only the date part of ZonedDateTime creating LocalDate.
Last example is comparing ZonedDateTime from different time zones with Instant class which we create from specific zoned date time objects.
OK Lets see now the Java example:
package org.example; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) { // ---------- Difference between summer and wither times ---------- // Creating date time with the default (system) timezone ZonedDateTime dateTimeSystemTimezone = ZonedDateTime.now(); // 2023-03-18T15:35:56.142895327+01:00[Europe/Paris] // In this example We are in the winter time. // That's why we have +01:00 System.out.println("dateTimeSystemTimezone: " + dateTimeSystemTimezone); // Create new ZonedDateTime with 30 days from now // Here we can see that we change to the summer time. // Relative to UTC our time zone go from +1 to +2 hours ZonedDateTime dateTimePlusDays = dateTimeSystemTimezone.plusDays(30); System.out.println("dateTimePlusDays: " + dateTimePlusDays); // ------ Comparing ZonedDateTimes --------------------- ZonedDateTime zonedDateTimeRome = ZonedDateTime.of(2023, 4, 10, 20, 35, 10, 0, ZoneId.of("Europe/Rome")); ZonedDateTime zonedDateTimeSaoPaulo = ZonedDateTime.of(2023, 4, 10, 20, 35, 10, 0, ZoneId.of("America/Sao_Paulo")); // Even if the date and time is the same the two zoned date times are different // because they are in different timezones System.out.println("zonedDateTimeRome: " + zonedDateTimeRome + " zonedDateTimeSaoPaulo: " + zonedDateTimeSaoPaulo); System.out.println("zonedDateTimeRome == zonedDateTimeSaoPaulo: " + zonedDateTimeRome.equals(zonedDateTimeSaoPaulo)); // --------- Travel from Rome to São Paulo ------------------- // Let's see if we take airplane and travel 12h and 15 min from // Rome to São Paulo for a given date at what time we will arrive ZonedDateTime zonedDateTimeSaoPauloArrive = zonedDateTimeRome .plusHours(12).plusMinutes(15) .withZoneSameInstant(ZoneId.of("America/Sao_Paulo")); System.out.println("zonedDateTimeRome: " + zonedDateTimeRome); System.out.println("zonedDateTimeSaoPauloArrive: " + zonedDateTimeSaoPauloArrive); // ----------- Compare only dates ---------------- // If we want to compare only dates we can use LocalDate LocalDate dateRome = zonedDateTimeRome.toLocalDate(); LocalDate dateSaoPaulo = zonedDateTimeSaoPaulo.toLocalDate(); System.out.println("dateRome: " + dateRome + " dateSaoPaulo: " + dateSaoPaulo); // here we see that they are the same System.out.println("dateRome == dateSaoPaulo: " + dateRome.equals(dateSaoPaulo)); // ----------- compare two ZonedDateTimes with Instant ----- // if we want to compare two ZonedDateTimes with different zones we can use Instant Instant instantFromRomeDateTime = zonedDateTimeRome.toInstant(); Instant instantFromSaoPauloDateTime = zonedDateTimeSaoPaulo.toInstant(); System.out.println("instantFromRomeDateTime: " + instantFromRomeDateTime); System.out.println("instantFromSaoPauloDateTime: " + instantFromSaoPauloDateTime); System.out.println("instantFromRomeDateTime == instantFromSaoPauloDateTime: " + instantFromRomeDateTime.equals(instantFromSaoPauloDateTime)); } }
The result in the output:
dateTimeSystemTimezone: 2023-03-18T15:35:56.142895327+01:00[Europe/Paris]
dateTimePlusDays: 2023-04-17T15:35:56.142895327+02:00[Europe/Paris]
zonedDateTimeRome: 2023-04-10T20:35:10+02:00[Europe/Rome] zonedDateTimeSaoPaulo: 2023-04-10T20:35:10-03:00[America/Sao_Paulo]
zonedDateTimeRome == zonedDateTimeSaoPaulo: false
zonedDateTimeRome: 2023-04-10T20:35:10+02:00[Europe/Rome]
zonedDateTimeSaoPauloArrive: 2023-04-11T03:50:10-03:00[America/Sao_Paulo]
dateRome: 2023-04-10 dateSaoPaulo: 2023-04-10
dateRome == dateSaoPaulo: true
instantFromRomeDateTime: 2023-04-10T18:35:10Z
instantFromSaoPauloDateTime: 2023-04-10T23:35:10Z
instantFromRomeDateTime == instantFromSaoPauloDateTime: false