001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Comparator;
018    import java.util.TimeZone;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
023    public class TimeZoneComparator implements Comparator<TimeZone> {
024    
025            public TimeZoneComparator() {
026            }
027    
028            @Override
029            public int compare(TimeZone timeZone1, TimeZone timeZone2) {
030                    long currentTime = System.currentTimeMillis();
031    
032                    Integer totalOffset1 = timeZone1.getOffset(currentTime);
033                    Integer totalOffset2 = timeZone2.getOffset(currentTime);
034    
035                    int value = totalOffset1.compareTo(totalOffset2);
036    
037                    if (value == 0) {
038                            String timeZoneId1 = timeZone1.getID();
039                            String timeZoneId2 = timeZone2.getID();
040    
041                            value = timeZoneId1.compareTo(timeZoneId2);
042                    }
043    
044                    return value;
045            }
046    
047    }