2
|
1 |
/*
|
|
2 |
* Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.sql;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* <P>A thin wrapper around a millisecond value that allows
|
|
30 |
* JDBC to identify this as an SQL <code>DATE</code> value. A
|
|
31 |
* milliseconds value represents the number of milliseconds that
|
|
32 |
* have passed since January 1, 1970 00:00:00.000 GMT.
|
|
33 |
* <p>
|
|
34 |
* To conform with the definition of SQL <code>DATE</code>, the
|
|
35 |
* millisecond values wrapped by a <code>java.sql.Date</code> instance
|
|
36 |
* must be 'normalized' by setting the
|
|
37 |
* hours, minutes, seconds, and milliseconds to zero in the particular
|
|
38 |
* time zone with which the instance is associated.
|
|
39 |
*/
|
|
40 |
public class Date extends java.util.Date {
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Constructs a <code>Date</code> object initialized with the given
|
|
44 |
* year, month, and day.
|
|
45 |
* <P>
|
|
46 |
* The result is undefined if a given argument is out of bounds.
|
|
47 |
*
|
|
48 |
* @param year the year minus 1900; must be 0 to 8099. (Note that
|
|
49 |
* 8099 is 9999 minus 1900.)
|
|
50 |
* @param month 0 to 11
|
|
51 |
* @param day 1 to 31
|
|
52 |
* @deprecated instead use the constructor <code>Date(long date)</code>
|
|
53 |
*/
|
|
54 |
public Date(int year, int month, int day) {
|
|
55 |
super(year, month, day);
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Constructs a <code>Date</code> object using the given milliseconds
|
|
60 |
* time value. If the given milliseconds value contains time
|
|
61 |
* information, the driver will set the time components to the
|
|
62 |
* time in the default time zone (the time zone of the Java virtual
|
|
63 |
* machine running the application) that corresponds to zero GMT.
|
|
64 |
*
|
|
65 |
* @param date milliseconds since January 1, 1970, 00:00:00 GMT not
|
|
66 |
* to exceed the milliseconds representation for the year 8099.
|
|
67 |
* A negative number indicates the number of milliseconds
|
|
68 |
* before January 1, 1970, 00:00:00 GMT.
|
|
69 |
*/
|
|
70 |
public Date(long date) {
|
|
71 |
// If the millisecond date value contains time info, mask it out.
|
|
72 |
super(date);
|
|
73 |
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Sets an existing <code>Date</code> object
|
|
78 |
* using the given milliseconds time value.
|
|
79 |
* If the given milliseconds value contains time information,
|
|
80 |
* the driver will set the time components to the
|
|
81 |
* time in the default time zone (the time zone of the Java virtual
|
|
82 |
* machine running the application) that corresponds to zero GMT.
|
|
83 |
*
|
|
84 |
* @param date milliseconds since January 1, 1970, 00:00:00 GMT not
|
|
85 |
* to exceed the milliseconds representation for the year 8099.
|
|
86 |
* A negative number indicates the number of milliseconds
|
|
87 |
* before January 1, 1970, 00:00:00 GMT.
|
|
88 |
*/
|
|
89 |
public void setTime(long date) {
|
|
90 |
// If the millisecond date value contains time info, mask it out.
|
|
91 |
super.setTime(date);
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Converts a string in JDBC date escape format to
|
|
96 |
* a <code>Date</code> value.
|
|
97 |
*
|
|
98 |
* @param s a <code>String</code> object representing a date in
|
|
99 |
* in the format "yyyy-mm-dd"
|
|
100 |
* @return a <code>java.sql.Date</code> object representing the
|
|
101 |
* given date
|
|
102 |
* @throws IllegalArgumentException if the date given is not in the
|
|
103 |
* JDBC date escape format (yyyy-mm-dd)
|
|
104 |
*/
|
|
105 |
public static Date valueOf(String s) {
|
|
106 |
int year;
|
|
107 |
int month;
|
|
108 |
int day;
|
|
109 |
int firstDash;
|
|
110 |
int secondDash;
|
|
111 |
|
|
112 |
if (s == null) throw new java.lang.IllegalArgumentException();
|
|
113 |
|
|
114 |
firstDash = s.indexOf('-');
|
|
115 |
secondDash = s.indexOf('-', firstDash+1);
|
|
116 |
if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
|
|
117 |
year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
|
|
118 |
month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
|
|
119 |
day = Integer.parseInt(s.substring(secondDash+1));
|
|
120 |
} else {
|
|
121 |
throw new java.lang.IllegalArgumentException();
|
|
122 |
}
|
|
123 |
|
|
124 |
return new Date(year, month, day);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Formats a date in the date escape format yyyy-mm-dd.
|
|
129 |
* <P>
|
|
130 |
* @return a String in yyyy-mm-dd format
|
|
131 |
*/
|
|
132 |
public String toString () {
|
|
133 |
int year = super.getYear() + 1900;
|
|
134 |
int month = super.getMonth() + 1;
|
|
135 |
int day = super.getDate();
|
|
136 |
|
|
137 |
char buf[] = "2000-00-00".toCharArray();
|
|
138 |
buf[0] = Character.forDigit(year/1000,10);
|
|
139 |
buf[1] = Character.forDigit((year/100)%10,10);
|
|
140 |
buf[2] = Character.forDigit((year/10)%10,10);
|
|
141 |
buf[3] = Character.forDigit(year%10,10);
|
|
142 |
buf[5] = Character.forDigit(month/10,10);
|
|
143 |
buf[6] = Character.forDigit(month%10,10);
|
|
144 |
buf[8] = Character.forDigit(day/10,10);
|
|
145 |
buf[9] = Character.forDigit(day%10,10);
|
|
146 |
|
|
147 |
return new String(buf);
|
|
148 |
}
|
|
149 |
|
|
150 |
// Override all the time operations inherited from java.util.Date;
|
|
151 |
|
|
152 |
/**
|
|
153 |
* This method is deprecated and should not be used because SQL Date
|
|
154 |
* values do not have a time component.
|
|
155 |
*
|
|
156 |
* @deprecated
|
|
157 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
158 |
* @see #setHours
|
|
159 |
*/
|
|
160 |
public int getHours() {
|
|
161 |
throw new java.lang.IllegalArgumentException();
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* This method is deprecated and should not be used because SQL Date
|
|
166 |
* values do not have a time component.
|
|
167 |
*
|
|
168 |
* @deprecated
|
|
169 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
170 |
* @see #setMinutes
|
|
171 |
*/
|
|
172 |
public int getMinutes() {
|
|
173 |
throw new java.lang.IllegalArgumentException();
|
|
174 |
}
|
|
175 |
|
|
176 |
/**
|
|
177 |
* This method is deprecated and should not be used because SQL Date
|
|
178 |
* values do not have a time component.
|
|
179 |
*
|
|
180 |
* @deprecated
|
|
181 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
182 |
* @see #setSeconds
|
|
183 |
*/
|
|
184 |
public int getSeconds() {
|
|
185 |
throw new java.lang.IllegalArgumentException();
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* This method is deprecated and should not be used because SQL Date
|
|
190 |
* values do not have a time component.
|
|
191 |
*
|
|
192 |
* @deprecated
|
|
193 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
194 |
* @see #getHours
|
|
195 |
*/
|
|
196 |
public void setHours(int i) {
|
|
197 |
throw new java.lang.IllegalArgumentException();
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
|
201 |
* This method is deprecated and should not be used because SQL Date
|
|
202 |
* values do not have a time component.
|
|
203 |
*
|
|
204 |
* @deprecated
|
|
205 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
206 |
* @see #getMinutes
|
|
207 |
*/
|
|
208 |
public void setMinutes(int i) {
|
|
209 |
throw new java.lang.IllegalArgumentException();
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* This method is deprecated and should not be used because SQL Date
|
|
214 |
* values do not have a time component.
|
|
215 |
*
|
|
216 |
* @deprecated
|
|
217 |
* @exception java.lang.IllegalArgumentException if this method is invoked
|
|
218 |
* @see #getSeconds
|
|
219 |
*/
|
|
220 |
public void setSeconds(int i) {
|
|
221 |
throw new java.lang.IllegalArgumentException();
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Private serial version unique ID to ensure serialization
|
|
226 |
* compatibility.
|
|
227 |
*/
|
|
228 |
static final long serialVersionUID = 1511598038487230103L;
|
|
229 |
}
|