KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > DateRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2001 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.write.biff;
21
22 import java.util.Date JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.TimeZone JavaDoc;
25 import java.text.DateFormat JavaDoc;
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 /**
36  * A date stored in the database
37  */

38 public abstract class DateRecord extends CellValue
39 {
40   /**
41    * The excel value of the date
42    */

43   private double value;
44   /**
45    * The java representation of the date
46    */

47   private Date JavaDoc date;
48
49   /**
50    * Indicates whether this is a full date, or just a time only
51    */

52   private boolean time;
53
54   // The number of days between 01 Jan 1900 and 01 Jan 1970 - this gives
55
// the UTC offset
56
/**
57    */

58   private final static int utcOffsetDays = 25569;
59
60   // The number of milliseconds in a day
61
/**
62    */

63   private final static long msInADay = 24 * 60 * 60 * 1000;
64
65   /**
66    * This is package protected so that the worksheet might detect
67    * whether or not to override it with the column cell format
68    */

69   static final WritableCellFormat defaultDateFormat =
70     new WritableCellFormat(DateFormats.DEFAULT);
71
72   // The number of days between 1 Jan 1900 and 1 March 1900. Excel thinks
73
// the day before this was 29th Feb 1900, but it was 28th Feb 19000.
74
// I guess the programmers thought nobody would notice that they
75
// couldn't be bothered to program this dating anomaly properly
76
/**
77    */

78   private final static int nonLeapDay = 61;
79
80   /**
81    * Class definition for a dummy variable
82    */

83   protected static final class GMTDate
84   {
85     public GMTDate(){}
86   };
87
88   /**
89    * Constructor invoked by the user API
90    *
91    * @param c the column
92    * @param r the row
93    * @param d the date
94    */

95   protected DateRecord(int c, int r, Date JavaDoc d)
96   {
97     this(c, r, d, defaultDateFormat, true);
98   }
99
100   /**
101    * Constructor invoked by the user API
102    *
103    * @param c the column
104    * @param r the row
105    * @param d the date
106    * @param a adjust timezone
107    */

108   protected DateRecord(int c, int r, Date JavaDoc d, GMTDate a)
109   {
110     this(c, r, d, defaultDateFormat, false);
111   }
112
113   /**
114    * Constructor invoked from the user API
115    *
116    * @param c the column
117    * @param r the row
118    * @param st the format for the date
119    * @param d the date
120    */

121   protected DateRecord(int c, int r, Date JavaDoc d, CellFormat st)
122   {
123     super(Type.NUMBER, c, r,st);
124     date = d;
125     calculateValue(true);
126   }
127
128   /**
129    * Constructor invoked from the user API
130    *
131    * @param c the column
132    * @param r the row
133    * @param st the format for the date
134    * @param d the date
135    * @param a adjust for the timezone
136    */

137   protected DateRecord(int c, int r, Date JavaDoc d, CellFormat st, GMTDate a)
138   {
139     super(Type.NUMBER, c, r, st);
140     date = d;
141     calculateValue(false);
142   }
143
144   /**
145    * Constructor invoked from the API
146    *
147    * @param c the column
148    * @param r the row
149    * @param st the date format
150    * @param tim time indicator
151    * @param d the date
152    */

153   protected DateRecord(int c, int r, Date JavaDoc 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   /**
162    * Constructor invoked when copying a readable spreadsheet
163    *
164    * @param dc the date to copy
165    */

166   protected DateRecord(DateCell dc)
167   {
168     super(Type.NUMBER, dc);
169     date = dc.getDate();
170     calculateValue(false);
171   }
172
173   /**
174    * Copy constructor
175    *
176    * @param c the column
177    * @param r the row
178    * @param dr the record to copy
179    */

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   /**
189    * Calculates the 1900 based numerical value based upon the utc value held
190    * in the date object
191    *
192    * @param adjust TRUE if we want to incorporate timezone information
193    * into the raw UTC date eg. when copying from a spreadsheet
194    */

195   private void calculateValue(boolean adjust)
196   {
197     // Offsets for current time zone
198
long zoneOffset = 0;
199     long dstOffset = 0;
200
201     // Get the timezone and dst offsets if we want to take these into
202
// account
203
if (adjust)
204     {
205       // Get the current calender, replete with timezone information
206
Calendar JavaDoc 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     // Convert this to the number of days, plus fractions of a day since
216
// 01 Jan 1970
217
double utcDays = (double) utcValue / (double) msInADay;
218
219     // Add in the offset to get the number of days since 01 Jan 1900
220
value = utcDays + utcOffsetDays;
221
222     // Work round a bug in excel. Excel seems to think there is a date
223
// called the 29th Feb, 1900 - but this was not a leap year.
224
// Therefore for values less than 61, we must subtract 1
225
if (value < nonLeapDay)
226     {
227       value -= 1;
228     }
229
230     // If this refers to a time, then get rid of the integer part
231
if (time)
232     {
233       value = value - (int) value;
234     }
235   }
236
237   /**
238    * Returns the content type of this cell
239    *
240    * @return the content type for this cell
241    */

242   public CellType getType()
243   {
244     return CellType.DATE;
245   }
246
247   /**
248    * Gets the binary data for writing
249    *
250    * @return the binary data
251    */

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   /**
263    * Quick and dirty function to return the contents of this cell as a string.
264    * For more complex manipulation of the contents, it is necessary to cast
265    * this interface to correct subinterface
266    *
267    * @return the contents of this cell as a string
268    */

269   public String JavaDoc getContents()
270   {
271     return date.toString();
272   }
273
274   /**
275    * Sets the date in this cell
276    *
277    * @param d the date
278    */

279   protected void setDate(Date JavaDoc d)
280   {
281     date = d;
282     calculateValue(true);
283   }
284
285   /**
286    * Sets the date in this cell, taking the timezone into account
287    *
288    * @param d the date
289    * @param a adjust for timezone
290    */

291   protected void setDate(Date JavaDoc d, GMTDate a)
292   {
293     date = d;
294     calculateValue(false);
295   }
296
297
298   /**
299    * Gets the date contained in this cell
300    *
301    * @return the cell contents
302    */

303   public Date JavaDoc getDate()
304   {
305     return date;
306   }
307
308   /**
309    * Indicates whether the date value contained in this cell refers to a date,
310    * or merely a time. When writing a cell, all dates are fully defined,
311    * even if they refer to a time
312    *
313    * @return FALSE if this is full date, TRUE if a time
314    */

315   public boolean isTime()
316   {
317     return time;
318   }
319
320   /**
321    * Gets the DateFormat used to format the cell. This will normally be
322    * the format specified in the excel spreadsheet, but in the event of any
323    * difficulty parsing this, it will revert to the default date/time format.
324    *
325    * @return the DateFormat object used to format the date in the original
326    * excel cell
327    */

328   public DateFormat JavaDoc getDateFormat()
329   {
330     return null;
331   }
332 }
333
334
335
336
Popular Tags