TestHSSFDateUtilpublic class TestHSSFDateUtil extends TestCase
Constructors Summary |
---|
public TestHSSFDateUtil(String s)
super(s);
|
Methods Summary |
---|
public static void | main(java.lang.String[] args)
System.out
.println("Testing org.apache.poi.hssf.usermodel.TestHSSFDateUtil");
junit.textui.TestRunner.run(TestHSSFDateUtil.class);
| public void | testDateConversion()Checks the date conversion functions in the HSSFDateUtil class.
// Iteratating over the hours exposes any rounding issues.
for (int hour = 0; hour < 23; hour++)
{
GregorianCalendar date = new GregorianCalendar(2002, 0, 1,
hour, 1, 1);
double excelDate =
HSSFDateUtil.getExcelDate(date.getTime());
assertEquals("Checking hour = " + hour, date.getTime().getTime(),
HSSFDateUtil.getJavaDate(excelDate).getTime());
}
// check 1900 and 1904 date windowing conversions
double excelDate = 36526.0;
// with 1900 windowing, excelDate is Jan. 1, 2000
// with 1904 windowing, excelDate is Jan. 2, 2004
GregorianCalendar cal = new GregorianCalendar(2000,0,1); // Jan. 1, 2000
Date dateIf1900 = cal.getTime();
cal.add(GregorianCalendar.YEAR,4); // now Jan. 1, 2004
cal.add(GregorianCalendar.DATE,1); // now Jan. 2, 2004
Date dateIf1904 = cal.getTime();
// 1900 windowing
assertEquals("Checking 1900 Date Windowing",
dateIf1900.getTime(),
HSSFDateUtil.getJavaDate(excelDate,false).getTime());
// 1904 windowing
assertEquals("Checking 1904 Date Windowing",
dateIf1904.getTime(),
HSSFDateUtil.getJavaDate(excelDate,true).getTime());
| public void | testExcelConversionOnDSTEnd()Checks the conversion of a java.util.Date to Excel on a day when
Daylight Saving Time ends.
TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen");
TimeZone.setDefault(cet);
Calendar cal = new GregorianCalendar(2004, Calendar.OCTOBER, 31);
for (int hour = 0; hour < 24; hour++) {
cal.set(Calendar.HOUR_OF_DAY, hour);
Date javaDate = cal.getTime();
double excelDate = HSSFDateUtil.getExcelDate(javaDate);
double difference = excelDate - Math.floor(excelDate);
int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60;
assertEquals("Checking " + hour + " hour on Daylight Saving Time end date",
hour,
differenceInHours);
assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
javaDate.getTime(),
HSSFDateUtil.getJavaDate(excelDate).getTime());
}
| public void | testExcelConversionOnDSTStart()Checks the conversion of a java.util.date to Excel on a day when
Daylight Saving Time starts.
TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen");
TimeZone.setDefault(cet);
Calendar cal = new GregorianCalendar(2004, Calendar.MARCH, 28);
for (int hour = 0; hour < 24; hour++) {
// Skip 02:00 CET as that is the Daylight change time
// and Java converts it automatically to 03:00 CEST
if (hour == 2) {
continue;
}
cal.set(Calendar.HOUR_OF_DAY, hour);
Date javaDate = cal.getTime();
double excelDate = HSSFDateUtil.getExcelDate(javaDate);
double difference = excelDate - Math.floor(excelDate);
int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60;
assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
hour,
differenceInHours);
assertEquals("Checking " + hour + " hour on Daylight Saving Time start date",
javaDate.getTime(),
HSSFDateUtil.getJavaDate(excelDate).getTime());
}
| public void | testIdentifyDateFormats()Tests that we correctly detect date formats as such
// First up, try with a few built in date formats
short[] builtins = new short[] { 0x0e, 0x0f, 0x10, 0x16, 0x2d, 0x2e };
for(int i=0; i<builtins.length; i++) {
String formatStr = HSSFDataFormat.getBuiltinFormat(builtins[i]);
assertTrue( HSSFDateUtil.isInternalDateFormat(builtins[i]) );
assertTrue( HSSFDateUtil.isADateFormat(builtins[i],formatStr) );
}
// Now try a few built-in non date formats
builtins = new short[] { 0x01, 0x02, 0x17, 0x1f, 0x30 };
for(int i=0; i<builtins.length; i++) {
String formatStr = HSSFDataFormat.getBuiltinFormat(builtins[i]);
assertFalse( HSSFDateUtil.isInternalDateFormat(builtins[i]) );
assertFalse( HSSFDateUtil.isADateFormat(builtins[i],formatStr) );
}
// Now for some non-internal ones
// These come after the real ones
int numBuiltins = HSSFDataFormat.getNumberOfBuiltinBuiltinFormats();
assertTrue(numBuiltins < 60);
short formatId = 60;
assertFalse( HSSFDateUtil.isInternalDateFormat(formatId) );
// Valid ones first
String[] formats = new String[] {
"yyyy-mm-dd", "yyyy/mm/dd", "yy/mm/dd", "yy/mmm/dd",
"dd/mm/yy", "dd/mm/yyyy", "dd/mmm/yy",
"dd-mm-yy", "dd-mm-yyyy",
"dd\\-mm\\-yy", // Sometimes escaped
};
for(int i=0; i<formats.length; i++) {
assertTrue( HSSFDateUtil.isADateFormat(formatId, formats[i]) );
}
// Then invalid ones
formats = new String[] {
"yyyy:mm:dd",
"0.0", "0.000",
"0%", "0.0%",
"", null
};
for(int i=0; i<formats.length; i++) {
assertFalse( HSSFDateUtil.isADateFormat(formatId, formats[i]) );
}
| public void | testJavaConversionOnDSTEnd()Checks the conversion of an Excel date to java.util.Date on a day when
Daylight Saving Time ends.
TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen");
TimeZone.setDefault(cet);
Calendar cal = new GregorianCalendar(2004, Calendar.OCTOBER, 31);
double excelDate = HSSFDateUtil.getExcelDate(cal.getTime());
double oneHour = 1.0 / 24;
double oneMinute = oneHour / 60;
for (int hour = 0; hour < 24; hour++, excelDate += oneHour) {
cal.set(Calendar.HOUR_OF_DAY, hour);
Date javaDate = HSSFDateUtil.getJavaDate(excelDate);
assertEquals("Checking " + hour + " hours on Daylight Saving Time start date",
excelDate,
HSSFDateUtil.getExcelDate(javaDate), oneMinute);
}
| public void | testJavaConversionOnDSTStart()Checks the conversion of an Excel date to a java.util.date on a day when
Daylight Saving Time starts.
TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen");
TimeZone.setDefault(cet);
Calendar cal = new GregorianCalendar(2004, Calendar.MARCH, 28);
double excelDate = HSSFDateUtil.getExcelDate(cal.getTime());
double oneHour = 1.0 / 24;
double oneMinute = oneHour / 60;
for (int hour = 0; hour < 24; hour++, excelDate += oneHour) {
// Skip 02:00 CET as that is the Daylight change time
// and Java converts it automatically to 03:00 CEST
if (hour == 2) {
continue;
}
cal.set(Calendar.HOUR_OF_DAY, hour);
Date javaDate = HSSFDateUtil.getJavaDate(excelDate);
assertEquals("Checking " + hour + " hours on Daylight Saving Time start date",
excelDate,
HSSFDateUtil.getExcelDate(javaDate), oneMinute);
}
|
|