KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > TempTest


1 /*
2  * Copyright 2001-2006 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.time;
17
18 import java.util.TimeZone JavaDoc;
19
20 /**
21  * Entry point for all tests in this package.
22  *
23  * @version $Revision: 1057 $ $Date: 2006-04-01 15:33:41 +0100 (Sat, 01 Apr
24  * 2006) $
25  *
26  * @author Stephen Colebourne
27  */

28 public class TempTest {
29
30     public static void main(String JavaDoc args[]) {
31         
32         DateTime a = new DateTime(1989, 7, 25, 0, 0, 0, 0);
33         DateTime b = new DateTime(1994, 7, 18, 0, 0, 0, 0);
34         System.out.println(new Period(a, b));
35
36         // new TempTest().testZone();
37
}
38
39     public void testZone() {
40         DateTimeZone zone = DateTimeZone.forID("America/Sao_Paulo");
41         YearMonthDay baseDate = new YearMonthDay(2006, 10, 15);
42         try {
43             baseDate.toDateMidnight(zone);
44         } catch (IllegalArgumentException JavaDoc ex) {
45             DateTime dm = baseDate.plusDays(1).toDateTimeAtMidnight(zone);
46             long adjustedMillis = zone.previousTransition(dm.getMillis()) + 1;
47             dm = new DateTime(adjustedMillis, zone);
48             System.out.println(dm);
49         }
50         DateTime dt = baseDate.toDateTime(new TimeOfDay(12, 0), zone);
51         System.out.println(dt);
52         dt = dt.dayOfYear().roundFloorCopy();
53         System.out.println(dt);
54     }
55     
56     public void testPlusMonths() {
57         Object JavaDoc[] allTimeZones = DateTimeZone.getAvailableIDs().toArray();
58         //System.out.println("OK state;Timezone Alias;Timezone name");
59
for (int i = 0; i < allTimeZones.length; i++) {
60             plusMonths((String JavaDoc) allTimeZones[i], 1800);
61         }
62 // plusMonths("Europe/London", 1600);
63
}
64
65     private void plusMonths(String JavaDoc tzId, int year) {
66
67         setTimezone(tzId);
68
69         DateTime startDate;
70         boolean ok = true;
71         for (int hr = 0; hr < 24; hr++) {
72             try {
73                 startDate = new DateTime(year, 1, 1, hr, 0, 0, 0);
74             } catch (IllegalArgumentException JavaDoc e) {
75                 // DateTime does not exist, due to timezone offset transition
76
continue;
77             }
78             startDate = new DateTime("1911-12-01T00:00:00.000-00:16:08");
79             if (plusMonthsWith(startDate) == false) {
80                 ok = false;
81                 break;
82             }
83         }
84
85         if (!ok) {
86             System.out.println("NOK;" + DateTimeZone.getDefault().getID() + ";" + tzId);
87         } else {
88             //System.out.println("OK;" + DateTimeZone.getDefault().getID() + ";" + tzId);
89
}
90     }
91
92     private boolean plusMonthsWith(DateTime startDate) {
93
94         int latestMonth;
95         int expectedMonth;
96         DateTime lastDT;
97         DateTime dt = startDate.toDateTime();
98         boolean ok = true;
99         for (int i = 0; i < 10000; i++) {
100             latestMonth = dt.getMonthOfYear();
101             lastDT = dt;
102             dt = dt.plusMonths(1);
103             if (latestMonth == 12) {
104                 expectedMonth = 1;
105             } else {
106                 expectedMonth = latestMonth + 1;
107             }
108             // assertEquals(expectedMonth, dt.getMonthOfYear());
109
if (expectedMonth != dt.getMonthOfYear()) {
110                 System.out.println("ExpectedMonth=" + expectedMonth);
111                 System.out.println(" ActualMonth=" + dt.getMonthOfYear());
112                 System.out.println(" LastDT=" + lastDT);
113                 System.out.println(" ActualDT=" + dt);
114                 System.out.println(" Zone=" + new DateTime(DateTimeZone.getDefault().previousTransition(dt.getMillis() - 1) - 1));
115                 System.out.println(" Zone=" + new DateTime(DateTimeZone.getDefault().previousTransition(dt.getMillis()) - 1));
116                 System.out.println(" Zone=" + new DateTime(DateTimeZone.getDefault().nextTransition(dt.getMillis()) + 1));
117                 System.out.println(" Zone=" + new DateTime(DateTimeZone.getDefault().nextTransition(dt.getMillis() + 1) + 1));
118                 if (!ok) {
119                     System.exit(0);
120                 }
121                 ok = false;
122                 //break;
123
}
124         }
125         //System.out.println(" EndDT=" + dt);
126

127         return ok;
128     }
129
130     private void setTimezone(String JavaDoc id) {
131         DateTimeZone.setDefault(DateTimeZone.forID(id));
132         TimeZone.setDefault(TimeZone.getTimeZone(DateTimeZone.getDefault().getID()));
133     }
134
135 }
136
Popular Tags