KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > example > time > TimeZoneTable


1 /*
2  * Copyright 2001-2005 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.example.time;
17
18 import java.io.PrintStream JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import org.joda.time.*;
26 import org.joda.time.format.*;
27
28 /**
29  * Prints out all available time zones to standard out in an HTML table.
30  *
31  * @author Brian S O'Neill
32  */

33 public class TimeZoneTable {
34     static final long cNow = System.currentTimeMillis();
35
36     static final DateTimeFormatter cOffsetFormatter = new DateTimeFormatterBuilder()
37         .appendTimeZoneOffset(null, true, 2, 4)
38         .toFormatter();
39
40     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
41         Set JavaDoc idSet = DateTimeZone.getAvailableIDs();
42         ZoneData[] zones = new ZoneData[idSet.size()];
43         
44         {
45             Iterator JavaDoc it = idSet.iterator();
46             int i = 0;
47             while (it.hasNext()) {
48                 String JavaDoc id = (String JavaDoc) it.next();
49                 zones[i++] = new ZoneData(id, DateTimeZone.forID(id));
50             }
51             Arrays.sort(zones);
52         }
53
54         PrintStream JavaDoc out = System.out;
55
56         out.println("<table>");
57
58         out.println("<tr>" +
59                     "<th align=\"left\">Standard Offset</th>" +
60                     "<th align=\"left\">Canonical ID</th>" +
61                     "<th align=\"left\">Aliases</th>" +
62                     "</tr>");
63
64         ZoneData canonical = null;
65         List JavaDoc aliases = new ArrayList JavaDoc();
66
67         for (int i=0; i<zones.length; i++) {
68             ZoneData zone = zones[i];
69
70             if (!zone.isCanonical()) {
71                 aliases.add(zone);
72                 continue;
73             }
74
75             if (canonical != null) {
76                 printRow(out, canonical, aliases);
77             }
78
79             canonical = zone;
80             aliases.clear();
81         }
82
83         if (canonical != null) {
84             printRow(out, canonical, aliases);
85         }
86
87         out.println("</table>");
88     }
89
90     private static void printRow(PrintStream JavaDoc out, ZoneData zone, List JavaDoc aliases) {
91         out.print("<tr>");
92                 
93         out.print("<td align=\"left\" valign=\"top\">");
94         out.print(zone.getStandardOffsetStr());
95         out.print("</td>");
96         
97         out.print("<td align=\"left\" valign=\"top\">");
98         out.print(zone.getCanonicalID());
99         out.print("</td>");
100         
101         out.print("<td align=\"left\" valign=\"top\">");
102         if (aliases.size() > 0) {
103             for (int j=0; j<aliases.size(); j++) {
104                 if (j > 0) {
105                     out.print(", ");
106                 }
107                 out.print(((ZoneData) aliases.get(j)).getID());
108             }
109         }
110         out.print("</td>");
111         
112         out.println("</tr>");
113     }
114
115     private static class ZoneData implements Comparable JavaDoc {
116         private final String JavaDoc iID;
117         private final DateTimeZone iZone;
118         
119         ZoneData(String JavaDoc id, DateTimeZone zone) {
120             iID = id;
121             iZone = zone;
122         }
123
124         public String JavaDoc getID() {
125             return iID;
126         }
127
128         public String JavaDoc getCanonicalID() {
129             return iZone.getID();
130         }
131
132         public boolean isCanonical() {
133             return getID().equals(getCanonicalID());
134         }
135
136         public String JavaDoc getStandardOffsetStr() {
137             long millis = cNow;
138             while (iZone.getOffset(millis) != iZone.getStandardOffset(millis)) {
139                 millis = iZone.nextTransition(millis);
140             }
141             return cOffsetFormatter.withZone(iZone).print(millis);
142         }
143
144         public int compareTo(Object JavaDoc obj) {
145             ZoneData other = (ZoneData) obj;
146
147             int offsetA = iZone.getStandardOffset(cNow);
148             int offsetB = other.iZone.getStandardOffset(cNow);
149
150             if (offsetA < offsetB) {
151                 return -1;
152             }
153             if (offsetA > offsetB) {
154                 return 1;
155             }
156
157             int result = getCanonicalID().compareTo(other.getCanonicalID());
158
159             if (result != 0) {
160                 return result;
161             }
162
163             if (isCanonical()) {
164                 if (!other.isCanonical()) {
165                     return -1;
166                 }
167             } else if (other.isCanonical()) {
168                 return 1;
169             }
170
171             return getID().compareTo(other.getID());
172         }
173     }
174 }
175
Popular Tags