All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.util.TimeZoneUtil Maven / Gradle / Ivy

/*
 * $Id: TimeZoneUtil.java 12345 2004-08-22 04:56:09Z fielding $
 *
 * Copyright 1997-2004 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */

package com.day.util;

import java.util.*;
import java.text.DecimalFormat;

/**
 * The TimeZoneUtil class provides convenience methods for handling
 * time zones in Java.
 *
 * @version $Revision: 1.3 $, $Date: 2004-08-22 06:56:09 +0200 (Sun, 22 Aug 2004) $
 * @author mreutegg
 * @since echidna
 * @audience wad
 */
public class TimeZoneUtil {

    private static TimeZone[] zones = null;

    private static final DecimalFormat offsetFormatter = new DecimalFormat("00.00");

    /**
     * Returns an array of TimeZone objects sorted in ascending order
     * by their UTC time offset.
     *
     * @return a sorted array of TimeZone objects.
     */
    public synchronized static TimeZone[] getAvailableZones() {
        if (zones == null) {
            String[] allZones = TimeZone.getAvailableIDs();
            // fill a Vector and ignore all three letter id's, but keep UTC
            Vector uniqueZones = new Vector();
            for (int i=0; i < allZones.length; i++) {
                // ignore all three letter id's, but keep UTC
                if (allZones[i].length() <= 3 && !allZones[i].equals("UTC")) continue;
                TimeZone zone = TimeZone.getTimeZone(allZones[i]);
                uniqueZones.add(zone);
            }
            zones = new TimeZone[0];
            zones = (TimeZone[])uniqueZones.toArray(zones);
            Arrays.sort(zones, new TimeZoneComparator());
        }
        // return a copy of the internal array
        TimeZone[] retVal = new TimeZone[zones.length];
        System.arraycopy(zones, 0, retVal, 0, zones.length);

        return retVal;
    }

    /**
     * Returns the TimeZone of the Communiqu? server.
     * @return the TimeZone of the Communiqu? server.
     */
    public static TimeZone getServerTimeZone() {
        return TimeZone.getDefault();
    }

    /**
     * Formats the passed TimeZone object to a String representation:
     * 

[<UTC offset>] <time zone name> (<short name>) *

Example: [UTC+01.00] Europe/Berlin (CEST) * @param zone The TimeZone to format * @return a String representation of the passed TimeZone. */ public static String getDisplayName(TimeZone zone) { StringBuffer buffer = new StringBuffer("[UTC"); if (zone.getRawOffset() >= 0) buffer.append("+"); buffer.append(offsetFormatter.format(((double)zone.getRawOffset())/(1000*60*60))); buffer.append("] ").append(zone.getID()); buffer.append(" (").append(zone.getDisplayName(true, TimeZone.SHORT)); buffer.append(")"); return buffer.toString(); } /** * Inner utility class for comparing two TimeZone objects regarding their UTC * offset. */ static class TimeZoneComparator implements Comparator { /** * Compares its two arguments (TimeZone objects) for order. * * @param o1 the first object to be compared. * @param o2 the second object to be compared. * @return a negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater than the * second. * @throws ClassCastException if the arguments' types prevent them from * being compared by this Comparator. */ public int compare(Object o1, Object o2) { int diff = ((TimeZone)o1).getRawOffset() - ((TimeZone)o2).getRawOffset(); return (diff != 0) ? diff : ((TimeZone)o1).getID().compareTo(((TimeZone)o2).getID()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy