001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.example.time;
017:
018: import java.io.PrintStream;
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.joda.time.*;
026: import org.joda.time.format.*;
027:
028: /**
029: * Prints out all available time zones to standard out in an HTML table.
030: *
031: * @author Brian S O'Neill
032: */
033: public class TimeZoneTable {
034: static final long cNow = System.currentTimeMillis();
035:
036: static final DateTimeFormatter cOffsetFormatter = new DateTimeFormatterBuilder()
037: .appendTimeZoneOffset(null, true, 2, 4).toFormatter();
038:
039: public static void main(String[] args) throws Exception {
040: Set idSet = DateTimeZone.getAvailableIDs();
041: ZoneData[] zones = new ZoneData[idSet.size()];
042:
043: {
044: Iterator it = idSet.iterator();
045: int i = 0;
046: while (it.hasNext()) {
047: String id = (String) it.next();
048: zones[i++] = new ZoneData(id, DateTimeZone.forID(id));
049: }
050: Arrays.sort(zones);
051: }
052:
053: PrintStream out = System.out;
054:
055: out.println("<table>");
056:
057: out.println("<tr>" + "<th align=\"left\">Standard Offset</th>"
058: + "<th align=\"left\">Canonical ID</th>"
059: + "<th align=\"left\">Aliases</th>" + "</tr>");
060:
061: ZoneData canonical = null;
062: List aliases = new ArrayList();
063:
064: for (int i = 0; i < zones.length; i++) {
065: ZoneData zone = zones[i];
066:
067: if (!zone.isCanonical()) {
068: aliases.add(zone);
069: continue;
070: }
071:
072: if (canonical != null) {
073: printRow(out, canonical, aliases);
074: }
075:
076: canonical = zone;
077: aliases.clear();
078: }
079:
080: if (canonical != null) {
081: printRow(out, canonical, aliases);
082: }
083:
084: out.println("</table>");
085: }
086:
087: private static void printRow(PrintStream out, ZoneData zone,
088: List aliases) {
089: out.print("<tr>");
090:
091: out.print("<td align=\"left\" valign=\"top\">");
092: out.print(zone.getStandardOffsetStr());
093: out.print("</td>");
094:
095: out.print("<td align=\"left\" valign=\"top\">");
096: out.print(zone.getCanonicalID());
097: out.print("</td>");
098:
099: out.print("<td align=\"left\" valign=\"top\">");
100: if (aliases.size() > 0) {
101: for (int j = 0; j < aliases.size(); j++) {
102: if (j > 0) {
103: out.print(", ");
104: }
105: out.print(((ZoneData) aliases.get(j)).getID());
106: }
107: }
108: out.print("</td>");
109:
110: out.println("</tr>");
111: }
112:
113: private static class ZoneData implements Comparable {
114: private final String iID;
115: private final DateTimeZone iZone;
116:
117: ZoneData(String id, DateTimeZone zone) {
118: iID = id;
119: iZone = zone;
120: }
121:
122: public String getID() {
123: return iID;
124: }
125:
126: public String getCanonicalID() {
127: return iZone.getID();
128: }
129:
130: public boolean isCanonical() {
131: return getID().equals(getCanonicalID());
132: }
133:
134: public String getStandardOffsetStr() {
135: long millis = cNow;
136: while (iZone.getOffset(millis) != iZone
137: .getStandardOffset(millis)) {
138: millis = iZone.nextTransition(millis);
139: }
140: return cOffsetFormatter.withZone(iZone).print(millis);
141: }
142:
143: public int compareTo(Object obj) {
144: ZoneData other = (ZoneData) obj;
145:
146: int offsetA = iZone.getStandardOffset(cNow);
147: int offsetB = other.iZone.getStandardOffset(cNow);
148:
149: if (offsetA < offsetB) {
150: return -1;
151: }
152: if (offsetA > offsetB) {
153: return 1;
154: }
155:
156: int result = getCanonicalID().compareTo(
157: 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: }
|