]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/samples/src/com/ibm/icu/samples/util/timezone/BasicTimeZoneExample.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / samples / src / com / ibm / icu / samples / util / timezone / BasicTimeZoneExample.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2011, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.samples.util.timezone;
8
9 import java.util.Date;
10
11 import com.ibm.icu.text.SimpleDateFormat;
12 import com.ibm.icu.util.BasicTimeZone;
13 import com.ibm.icu.util.Calendar;
14 import com.ibm.icu.util.GregorianCalendar;
15 import com.ibm.icu.util.TimeZone;
16 import com.ibm.icu.util.TimeZoneRule;
17 import com.ibm.icu.util.TimeZoneTransition;
18 import com.ibm.icu.util.ULocale;
19
20 /**
21  * com.ibm.icu.util.BasicTimeZone Coding Examples
22  */
23 public class BasicTimeZoneExample {
24     public static void main(String... args) {
25         nextTransitionExample();
26         previousTransitionExample();
27         timeZoneRulesExample();
28         equivalentTransitionsExample();
29     }
30
31     public static void nextTransitionExample() {
32         // ---getNextTransitionExample
33         System.out.println("### Iterates time zone transitions in America/Los_Angeles starting 2005-01-01 and forward");
34
35         // A TimeZone instance created by getTimeZone with TIMEZONE_ICU is always a BasicTimeZone
36         BasicTimeZone btz = (BasicTimeZone)TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
37
38         // Date format for the wall time
39         SimpleDateFormat wallTimeFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", ULocale.US);
40         wallTimeFmt.setTimeZone(btz);
41
42         long start = 1104537600000L;    // 2005-01-01 0:00 UTC
43         for (int i = 0; i < 5; i++) {   // Up to 5 transitions
44             TimeZoneTransition trans = btz.getNextTransition(start, false /* not including start time */);
45
46             // Display the transition time and offset information
47             long transTime = trans.getTime();
48             System.out.println(wallTimeFmt.format(new Date(transTime - 1)) + " -> " + wallTimeFmt.format(new Date(transTime)));
49             System.out.println(" - Before (Offset/Save): " + trans.getFrom().getRawOffset() + "/" + trans.getFrom().getDSTSavings());
50             System.out.println(" - After  (Offset/Save): " + trans.getTo().getRawOffset() + "/" + trans.getTo().getDSTSavings());
51
52             // Update start time for next transition
53             start = transTime;
54         }
55         // ---getNextTransitionExample
56     }
57
58     public static void previousTransitionExample() {
59         // ---getPreviousTransitionExample
60         System.out.println("### Iterates time zone transitions in America/Los_Angeles starting 2010-01-01 and backward");
61
62         // A TimeZone instance created by getTimeZone with TIMEZONE_ICU is always a BasicTimeZone
63         BasicTimeZone btz = (BasicTimeZone)TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
64
65         // Date format for the wall time
66         SimpleDateFormat wallTimeFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", ULocale.US);
67         wallTimeFmt.setTimeZone(btz);
68
69         long start = 1262304000000L;    // 2010-01-01 0:00 UTC
70         for (int i = 0; i < 5; i++) {   // Up to 5 transitions
71             TimeZoneTransition trans = btz.getPreviousTransition(start, false /* not including start time */);
72
73             // Display the transition time and offset information
74             long transTime = trans.getTime();
75             System.out.println(wallTimeFmt.format(new Date(transTime - 1)) + " -> " + wallTimeFmt.format(new Date(transTime)));
76             System.out.println(" - Before (Offset/Save): " + trans.getFrom().getRawOffset() + "/" + trans.getFrom().getDSTSavings());
77             System.out.println(" - After  (Offset/Save): " + trans.getTo().getRawOffset() + "/" + trans.getTo().getDSTSavings());
78
79             // Update start time for next transition
80             start = transTime;
81         }
82         // ---getPreviousTransitionExample
83     }
84
85     public static void timeZoneRulesExample() {
86         // ---getTimeZoneRulesExample
87         System.out.println("### Extracts time zone rules used by America/Los_Angeles since year 2005");
88
89         // A TimeZone instance created by getTimeZone with TIMEZONE_ICU is always a BasicTimeZone
90         BasicTimeZone btz = (BasicTimeZone)TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
91         long since = 1104537600000L;    // 2005-01-01 0:00 UTC
92         TimeZoneRule[] rules = btz.getTimeZoneRules(since);
93         System.out.println("Rule(initial): " + rules[0]);
94         for (int i = 1; i < rules.length; i++) {
95             System.out.println("Rule: " + rules[i]);
96         }
97         // ---getTimeZoneRulesExample
98     }
99
100     public static void equivalentTransitionsExample() {
101         // ---hasEquivalentTransitionsExample
102         System.out.println("### Compare America/New_York and America/Detroit since year 1970");
103
104         // A TimeZone instance created by getTimeZone with TIMEZONE_ICU is always a BasicTimeZone
105         BasicTimeZone tzNewYork = (BasicTimeZone)TimeZone.getTimeZone("America/New_York", TimeZone.TIMEZONE_ICU);
106         BasicTimeZone tzDetroit = (BasicTimeZone)TimeZone.getTimeZone("America/Detroit", TimeZone.TIMEZONE_ICU);
107
108         GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
109
110         // Compare these time zones every 10 years since year 1970 up to year 2009
111         for (int startYear = 1970; startYear <= 2000; startYear += 10) {
112             long start, end;
113
114             cal.set(startYear, Calendar.JANUARY, 1, 0, 0, 0);
115             cal.set(Calendar.MILLISECOND, 0);
116             start = cal.getTimeInMillis();
117
118             // Set the end time to the end of startYear + 9
119             int endYear = startYear + 9;
120             cal.set(endYear + 1, Calendar.JANUARY, 1, 0, 0, 0);
121             end = cal.getTimeInMillis() - 1;
122
123             // Check if these two zones have equivalent time zone transitions for the given time range
124             boolean isEquivalent = tzNewYork.hasEquivalentTransitions(tzDetroit, start, end);
125             System.out.println(startYear + "-" + endYear + ": " + isEquivalent);
126         }
127         // ---hasEquivalentTransitionsExample
128     }
129 }