API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.utils. JOTTimezoneUtils View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

/*
 * JavaOnTrack/Jotwiki
 * Thibaut Colar.
 * tcolar-jot AT colar  DOT net
 */
package net.jot.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.TimeZone;
import java.util.Vector;

/**
 * Provides utilities for working with times and converting timezones.
 * @author thibautc
 */
public class JOTTimezoneUtils {
    /**EEE dd MMM yyyy HH:mm z */
    public final static String FORMAT_STD = "EEE dd MMM yyyy HH:mm z";
    /**MM-dd-yyyy HH:mm */
    public final static String FORMAT_DB = "MM-dd-yyyy HH:mm";
    /**EEE, dd MMM yyyy HH:MM:SS*/
    public final static String FORMAT_HEADER = "EEE, dd MMM yyyy HH:mm:ss";


    static Hashtable commonZones = new Hashtable();
    public final static String AMERICA_ID = "America";
    public final static String EUROPE_ID = "Europe";
    public final static String ASIA_ID = "Asia";
    public final static String PACIFIC_ID = "Pacific";
    public final static String AFRICA_ID = "Africa";
    public final static String OTHERS_ID = "Others";

    public static String[] getTimezoneList()
    {
        return TimeZone.getAvailableIDs();
    }

    /**
     * Get the timezone list for a specific offset from UTC(in hours)
     * @param hoursOffset
     * @return
     */
    public static String[] getTimezoneList(int hoursOffset)
    {
        ArrayList list = new ArrayList();
        list.add("");
        String[] s1 = TimeZone.getAvailableIDs(hoursOffset * 3600000);
        for (int i = 0; i != s1.length; i++)
        {
            list.add(s1[i]);
        }
        return (String[]) list.toArray(new String[0]);
    }

    /**
     * Add possible client list from it's current GMT offset in hours
     * We add the xones for the current offest and also the ones for offset-1 and offset+1 because
     * we have no way to know (or at leats that i know of) wether the client is currently in Daylight savings or not
     * by adding the 1hour before and after timezones this should cover the potential offsets at those times of the year
     * where the client is in DST but GMT is not yet.
     * Ex: client in the US during late march is -7 vs GMT(not swicthed to summer time yet) rather than usual -8.
     * @param hoursOffset
     * @return
     */
    public static ArrayList getPossibleUserTimezoneList(int userOffsetInHours)
    {
        ArrayList list = new ArrayList();
        String[] s1 = TimeZone.getAvailableIDs(userOffsetInHours * 3600000);
        for (int i = 0; i != s1.length; i++)
        {
            list.add(s1[i]);
        }
        s1 = TimeZone.getAvailableIDs((userOffsetInHours - 1) * 3600000);
        for (int i = 0; i != s1.length; i++)
        {
            list.add(s1[i]);
        }
        s1 = TimeZone.getAvailableIDs((userOffsetInHours + 1) * 3600000);
        for (int i = 0; i != s1.length; i++)
        {
            list.add(s1[i]);
        }
        return list;
    }

    public static String[] getCommonTimezoneList(String zoneId)
    {
        String[] result = null;
        if (commonZones.get(zoneId) == null)
        {
            synchronized (TimeZone.class)
            {
                Vector v = new Vector();
                v.add("");
                String[] zones = TimeZone.getAvailableIDs();
                for (int i = 0; i != zones.length; i++)
                {
                    if (zoneId.equals(AMERICA_ID) && (zones[i].startsWith("US/") || zones[i].startsWith("America/") || zones[i].startsWith("Canada/") || zones[i].startsWith("Mexico/")))
                    {
                        v.add(zones[i]);
                    } else if (zoneId.equals(PACIFIC_ID) && (zones[i].startsWith("Australia/") || zones[i].startsWith("Pacific/")))
                    {
                        v.add(zones[i]);
                    } else if (zoneId.equals(ASIA_ID) && (zones[i].startsWith("Asia/")))
                    {
                        v.add(zones[i]);
                    } else if (zoneId.equals(EUROPE_ID) && (zones[i].startsWith("Europe/")))
                    {
                        v.add(zones[i]);
                    } else if (zoneId.equals(AFRICA_ID) && (zones[i].startsWith("Africa/")))
                    {
                        v.add(zones[i]);
                    } else if (zoneId.equals(OTHERS_ID) && !(zones[i].startsWith("Africa/") || zones[i].startsWith("Europe/") || zones[i].startsWith("Asia/") || zones[i].startsWith("Australia/") || zones[i].startsWith("Pacific/") || zones[i].startsWith("US/") || zones[i].startsWith("America/") || zones[i].startsWith("Canada/") || zones[i].startsWith("Mexico/")))
                    {
                        v.add(zones[i]);
                    }
                }
                result = (String[]) v.toArray(new String[0]);
                commonZones.put(zoneId, result);
            }
        }
        return (String[])commonZones.get(zoneId);
    }

        /**
     * Converts a date into another timezone.
     * TODO: not sure that works - needs testing.
     * @param date
     * @param destZone
     * @param dbFormat
     * @return
     */
    public static String convertTimezone(Date date, String destZone, String dateFormat)
    {
        DateFormat df = new SimpleDateFormat(dateFormat);
        df.setTimeZone(TimeZone.getTimeZone(destZone));
        return df.format(date);
    }


    public static void main(String args[])
    {
        System.out.println("====LOCAL====");
        String[] b = (String[]) getPossibleUserTimezoneList(-8).toArray(new String[0]);
        for (int i = 0; i != b.length; i++)
        {
            System.out.println("\t" + b[i].length());
        }

        System.out.println("====AMERICA====");
        String[] a = getCommonTimezoneList(AMERICA_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }
        System.out.println("====PACIFIC====");
        a = getCommonTimezoneList(PACIFIC_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }
        System.out.println("====EUROPE====");
        a = getCommonTimezoneList(EUROPE_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }
        System.out.println("====ASIA====");
        a = getCommonTimezoneList(ASIA_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }
        System.out.println("====AFRICA====");
        a = getCommonTimezoneList(AFRICA_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }
        System.out.println("===OTHERS====");
        a = getCommonTimezoneList(OTHERS_ID);
        for (int i = 0; i != a.length; i++)
        {
            System.out.println("\t" + a[i].length());
        }

    }
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar