View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2000 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$ Date Author Changes 10/02/2002 Arnaud Clean up 04/18/2002 Arnaud String constructor
34   * 05/23/2001 Arnaud Blandin Update to be compliant with XML Schema Recommendation 11/16/2000 Arnaud
35   * Blandin Constructor Date(java.util.Date) 11/01/2000 Arnaud Blandin Enhancements (constructor,
36   * methods access...) 10/23/2000 Arnaud Blandin Created
37   */
38  package org.exolab.castor.types;
39  
40  import java.text.ParseException;
41  import java.util.Calendar;
42  import java.util.GregorianCalendar;
43  
44  /**
45   * Describe an XML Schema Date.
46   * <p>
47   * The format is defined by W3C XML Schema Recommendation and ISO8601 i.e
48   * <tt>(-)CCYY-MM-DD(Z|(+|-)hh:mm)</tt>
49   *
50   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
51   * @author <a href="mailto:edward.kuns@aspect.com">Edward Kuns</a>
52   * @version $Revision$
53   */
54  public class Date extends DateTimeBase {
55    /** SerialVersionUID */
56    private static final long serialVersionUID = -1634875709019365137L;
57    /** Complaint string. */
58    private static final String BAD_DATE = "Bad Date format: ";
59  
60    /**
61     * No-arg constructor.
62     */
63    public Date() {
64      // Nothing to do
65    }
66  
67    /**
68     * Constructs a XML Schema Date instance given all the values of the different fields. By default
69     * a Date is not UTC and is local.
70     *
71     * @param values an array of shorts that represent the different fields of Time.
72     */
73    public Date(short[] values) {
74      setValues(values);
75    }
76  
77    /**
78     * This constructor is used to convert a long value representing a Date to a new
79     * org.exolab.castor.types.Date instance.
80     * <p>
81     * Note : all the information concerning the time part of the java.util.Date is lost since a W3C
82     * Schema Date only represents CCYY-MM-YY
83     *
84     * @param dateAsLong Date represented in from of a long value.
85     */
86    public Date(long dateAsLong) {
87      this(new java.util.Date(dateAsLong));
88    }
89  
90    /**
91     * This constructor is used to convert a java.util.Date into a new org.exolab.castor.types.Date.
92     * <p>
93     * Note : all the information concerning the time part of the java.util.Date is lost since a W3C
94     * Schema Date only represents CCYY-MM-YY.
95     *
96     * @param dateRef the java.util.Date to use to construct a new org.exolab.castor.types.Date
97     */
98    public Date(java.util.Date dateRef) {
99      GregorianCalendar tempCalendar = new GregorianCalendar();
100     tempCalendar.setTime(dateRef);
101     setCentury((short) (tempCalendar.get(Calendar.YEAR) / 100));
102     setYear((short) (tempCalendar.get(Calendar.YEAR) % 100));
103 
104     // we need to add 1 to the Month value returned by GregorianCalendar
105     // because 0<MONTH<11 (i.e January is 0)
106     setMonth((short) (tempCalendar.get(Calendar.MONTH) + 1));
107     setDay((short) tempCalendar.get(Calendar.DAY_OF_MONTH));
108   } // Date(java.util.Date)
109 
110   /**
111    * Constructs a date from a string.
112    * 
113    * @param date the string representing the date
114    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
115    *         right format (see the description of this class)
116    */
117   public Date(String date) throws java.text.ParseException {
118     parseDateInternal(date, this);
119   }
120 
121   /**
122    * Sets all the fields by reading the values in an array.
123    * <p>
124    * If a Time Zone is specified, it has to be set by using
125    * {@link DateTimeBase#setZone(short, short) setZone}.
126    *
127    * @param values an array of shorts with the values the array is supposed to be of length 4 and
128    *        ordered like the following:
129    *        <ul>
130    *        <li>century</li>
131    *        <li>year</li>
132    *        <li>month</li>
133    *        <li>day</li>
134    *        </ul>
135    */
136   public void setValues(short[] values) {
137     if (values.length != 4) {
138       throw new IllegalArgumentException("Date#setValues: not the right number of values");
139     }
140     this.setCentury(values[0]);
141     this.setYear(values[1]);
142     this.setMonth(values[2]);
143     this.setDay(values[3]);
144   }
145 
146   /**
147    * Returns an array of short with all the fields that describe this Date type.
148    * <p>
149    * Note:the time zone is not included.
150    *
151    * @return an array of short with all the fields that describe this Date type.
152    */
153   public short[] getValues() {
154     short[] result = new short[4];
155     result[0] = this.getCentury();
156     result[1] = this.getYear();
157     result[2] = this.getMonth();
158     result[3] = this.getDay();
159     return result;
160   } // getValues
161 
162   /**
163    * Converts this Date into a local java.util.Date.
164    * 
165    * @return a local date representing this Date.
166    */
167   public java.util.Date toDate() {
168     Calendar calendar =
169         new GregorianCalendar(getCentury() * 100 + getYear(), getMonth() - 1, getDay());
170     setDateFormatTimeZone(calendar);
171     return calendar.getTime();
172   } // toDate()
173 
174   /**
175    * Converts this date into a long value.
176    * 
177    * @return This date instance as a long value.
178    */
179   public long toLong() {
180     return this.toDate().getTime();
181   }
182 
183   /**
184    * convert this Date to a string The format is defined by W3C XML Schema recommendation and
185    * ISO8601 i.e (+|-)CCYY-MM-DD
186    * 
187    * @return a string representing this Date
188    */
189   public String toString() {
190     StringBuffer result = new StringBuffer();
191 
192     appendDateString(result);
193     appendTimeZoneString(result);
194 
195     return result.toString();
196   } // toString
197 
198   /**
199    * parse a String and convert it into an java.lang.Object
200    * 
201    * @param str the string to parse
202    * @return an Object represented by the string
203    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
204    *         right format (see the description of this class)
205    */
206   public static Object parse(String str) throws ParseException {
207     return parseDate(str);
208   }
209 
210   /**
211    * parse a String and convert it into a Date.
212    * 
213    * @param str the string to parse
214    * @return the Date represented by the string
215    * @throws ParseException a parse exception is thrown if the string to parse does not follow the
216    *         right format (see the description of this class)
217    */
218   public static Date parseDate(String str) throws ParseException {
219     Date result = new Date();
220     return parseDateInternal(str, result);
221   }
222 
223   private static Date parseDateInternal(String str, Date result) throws ParseException {
224     if (str == null) {
225       throw new IllegalArgumentException("The string to be parsed must not be null.");
226     }
227 
228     if (result == null) {
229       result = new Date();
230     }
231 
232     char[] chars = str.toCharArray();
233 
234     // Year
235     int idx = 0;
236     if (chars[idx] == '-') {
237       idx++;
238       result.setNegative();
239     }
240 
241     if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])
242         || !Character.isDigit(chars[idx + 2]) || !Character.isDigit(chars[idx + 3])) {
243       throw new ParseException(BAD_DATE + "'" + str + "'\nThe Year must be 4 digits long", idx);
244     }
245 
246     short value1;
247     short value2;
248 
249     value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
250     value2 = (short) ((chars[idx + 2] - '0') * 10 + (chars[idx + 3] - '0'));
251 
252     if (value1 == 0 && value2 == 0) {
253       throw new ParseException(BAD_DATE + str + "\n'0000' is not allowed as a year.", idx);
254     }
255 
256     result.setCentury(value1);
257     result.setYear(value2);
258 
259     idx += 4;
260 
261     // Month
262     if (chars[idx] != '-') {
263       throw new ParseException(BAD_DATE + "'" + str + "'\n '-' " + DateTimeBase.WRONGLY_PLACED,
264           idx);
265     }
266 
267     idx++;
268 
269     if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])) {
270       throw new ParseException(BAD_DATE + "'" + str + "'\nThe Month must be 2 digits long", idx);
271     }
272 
273     value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
274     result.setMonth(value1);
275 
276     idx += 2;
277 
278     // Day
279     if (chars[idx] != '-') {
280       throw new ParseException(BAD_DATE + "'" + str + "'\n '-' " + DateTimeBase.WRONGLY_PLACED,
281           idx);
282     }
283 
284     idx++;
285 
286     if (!Character.isDigit(chars[idx]) || !Character.isDigit(chars[idx + 1])) {
287       throw new ParseException(BAD_DATE + "'" + str + "'\nThe Day must be 2 digits long", idx);
288     }
289 
290     value1 = (short) ((chars[idx] - '0') * 10 + (chars[idx + 1] - '0'));
291     result.setDay(value1);
292 
293     idx += 2;
294 
295     parseTimeZone(str, result, chars, idx, BAD_DATE);
296 
297     return result;
298   } // parse
299 
300   /////////////////////////// DISALLOWED METHODS ///////////////////////////
301 
302   public boolean hasHour() {
303     return false;
304   }
305 
306   public short getHour() {
307     String err = "org.exolab.castor.types.Date does not have an Hour field.";
308     throw new UnsupportedOperationException(err);
309   }
310 
311   public void setHour(short hour) {
312     String err = "org.exolab.castor.types.Date does not have an Hour field.";
313     throw new UnsupportedOperationException(err);
314   }
315 
316   public boolean hasMinute() {
317     return false;
318   }
319 
320   public short getMinute() {
321     String err = "org.exolab.castor.types.Date does not have a Minute field.";
322     throw new UnsupportedOperationException(err);
323   }
324 
325   public void setMinute(short minute) {
326     String err = "org.exolab.castor.types.Date does not have a Minute field.";
327     throw new UnsupportedOperationException(err);
328   }
329 
330   public boolean hasSeconds() {
331     return false;
332   }
333 
334   public short getSeconds() {
335     String err = "org.exolab.castor.types.Date does not have a Seconds field.";
336     throw new UnsupportedOperationException(err);
337   }
338 
339   public void setSecond(short second) {
340     String err = "org.exolab.castor.types.Date does not have a Seconds field.";
341     throw new UnsupportedOperationException(err);
342   }
343 
344   public boolean hasMilli() {
345     return false;
346   }
347 
348   public short getMilli() {
349     String err = "org.exolab.castor.types.Date does not have a Milliseconds field.";
350     throw new UnsupportedOperationException(err);
351   }
352 
353   public void setMilliSecond(short millisecond) {
354     String err = "org.exolab.castor.types.Date does not have a Milliseconds field.";
355     throw new UnsupportedOperationException(err);
356   }
357 
358 }// Date