KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestHSSFDateUtil


1
2 /* ====================================================================
3 Copyright 2002-2004 Apache Software Foundation
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */

17
18
19 package org.apache.poi.hssf.usermodel;
20
21 import junit.framework.TestCase;
22
23 import java.util.Date JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26 import java.util.TimeZone JavaDoc;
27
28 /**
29  * Class TestHSSFDateUtil
30  *
31  *
32  * @author
33  * @author Dan Sherman (dsherman at isisph.com)
34  * @author Hack Kampbjorn (hak at 2mba.dk)
35  * @version %I%, %G%
36  */

37
38 public class TestHSSFDateUtil
39         extends TestCase
40 {
41     public TestHSSFDateUtil(String JavaDoc s)
42     {
43         super(s);
44     }
45
46     /**
47      * Checks the date conversion functions in the HSSFDateUtil class.
48      */

49
50     public void testDateConversion()
51             throws Exception JavaDoc
52     {
53
54         // Iteratating over the hours exposes any rounding issues.
55
for (int hour = 0; hour < 23; hour++)
56         {
57             GregorianCalendar JavaDoc date = new GregorianCalendar JavaDoc(2002, 0, 1,
58                     hour, 1, 1);
59             double excelDate =
60                     HSSFDateUtil.getExcelDate(date.getTime());
61
62             assertEquals("Checking hour = " + hour, date.getTime().getTime(),
63                     HSSFDateUtil.getJavaDate(excelDate).getTime());
64         }
65
66         // check 1900 and 1904 date windowing conversions
67
double excelDate = 36526.0;
68         // with 1900 windowing, excelDate is Jan. 1, 2000
69
// with 1904 windowing, excelDate is Jan. 2, 2004
70
GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(2000,0,1); // Jan. 1, 2000
71
Date JavaDoc dateIf1900 = cal.getTime();
72         cal.add(GregorianCalendar.YEAR,4); // now Jan. 1, 2004
73
cal.add(GregorianCalendar.DATE,1); // now Jan. 2, 2004
74
Date JavaDoc dateIf1904 = cal.getTime();
75         // 1900 windowing
76
assertEquals("Checking 1900 Date Windowing",
77                 dateIf1900.getTime(),
78                 HSSFDateUtil.getJavaDate(excelDate,false).getTime());
79         // 1904 windowing
80
assertEquals("Checking 1904 Date Windowing",
81                 dateIf1904.getTime(),
82                 HSSFDateUtil.getJavaDate(excelDate,true).getTime());
83     }
84
85     /**
86      * Checks the conversion of a java.util.date to Excel on a day when
87      * Daylight Saving Time starts.
88      */

89     public void testExcelConversionOnDSTStart() {
90         TimeZone JavaDoc cet = TimeZone.getTimeZone("Europe/Copenhagen");
91         TimeZone.setDefault(cet);
92         Calendar JavaDoc cal = new GregorianCalendar JavaDoc(2004, Calendar.MARCH, 28);
93         for (int hour = 0; hour < 24; hour++) {
94
95             // Skip 02:00 CET as that is the Daylight change time
96
// and Java converts it automatically to 03:00 CEST
97
if (hour == 2) {
98                 continue;
99             }
100
101             cal.set(Calendar.HOUR_OF_DAY, hour);
102             Date JavaDoc javaDate = cal.getTime();
103             double excelDate = HSSFDateUtil.getExcelDate(javaDate);
104             double difference = excelDate - Math.floor(excelDate);
105             int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60;
106             assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
107                     hour,
108                     differenceInHours);
109             assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
110                     javaDate.getTime(),
111                     HSSFDateUtil.getJavaDate(excelDate).getTime());
112         }
113     }
114
115     /**
116      * Checks the conversion of an Excel date to a java.util.date on a day when
117      * Daylight Saving Time starts.
118      */

119     public void testJavaConversionOnDSTStart() {
120         TimeZone JavaDoc cet = TimeZone.getTimeZone("Europe/Copenhagen");
121         TimeZone.setDefault(cet);
122         Calendar JavaDoc cal = new GregorianCalendar JavaDoc(2004, Calendar.MARCH, 28);
123         double excelDate = HSSFDateUtil.getExcelDate(cal.getTime());
124         double oneHour = 1.0 / 24;
125         double oneMinute = oneHour / 60;
126         for (int hour = 0; hour < 24; hour++, excelDate += oneHour) {
127
128             // Skip 02:00 CET as that is the Daylight change time
129
// and Java converts it automatically to 03:00 CEST
130
if (hour == 2) {
131                 continue;
132             }
133
134             cal.set(Calendar.HOUR_OF_DAY, hour);
135             Date JavaDoc javaDate = HSSFDateUtil.getJavaDate(excelDate);
136             assertEquals("Checking " + hour + " hours on Daylight Saving Time start date",
137                     excelDate,
138                     HSSFDateUtil.getExcelDate(javaDate), oneMinute);
139         }
140     }
141
142     /**
143      * Checks the conversion of a java.util.Date to Excel on a day when
144      * Daylight Saving Time ends.
145      */

146     public void testExcelConversionOnDSTEnd() {
147         TimeZone JavaDoc cet = TimeZone.getTimeZone("Europe/Copenhagen");
148         TimeZone.setDefault(cet);
149         Calendar JavaDoc cal = new GregorianCalendar JavaDoc(2004, Calendar.OCTOBER, 31);
150         for (int hour = 0; hour < 24; hour++) {
151             cal.set(Calendar.HOUR_OF_DAY, hour);
152             Date JavaDoc javaDate = cal.getTime();
153             double excelDate = HSSFDateUtil.getExcelDate(javaDate);
154             double difference = excelDate - Math.floor(excelDate);
155             int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60;
156             assertEquals("Checking " + hour + " hour on Daylight Saving Time end date",
157                     hour,
158                     differenceInHours);
159             assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
160                     javaDate.getTime(),
161                     HSSFDateUtil.getJavaDate(excelDate).getTime());
162         }
163     }
164
165     /**
166      * Checks the conversion of an Excel date to java.util.Date on a day when
167      * Daylight Saving Time ends.
168      */

169     public void testJavaConversionOnDSTEnd() {
170         TimeZone JavaDoc cet = TimeZone.getTimeZone("Europe/Copenhagen");
171         TimeZone.setDefault(cet);
172         Calendar JavaDoc cal = new GregorianCalendar JavaDoc(2004, Calendar.OCTOBER, 31);
173         double excelDate = HSSFDateUtil.getExcelDate(cal.getTime());
174         double oneHour = 1.0 / 24;
175         double oneMinute = oneHour / 60;
176         for (int hour = 0; hour < 24; hour++, excelDate += oneHour) {
177             cal.set(Calendar.HOUR_OF_DAY, hour);
178             Date JavaDoc javaDate = HSSFDateUtil.getJavaDate(excelDate);
179             assertEquals("Checking " + hour + " hours on Daylight Saving Time start date",
180                     excelDate,
181                     HSSFDateUtil.getExcelDate(javaDate), oneMinute);
182         }
183     }
184
185     public static void main(String JavaDoc [] args) {
186         System.out
187                 .println("Testing org.apache.poi.hssf.usermodel.TestHSSFDateUtil");
188         junit.textui.TestRunner.run(TestHSSFDateUtil.class);
189     }
190
191 }
192
Popular Tags