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