1 19 20 package jxl.write.biff; 21 22 import java.util.Date ; 23 import java.util.Calendar ; 24 import java.util.TimeZone ; 25 import java.text.DateFormat ; 26 27 import jxl.format.CellFormat; 28 import jxl.CellType; 29 import jxl.DateCell; 30 import jxl.biff.Type; 31 import jxl.biff.DoubleHelper; 32 import jxl.write.DateFormats; 33 import jxl.write.WritableCellFormat; 34 35 38 public abstract class DateRecord extends CellValue 39 { 40 43 private double value; 44 47 private Date date; 48 49 52 private boolean time; 53 54 58 private final static int utcOffsetDays = 25569; 59 60 63 private final static long msInADay = 24 * 60 * 60 * 1000; 64 65 69 static final WritableCellFormat defaultDateFormat = 70 new WritableCellFormat(DateFormats.DEFAULT); 71 72 78 private final static int nonLeapDay = 61; 79 80 83 protected static final class GMTDate 84 { 85 public GMTDate(){} 86 }; 87 88 95 protected DateRecord(int c, int r, Date d) 96 { 97 this(c, r, d, defaultDateFormat, true); 98 } 99 100 108 protected DateRecord(int c, int r, Date d, GMTDate a) 109 { 110 this(c, r, d, defaultDateFormat, false); 111 } 112 113 121 protected DateRecord(int c, int r, Date d, CellFormat st) 122 { 123 super(Type.NUMBER, c, r,st); 124 date = d; 125 calculateValue(true); 126 } 127 128 137 protected DateRecord(int c, int r, Date d, CellFormat st, GMTDate a) 138 { 139 super(Type.NUMBER, c, r, st); 140 date = d; 141 calculateValue(false); 142 } 143 144 153 protected DateRecord(int c, int r, Date d, CellFormat st, boolean tim) 154 { 155 super(Type.NUMBER, c, r, st); 156 date = d; 157 calculateValue(false); 158 time = tim; 159 } 160 161 166 protected DateRecord(DateCell dc) 167 { 168 super(Type.NUMBER, dc); 169 date = dc.getDate(); 170 calculateValue(false); 171 } 172 173 180 protected DateRecord(int c, int r, DateRecord dr) 181 { 182 super(Type.NUMBER, c, r, dr); 183 value = dr.value; 184 time = dr.time; 185 date = dr.date; 186 } 187 188 195 private void calculateValue(boolean adjust) 196 { 197 long zoneOffset = 0; 199 long dstOffset = 0; 200 201 if (adjust) 204 { 205 Calendar cal = Calendar.getInstance(); 207 cal.setTime(date); 208 209 zoneOffset = cal.get(Calendar.ZONE_OFFSET); 210 dstOffset = cal.get(Calendar.DST_OFFSET); 211 } 212 213 long utcValue = date.getTime() + zoneOffset + dstOffset; 214 215 double utcDays = (double) utcValue / (double) msInADay; 218 219 value = utcDays + utcOffsetDays; 221 222 if (value < nonLeapDay) 226 { 227 value -= 1; 228 } 229 230 if (time) 232 { 233 value = value - (int) value; 234 } 235 } 236 237 242 public CellType getType() 243 { 244 return CellType.DATE; 245 } 246 247 252 public byte[] getData() 253 { 254 byte[] celldata = super.getData(); 255 byte[] data = new byte[celldata.length + 8]; 256 System.arraycopy(celldata, 0, data, 0, celldata.length); 257 DoubleHelper.getIEEEBytes(value, data, celldata.length); 258 259 return data; 260 } 261 262 269 public String getContents() 270 { 271 return date.toString(); 272 } 273 274 279 protected void setDate(Date d) 280 { 281 date = d; 282 calculateValue(true); 283 } 284 285 291 protected void setDate(Date d, GMTDate a) 292 { 293 date = d; 294 calculateValue(false); 295 } 296 297 298 303 public Date getDate() 304 { 305 return date; 306 } 307 308 315 public boolean isTime() 316 { 317 return time; 318 } 319 320 328 public DateFormat getDateFormat() 329 { 330 return null; 331 } 332 } 333 334 335 336 | Popular Tags |