KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > datatypes > DateTime


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program 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 program 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 program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.schema.datatypes;
24
25 import java.util.Date JavaDoc;
26
27 /**
28  * This class represents the actual value of a date- and time-related XML type. It extends the
29  * standard java.util.Date class with optional time zone information. When specified, the time zone
30  * information allows the DateTime object to represents an absolute date
31  */

32 public class DateTime extends Date JavaDoc {
33     
34     public static final int MAX_TIMEZONE_OFFSET = 14 * 60 * 60 * 1000;
35     /**
36      * tz is the difference with GMT in milliseconds.
37      */

38     private long tz = 0;
39     /**
40      * tzSpecified is true when timezone is known.
41      */

42     private boolean tzSpecified = false;
43
44     /**
45      * @param millis milliseconds since "epoch"
46      */

47     public DateTime(long millis) {
48         this(millis, 0, false);
49     }
50     
51     /**
52      *
53      * @param millis milliseconds since epoch
54      * @param tz timezone difference with GMT in milliseconds
55      */

56     public DateTime(long millis, long tz) {
57         this(millis, tz, true);
58     }
59     
60     /**
61      *
62      * @param millis milliseconds since epoch
63      * @param tz timezone difference with GMT in milliseconds
64      * @param tzSpecified whether tz is significant or not
65      */

66     public DateTime(long millis, long tz, boolean tzSpecified) {
67         super(millis-tz);
68         this.tz = tz;
69         this.tzSpecified = tzSpecified;
70     }
71     
72     public boolean equals(Object JavaDoc obj) {
73         if (this == obj) return true;
74         if (!checkClass(obj)) return false;
75         return this.tzSpecified == ((DateTime)obj).tzSpecified && this.getTime() == ((DateTime)obj).getTime();
76     }
77
78     protected boolean checkClass(Object JavaDoc obj) {
79         return obj instanceof DateTime;
80     }
81     
82     public boolean hasTimeZone() {
83         return tzSpecified;
84     }
85     
86     public long getTimeZone() {
87         return tz;
88     }
89     /**
90      * Implements the compareTo method of interface Comparable
91      * @param obj The object to be compared to this object. It must be of class
92      * java.util.Date or of a subclass. Objects which are not of class DateTime or of a
93      * subclass are considered as being without timezone information.
94      * @return -1 if this object is smaller than obj, 0 if it is equal, and 1 if it is greater
95      * @exception ClassCastException if the object to be compared is not castable to a Date
96      * @exception IllegalArgumentException if the comparison between the two Date objects is indeterminate
97      * (see XML Schema Datatypes specification for details)
98      */

99     public int compareTo(Object JavaDoc obj) throws ClassCastException JavaDoc, IllegalArgumentException JavaDoc {
100         return compareTo((Date JavaDoc) obj);
101     }
102
103     public int compareTo(Date JavaDoc other) throws IllegalArgumentException JavaDoc {
104         long min1, min2, max1, max2;
105         min1 = max1 = getTime();
106         min2 = max2 = other.getTime();
107         boolean tz1 = tzSpecified;
108         boolean tz2 = (other instanceof DateTime) && ((DateTime)other).tzSpecified;
109         if (tz1 == tz2) {
110             if (min1 < min2) return -1;
111             else if (min1 > min2) return 1;
112             else return 0;
113         } else if (!tz1) {
114             min1 -= MAX_TIMEZONE_OFFSET;
115             max1 += MAX_TIMEZONE_OFFSET;
116         } else {
117             min2 -= MAX_TIMEZONE_OFFSET;
118             max2 += MAX_TIMEZONE_OFFSET;
119         }
120         if (max1 < min2) return -1;
121         else if (max2 < min1) return 1;
122         else throw new IllegalArgumentException JavaDoc("Indeterminate comparison result for these arguments");
123     }
124 }
125
Popular Tags