1 16 17 18 19 24 package org.apache.poi.hssf.usermodel; 25 26 import java.util.Calendar ; 27 import java.util.Date ; 28 import java.util.GregorianCalendar ; 29 30 38 39 public class HSSFDateUtil 40 { 41 private HSSFDateUtil() 42 { 43 } 44 45 private static final int BAD_DATE = 46 -1; private static final long DAY_MILLISECONDS = 24 * 60 * 60 * 1000; 48 private static final double CAL_1900_ABSOLUTE = 49 ( double ) absoluteDay(new GregorianCalendar (1900, Calendar 50 .JANUARY, 1)) - 2.0; 51 52 59 60 public static double getExcelDate(Date date) 61 { 62 Calendar calStart = new GregorianCalendar (); 63 64 calStart.setTime( 65 date); if (calStart.get(Calendar.YEAR) < 1900) 67 { 68 return BAD_DATE; 69 } 70 else 71 { 72 double fraction = (((calStart.get(Calendar.HOUR_OF_DAY) * 60 80 + calStart.get(Calendar.MINUTE) 81 ) * 60 + calStart.get(Calendar.SECOND) 82 ) * 1000 + calStart.get(Calendar.MILLISECOND) 83 ) / ( double ) DAY_MILLISECONDS; 84 calStart = dayStart(calStart); 85 86 return fraction + ( double ) absoluteDay(calStart) 87 - CAL_1900_ABSOLUTE; 88 } 89 } 90 91 100 101 public static Date getJavaDate(double date) 102 { 103 return getJavaDate(date,false); 104 } 105 106 125 public static Date getJavaDate(double date, boolean use1904windowing) { 126 if (isValidExcelDate(date)) { 127 int startYear = 1900; 128 int dayAdjust = -1; int wholeDays = (int)Math.floor(date); 130 if (use1904windowing) { 131 startYear = 1904; 132 dayAdjust = 1; } 134 else if (wholeDays < 61) { 135 dayAdjust = 0; 138 } 139 GregorianCalendar calendar = new GregorianCalendar (startYear,0, 140 wholeDays + dayAdjust); 141 int millisecondsInDay = (int)((date - Math.floor(date)) * 142 (double) DAY_MILLISECONDS + 0.5); 143 calendar.set(GregorianCalendar.MILLISECOND, millisecondsInDay); 144 return calendar.getTime(); 145 } 146 else { 147 return null; 148 } 149 } 150 151 155 public static boolean isInternalDateFormat(int format) { 156 boolean retval =false; 157 158 switch(format) { 159 case 0x0e: 162 case 0x0f: 163 case 0x10: 164 case 0x11: 165 case 0x12: 166 case 0x13: 167 case 0x14: 168 case 0x15: 169 case 0x16: 170 case 0x2d: 171 case 0x2e: 172 case 0x2f: 173 retval = true; 174 break; 175 176 default: 177 retval = false; 178 break; 179 } 180 return retval; 181 } 182 183 189 public static boolean isCellDateFormatted(HSSFCell cell) { 190 if (cell == null) return false; 191 boolean bDate = false; 192 193 double d = cell.getNumericCellValue(); 194 if ( HSSFDateUtil.isValidExcelDate(d) ) { 195 HSSFCellStyle style = cell.getCellStyle(); 196 int i = style.getDataFormat(); 197 bDate = isInternalDateFormat(i); 198 } 199 return bDate; 200 } 201 202 203 209 210 public static boolean isValidExcelDate(double value) 211 { 212 return (value > -Double.MIN_VALUE); 213 } 214 215 222 223 private static int absoluteDay(Calendar cal) 224 { 225 return cal.get(Calendar.DAY_OF_YEAR) 226 + daysInPriorYears(cal.get(Calendar.YEAR)); 227 } 228 229 236 237 private static int daysInPriorYears(int yr) 238 { 239 if (yr < 1601) 240 { 241 throw new IllegalArgumentException ( 242 "'year' must be 1601 or greater"); 243 } 244 int y = yr - 1601; 245 int days = 365 * y + y / 4 - y / 100 + y / 400; 250 return days; 251 } 252 253 private static Calendar dayStart(final Calendar cal) 255 { 256 cal.get(Calendar 257 .HOUR_OF_DAY); cal.set(Calendar.HOUR_OF_DAY, 0); 259 cal.set(Calendar.MINUTE, 0); 260 cal.set(Calendar.SECOND, 0); 261 cal.set(Calendar.MILLISECOND, 0); 262 cal.get(Calendar 263 .HOUR_OF_DAY); return cal; 265 } 266 267 } 269 | Popular Tags |