Search code examples
javatimezonecomparisoncomparator

How to compare TimeZones


I need to compare time zones such that Asia/Singapore < UTC < Pacific/Honolulu.

I'm working with java.util.TimeZone (which doesn't implement Comparable).

My search for an existing implementation was unsuccessful because of the overwhelming number of questions about comparing dates with different time zones.

Question: What is a correct implementation of Comparator<TimeZone> that will solve this problem (and what makes it better than other solutions, if applicable)?

Note that I'm not able to use Joda Time for this problem, so "use Joda Time" is not a valid answer.

Edit for clarity

The < notation above was not well defined. My particular use case only requires a naive "geographical" ordering from east to west. As the comments have pointed out, a more advanced and generalizable solution would take into account temporal factors like daylight savings time and historical GMT offset changes. So I think there are two orderings we can consider, each requiring a different Comparator<TimeZone> implementation:

  • Strictly geographical (current UTC) - addressed by my answer.
  • Sensitive to local or civil time changes - addressed by rgettman's answer.

Solution

  • One might be able to create a Comparator<TimeZone> that takes into account time zone differences. The TimeZone may or may not obvserve daylight savings time, which would adjust the raw offset, thus messing up raw-offset-only comparisons. The TimeZone class seems to support the adjustment based on the 2 getOffset methods, but they need a reference date. How about:

    public class TimeZoneComparator implements Comparator<TimeZone>
    {
       private long date;
    
       public TimeZoneComparator(long date)
       {
          this.date = date;
       }
    
       public int compare(TimeZone tz1, TimeZone tz2)
       {
          return tz2.getOffset(this.date) - tz2.getOffset(this.date);
       }
    }