KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > client > DateRecord


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package com.google.gwt.i18n.client;
18
19 import java.util.Date JavaDoc;
20
21 /**
22  * DateRecord class exposes almost the same set of interface as Date class with
23  * only a few exceptions. The main purpose is the record all the information
24  * during parsing phase and resolve them in a later time when all information
25  * can be processed together.
26  */

27 class DateRecord extends Date JavaDoc {
28
29   /*
30    * The serial version UID is only defined because this class is implicitly
31    * serializable, causing warnings due to its absence. It will generally not be
32    * used.
33    */

34   private static final long serialVersionUID = -1278816193740448162L;
35
36   public static final int AM = 0;
37   public static final int PM = 1;
38
39   private static final int JS_START_YEAR = 1900;
40
41   private int era;
42   private int year;
43   private int month;
44   private int dayOfMonth;
45   private int ampm;
46   private int hours;
47   private int minutes;
48   private int seconds;
49   private int milliseconds;
50
51   private int tzOffset;
52   private int dayOfWeek;
53   private boolean ambiguousYear;
54
55   /**
56    * Initialize DateExt object with default value. Here we use -1 for most of
57    * the field to indicate that field is not set.
58    */

59   public DateRecord() {
60     era = -1;
61     ambiguousYear = false;
62     year = Integer.MIN_VALUE;
63     month = -1;
64     dayOfMonth = -1;
65     ampm = -1;
66     hours = -1;
67     minutes = -1;
68     seconds = -1;
69     milliseconds = -1;
70     dayOfWeek = -1;
71     tzOffset = Integer.MIN_VALUE;
72   };
73
74   /**
75    * calcDate uses all the field available so far to fill a Date object. For
76    * those information that is not provided, the existing value in 'date' will
77    * be kept. Ambiguouse year will be resolved after the date/time value are
78    * resolved.
79    *
80    * @param date The Date object being filled. Its value should be set to a
81    * accetable default before pass in to this method
82    * @return true if sucessful, otherwise false.
83    */

84   public boolean calcDate(Date JavaDoc date) {
85     // Year 0 is 1 BC, and so on.
86
if (this.era == 0 && this.year > 0) {
87       this.year = -(this.year - 1);
88     }
89
90     if (this.year > Integer.MIN_VALUE) {
91       date.setYear(this.year - JS_START_YEAR);
92     }
93
94     // "setMonth" and "setDate" is a little bit tricky. Suppose content in
95
// date is 11/30, switch month to 02 will lead to 03/02 since 02/30 does
96
// not exist. And you certain won't like 02/12 turn out to be 03/12. So
97
// here to set date to a smaller number before month, and later setMonth.
98
// Real date is set after, and that might cause month switch. However,
99
// that's desired.
100
int orgDayOfMonth = date.getDate();
101     date.setDate(1);
102
103     if (this.month >= 0) {
104       date.setMonth(this.month);
105     }
106
107     if (this.dayOfMonth >= 0) {
108       date.setDate(this.dayOfMonth);
109     } else {
110       date.setDate(orgDayOfMonth);
111     }
112
113     // adjust ampm
114
if (this.hours < 0) {
115       this.hours = date.getHours();
116     }
117
118     if (this.ampm > 0) {
119       if (this.hours < 12) {
120         this.hours += 12;
121       }
122     }
123     date.setHours(this.hours);
124
125     if (this.minutes >= 0) {
126       date.setMinutes(this.minutes);
127     }
128
129     if (this.seconds >= 0) {
130       date.setSeconds(this.seconds);
131     }
132
133     if (this.milliseconds >= 0) {
134       date.setTime(date.getTime() / 1000 * 1000 + this.milliseconds);
135     }
136
137     // Adjust time zone.
138
if (this.tzOffset > Integer.MIN_VALUE) {
139       int offset = date.getTimezoneOffset();
140       date.setTime(date.getTime() + (this.tzOffset - offset) * 60 * 1000);
141       // HBJ date.setTime(date.getTime() + this.tzOffset * 60 * 1000);
142
}
143
144     // Resolve ambiguous year if needed.
145
if (this.ambiguousYear) { // the two-digit year == the default start year
146
Date JavaDoc defaultCenturyStart = new Date JavaDoc();
147       defaultCenturyStart.setYear(defaultCenturyStart.getYear() - 80);
148       if (date.before(defaultCenturyStart)) {
149         date.setYear(defaultCenturyStart.getYear() + 100);
150       }
151     }
152
153     // Date is resolved to the nearest dayOfWeek if date is not explicitly
154
// specified. There is one exception, if the nearest dayOfWeek falls
155
// into a different month, the 2nd nearest dayOfWeek, which is on the
156
// other direction, will be used.
157
if (this.dayOfWeek >= 0) {
158       if (this.dayOfMonth == -1) {
159         // Adjust to the nearest day of the week.
160
int adjustment = (7 + this.dayOfWeek - date.getDay()) % 7;
161         if (adjustment > 3) {
162           adjustment -= 7;
163         }
164         int orgMonth = date.getMonth();
165         date.setDate(date.getDate() + adjustment);
166
167         // If the nearest weekday fall into a different month, we will use the
168
// 2nd nearest weekday, which will be on the other direction, and is
169
// sure fall into the same month.
170
if (date.getMonth() != orgMonth) {
171           date.setDate(date.getDate() + (adjustment > 0 ? -7 : 7));
172         }
173       } else {
174         if (date.getDay() != this.dayOfWeek) {
175           return false;
176         }
177       }
178     }
179     return true;
180   }
181
182   /**
183    * Set ambiguous year field. This flag indicates that a 2 digit years's
184    * century need to be determined by its date/time value. This can only be
185    * resolved after its date/time is known.
186    *
187    * @param ambiguousYear true if it is ambiguous year.
188    */

189   public void setAmbiguousYear(boolean ambiguousYear) {
190     this.ambiguousYear = ambiguousYear;
191   }
192
193   /**
194    * Set morning/afternoon field.
195    *
196    * @param ampm ampm value.
197    */

198   public void setAmpm(int ampm) {
199     this.ampm = ampm;
200   }
201
202   /**
203    * Set dayOfMonth field.
204    *
205    * @param day dayOfMonth value
206    */

207   public void setDayOfMonth(int day) {
208     this.dayOfMonth = day;
209   }
210
211   /**
212    * Set dayOfWeek field.
213    *
214    * @param dayOfWeek day of the week.
215    */

216   public void setDayOfWeek(int dayOfWeek) {
217     this.dayOfWeek = dayOfWeek;
218   }
219
220   /**
221    * Set Era field.
222    *
223    * @param era era value being set.
224    */

225   public void setEra(int era) {
226     this.era = era;
227   }
228
229   /**
230    * Set hour field.
231    *
232    * @param hours hour value.
233    */

234   public void setHours(int hours) {
235     this.hours = hours;
236   }
237
238   /**
239    * Set milliseconds field.
240    *
241    * @param milliseconds milliseconds value.
242    */

243   public void setMilliseconds(int milliseconds) {
244     this.milliseconds = milliseconds;
245   }
246
247   /**
248    * Set minute field.
249    *
250    * @param minutes minute value.
251    */

252   public void setMinutes(int minutes) {
253     this.minutes = minutes;
254   }
255
256   /**
257    * Set month field.
258    *
259    * @param month month value.
260    */

261   public void setMonth(int month) {
262     this.month = month;
263   }
264
265   /**
266    * Set seconds field.
267    *
268    * @param seconds second value.
269    */

270   public void setSeconds(int seconds) {
271     this.seconds = seconds;
272   }
273
274   /**
275    * Set timezone offset, in minutes.
276    *
277    * @param tzOffset timezone offset.
278    */

279   public void setTzOffset(int tzOffset) {
280     this.tzOffset = tzOffset;
281   }
282
283   /**
284    * Set year field.
285    *
286    * @param value year value.
287    */

288   public void setYear(int value) {
289     this.year = value;
290   }
291 }
292
Popular Tags