2 *******************************************************************************
3 * Copyright (C) 2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.samples.util.timezone;
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;
21 * com.ibm.icu.util.BasicTimeZone Coding Examples
23 public class BasicTimeZoneExample {
24 public static void main(String... args) {
25 nextTransitionExample();
26 previousTransitionExample();
27 timeZoneRulesExample();
28 equivalentTransitionsExample();
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");
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);
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);
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 */);
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());
52 // Update start time for next transition
55 // ---getNextTransitionExample
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");
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);
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);
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 */);
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());
79 // Update start time for next transition
82 // ---getPreviousTransitionExample
85 public static void timeZoneRulesExample() {
86 // ---getTimeZoneRulesExample
87 System.out.println("### Extracts time zone rules used by America/Los_Angeles since year 2005");
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]);
97 // ---getTimeZoneRulesExample
100 public static void equivalentTransitionsExample() {
101 // ---hasEquivalentTransitionsExample
102 System.out.println("### Compare America/New_York and America/Detroit since year 1970");
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);
108 GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
110 // Compare these time zones every 10 years since year 1970 up to year 2009
111 for (int startYear = 1970; startYear <= 2000; startYear += 10) {
114 cal.set(startYear, Calendar.JANUARY, 1, 0, 0, 0);
115 cal.set(Calendar.MILLISECOND, 0);
116 start = cal.getTimeInMillis();
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;
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);
127 // ---hasEquivalentTransitionsExample