Java 8 – Compare ZonedDateTime

Learn to compare two instances of ZonedDateTime either in same timezone or in different timezones in Java 8.

1. ZonedDateTime comparison in different timezones

If both instances of ZonedDateTime are in different timezones then we can use the ZonedDateTime.compareTo() method to compare both instances, including the chronology.

It returns a value negative if less, positive if greater and 0 if both date-time instances are equal.

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main 
{
    public static void main(String[] args)
    {
        ZonedDateTime zoneddatetime1 = ZonedDateTime.now();

        System.out.println("zoneddatetime1 in [Asia/Calcutta] - " + zoneddatetime1);
        
        ZonedDateTime zoneddatetime2 
                    = ZonedDateTime.parse("2019-04-03T11:21:12.123+05:30[UTC]");

        System.out.println("zoneddatetime2 in [UTC] - " + zoneddatetime2);
        System.out.println("zoneddatetime2 in [Asia/Calcutta] - " + zoneddatetime2.withZoneSameInstant(ZoneId.of("Asia/Calcutta")));
        
        long difference = zoneddatetime1.compareTo(zoneddatetime2);
        
        if(difference > 0) {
            System.out.println("zoneddatetime1 > zoneddatetime2");
        } else if(difference < 0) {
            System.out.println("zoneddatetime1 < zoneddatetime2");
        } else {
            System.out.println("\"zoneddatetime1 = zoneddatetime2");
        }
    }
}

Program output.

zoneddatetime1 in [Asia/Calcutta] - 2019-04-03T13:01:14.745+05:30[Asia/Calcutta]
zoneddatetime2 in [UTC] 		  - 2019-04-03T11:21:12.123Z[UTC]
zoneddatetime2 in [Asia/Calcutta] - 2019-04-03T16:51:12.123+05:30[Asia/Calcutta]
zoneddatetime1 < zoneddatetime2

2. Compare ZonedDateTime in similar timezone

If both instances of ZonedDateTime are in same timezone then we can use these methods to compare date-time values.

import java.time.ZonedDateTime;

public class Main 
{
    public static void main(String[] args)
    {
        ZonedDateTime zoneddatetime1 = 
                ZonedDateTime.parse("2019-04-03T13:01:14.745+05:30[Asia/Calcutta]");
        
        ZonedDateTime zoneddatetime2 = 
                ZonedDateTime.parse("2019-04-03T12:11:11.745+05:30[Asia/Calcutta]");
        
        System.out.println(zoneddatetime1.isAfter(zoneddatetime2));     //true
        
        System.out.println(zoneddatetime1.isBefore(zoneddatetime2));    //false
        
        System.out.println(zoneddatetime1.isEqual(zoneddatetime2));     //false
    }
}

Program output.

true
false
false

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java 8 – Compare ZonedDateTime”

  1. Thanks for the article, I was looking for this.

    I think you have a typo in the first paragraph – ‘not’ is missing.
    ‘1. ZonedDateTime comparison in different timezones
    If both instances of ZonedDateTime are NOT in same timezone…’

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.