1 2 17 18 19 package org.apache.poi.hssf.usermodel; 20 21 import junit.framework.TestCase; 22 23 import java.util.Date ; 24 import java.util.Calendar ; 25 import java.util.GregorianCalendar ; 26 import java.util.TimeZone ; 27 28 37 38 public class TestHSSFDateUtil 39 extends TestCase 40 { 41 public TestHSSFDateUtil(String s) 42 { 43 super(s); 44 } 45 46 49 50 public void testDateConversion() 51 throws Exception 52 { 53 54 for (int hour = 0; hour < 23; hour++) 56 { 57 GregorianCalendar date = new GregorianCalendar (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 double excelDate = 36526.0; 68 GregorianCalendar cal = new GregorianCalendar (2000,0,1); Date dateIf1900 = cal.getTime(); 72 cal.add(GregorianCalendar.YEAR,4); cal.add(GregorianCalendar.DATE,1); Date dateIf1904 = cal.getTime(); 75 assertEquals("Checking 1900 Date Windowing", 77 dateIf1900.getTime(), 78 HSSFDateUtil.getJavaDate(excelDate,false).getTime()); 79 assertEquals("Checking 1904 Date Windowing", 81 dateIf1904.getTime(), 82 HSSFDateUtil.getJavaDate(excelDate,true).getTime()); 83 } 84 85 89 public void testExcelConversionOnDSTStart() { 90 TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen"); 91 TimeZone.setDefault(cet); 92 Calendar cal = new GregorianCalendar (2004, Calendar.MARCH, 28); 93 for (int hour = 0; hour < 24; hour++) { 94 95 if (hour == 2) { 98 continue; 99 } 100 101 cal.set(Calendar.HOUR_OF_DAY, hour); 102 Date 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 119 public void testJavaConversionOnDSTStart() { 120 TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen"); 121 TimeZone.setDefault(cet); 122 Calendar cal = new GregorianCalendar (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 if (hour == 2) { 131 continue; 132 } 133 134 cal.set(Calendar.HOUR_OF_DAY, hour); 135 Date 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 146 public void testExcelConversionOnDSTEnd() { 147 TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen"); 148 TimeZone.setDefault(cet); 149 Calendar cal = new GregorianCalendar (2004, Calendar.OCTOBER, 31); 150 for (int hour = 0; hour < 24; hour++) { 151 cal.set(Calendar.HOUR_OF_DAY, hour); 152 Date 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 169 public void testJavaConversionOnDSTEnd() { 170 TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen"); 171 TimeZone.setDefault(cet); 172 Calendar cal = new GregorianCalendar (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 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 [] 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 |