KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 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.read.biff;
21
22 import java.util.Date JavaDoc;
23 import java.util.TimeZone JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.text.DateFormat JavaDoc;
26
27 import common.Assert;
28 import common.Logger;
29
30 import jxl.NumberCell;
31 import jxl.DateCell;
32 import jxl.CellType;
33 import jxl.CellFeatures;
34 import jxl.format.CellFormat;
35 import jxl.biff.FormattingRecords;
36
37 /**
38  * A date which is stored in the cell
39  */

40 class DateRecord implements DateCell, CellFeaturesAccessor
41 {
42   /**
43    * The logger
44    */

45   private static Logger logger = Logger.getLogger(DateRecord.class);
46
47   /**
48    * The date represented within this cell
49    */

50   private Date JavaDoc date;
51   /**
52    * The row number of this cell record
53    */

54   private int row;
55   /**
56    * The column number of this cell record
57    */

58   private int column;
59
60   /**
61    * Indicates whether this is a full date, or merely a time
62    */

63   private boolean time;
64
65   /**
66    * The format to use when displaying this cell's contents as a string
67    */

68   private DateFormat JavaDoc format;
69
70   /**
71    * The raw cell format
72    */

73   private CellFormat cellFormat;
74
75   /**
76    * The index to the XF Record
77    */

78   private int xfIndex;
79
80   /**
81    * A handle to the formatting records
82    */

83   private FormattingRecords formattingRecords;
84
85   /**
86    * A handle to the sheet
87    */

88   private SheetImpl sheet;
89
90   /**
91    * The cell features
92    */

93   private CellFeatures features;
94
95
96   /**
97    * A flag to indicate whether this objects formatting things have
98    * been initialized
99    */

100   private boolean initialized;
101
102   // The default formats used when returning the date as a string
103
private static final SimpleDateFormat JavaDoc dateFormat =
104    new SimpleDateFormat JavaDoc("dd MMM yyyy");
105
106   private static final SimpleDateFormat JavaDoc timeFormat =
107     new SimpleDateFormat JavaDoc("HH:mm:ss");
108
109   // The number of days between 1 Jan 1900 and 1 March 1900. Excel thinks
110
// the day before this was 29th Feb 1900, but it was 28th Feb 1900.
111
// I guess the programmers thought nobody would notice that they
112
// couldn't be bothered to program this dating anomaly properly
113
private static final int nonLeapDay = 61;
114
115   private static final TimeZone JavaDoc gmtZone = TimeZone.getTimeZone("GMT");
116
117   // The number of days between 01 Jan 1900 and 01 Jan 1970 - this gives
118
// the UTC offset
119
private static final int utcOffsetDays = 25569;
120
121   // The number of days between 01 Jan 1904 and 01 Jan 1970 - this gives
122
// the UTC offset using the 1904 date system
123
private static final int utcOffsetDays1904 = 24107;
124
125   // The number of milliseconds in a day
126
private static final long msInADay = 24 * 60 * 60 * 1000;
127
128   /**
129    * Constructs this object from the raw data
130    *
131    * @param num the numerical representation of this
132    * @param xfi the java equivalent of the excel date format
133    * @param fr the formatting records
134    * @param nf flag indicating whether we are using the 1904 date system
135    * @param si the sheet
136    */

137   public DateRecord(NumberCell num,
138                     int xfi, FormattingRecords fr,
139                     boolean nf, SheetImpl si)
140   {
141     row = num.getRow();
142     column = num.getColumn();
143     xfIndex = xfi;
144     formattingRecords = fr;
145     sheet = si;
146     initialized = false;
147
148     format = formattingRecords.getDateFormat(xfIndex);
149
150     // This value represents the number of days since 01 Jan 1900
151
double numValue = num.getValue();
152
153     if (Math.abs(numValue) < 1)
154     {
155       if (format == null)
156       {
157         format = timeFormat;
158       }
159       time = true;
160     }
161     else
162     {
163       if (format == null)
164       {
165         format = dateFormat;
166       }
167       time = false;
168     }
169
170     // Work round a bug in excel. Excel seems to think there is a date
171
// called the 29th Feb, 1900 - but in actual fact this was not a leap year.
172
// Therefore for values less than 61 in the 1900 date system,
173
// add one to the numeric value
174
if (!nf && !time && numValue < nonLeapDay)
175     {
176       numValue += 1;
177     }
178
179     // Get rid of any timezone adjustments - we are not interested
180
// in automatic adjustments
181
format.setTimeZone(gmtZone);
182
183     // Convert this to the number of days since 01 Jan 1970
184
int offsetDays = nf ? utcOffsetDays1904 : utcOffsetDays;
185     double utcDays = numValue - offsetDays;
186
187     // Convert this into utc by multiplying by the number of milliseconds
188
// in a day
189
long utcValue = Math.round(utcDays * msInADay);
190
191     date = new Date JavaDoc(utcValue);
192   }
193
194   /**
195    * Interface method which returns the row number of this cell
196    *
197    * @return the zero base row number
198    */

199   public final int getRow()
200   {
201     return row;
202   }
203
204   /**
205    * Interface method which returns the column number of this cell
206    *
207    * @return the zero based column number
208    */

209   public final int getColumn()
210   {
211     return column;
212   }
213
214   /**
215    * Gets the date
216    *
217    * @return the date
218    */

219   public Date JavaDoc getDate()
220   {
221     return date;
222   }
223
224   /**
225    * Gets the cell contents as a string. This method will use the java
226    * equivalent of the excel formatting string
227    *
228    * @return the label
229    */

230   public String JavaDoc getContents()
231   {
232     return format.format(date);
233   }
234
235   /**
236    * Accessor for the cell type
237    *
238    * @return the cell type
239    */

240   public CellType getType()
241   {
242     return CellType.DATE;
243   }
244
245   /**
246    * Indicates whether the date value contained in this cell refers to a date,
247    * or merely a time
248    *
249    * @return TRUE if the value refers to a time
250    */

251   public boolean isTime()
252   {
253     return time;
254   }
255
256   /**
257    * Gets the DateFormat used to format the cell. This will normally be
258    * the format specified in the excel spreadsheet, but in the event of any
259    * difficulty parsing this, it will revert to the default date/time format.
260    *
261    * @return the DateFormat object used to format the date in the original
262    * excel cell
263    */

264   public DateFormat JavaDoc getDateFormat()
265   {
266     Assert.verify(format != null);
267
268     return format;
269   }
270
271   /**
272    * Gets the CellFormat object for this cell. Used by the WritableWorkbook
273    * API
274    *
275    * @return the CellFormat used for this cell
276    */

277   public CellFormat getCellFormat()
278   {
279     if (!initialized)
280     {
281       cellFormat = formattingRecords.getXFRecord(xfIndex);
282       initialized = true;
283     }
284
285     return cellFormat;
286   }
287
288   /**
289    * Determines whether or not this cell has been hidden
290    *
291    * @return TRUE if this cell has been hidden, FALSE otherwise
292    */

293   public boolean isHidden()
294   {
295     ColumnInfoRecord cir = sheet.getColumnInfo(column);
296
297     if (cir != null && cir.getWidth() == 0)
298     {
299       return true;
300     }
301
302     RowRecord rr = sheet.getRowInfo(row);
303
304     if (rr != null && (rr.getRowHeight() == 0 || rr.isCollapsed()))
305     {
306       return true;
307     }
308
309     return false;
310   }
311
312   /**
313    * Accessor for the sheet
314    *
315    * @return the containing sheet
316    */

317   protected final SheetImpl getSheet()
318   {
319     return sheet;
320   }
321
322   /**
323    * Accessor for the cell features
324    *
325    * @return the cell features or NULL if this cell doesn't have any
326    */

327   public CellFeatures getCellFeatures()
328   {
329     return features;
330   }
331
332   /**
333    * Sets the cell features
334    *
335    * @param cf the cell features
336    */

337   public void setCellFeatures(CellFeatures cf)
338   {
339     features = cf;
340   }
341
342 }
343
344
345
346
347
348
Popular Tags