author | lancea |
Tue, 10 Aug 2010 10:07:33 -0400 | |
changeset 6298 | 470b6c49fe5c |
parent 5506 | 202f599c92aa |
child 6540 | a4ae668f6125 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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) { |
|
6298
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
106 |
final int YEAR_LENGTH = 4; |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
107 |
final int MONTH_LENGTH = 2; |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
108 |
final int DAY_LENGTH = 2; |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
109 |
final int MAX_MONTH = 12; |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
110 |
final int MAX_DAY = 31; |
2 | 111 |
int firstDash; |
112 |
int secondDash; |
|
6298
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
113 |
Date d = null; |
2 | 114 |
|
6298
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
115 |
if (s == null) { |
2 | 116 |
throw new java.lang.IllegalArgumentException(); |
117 |
} |
|
118 |
||
6298
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
119 |
firstDash = s.indexOf('-'); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
120 |
secondDash = s.indexOf('-', firstDash + 1); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
121 |
|
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
122 |
if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) { |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
123 |
String yyyy = s.substring(0, firstDash); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
124 |
String mm = s.substring(firstDash + 1, secondDash); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
125 |
String dd = s.substring(secondDash + 1); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
126 |
if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH && |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
127 |
dd.length() == DAY_LENGTH) { |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
128 |
int year = Integer.parseInt(yyyy); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
129 |
int month = Integer.parseInt(mm); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
130 |
int day = Integer.parseInt(dd); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
131 |
|
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
132 |
if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) { |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
133 |
d = new Date(year - 1900, month - 1, day); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
134 |
} |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
135 |
} |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
136 |
} |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
137 |
if (d == null) { |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
138 |
throw new java.lang.IllegalArgumentException(); |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
139 |
} |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
140 |
|
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
141 |
return d; |
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
142 |
|
2 | 143 |
} |
144 |
||
6298
470b6c49fe5c
6898593: java.sql.Date.valueOf no exception if date given is not in the JDBC date escape syntax
lancea
parents:
5506
diff
changeset
|
145 |
|
2 | 146 |
/** |
147 |
* Formats a date in the date escape format yyyy-mm-dd. |
|
148 |
* <P> |
|
149 |
* @return a String in yyyy-mm-dd format |
|
150 |
*/ |
|
151 |
public String toString () { |
|
152 |
int year = super.getYear() + 1900; |
|
153 |
int month = super.getMonth() + 1; |
|
154 |
int day = super.getDate(); |
|
155 |
||
156 |
char buf[] = "2000-00-00".toCharArray(); |
|
157 |
buf[0] = Character.forDigit(year/1000,10); |
|
158 |
buf[1] = Character.forDigit((year/100)%10,10); |
|
159 |
buf[2] = Character.forDigit((year/10)%10,10); |
|
160 |
buf[3] = Character.forDigit(year%10,10); |
|
161 |
buf[5] = Character.forDigit(month/10,10); |
|
162 |
buf[6] = Character.forDigit(month%10,10); |
|
163 |
buf[8] = Character.forDigit(day/10,10); |
|
164 |
buf[9] = Character.forDigit(day%10,10); |
|
165 |
||
166 |
return new String(buf); |
|
167 |
} |
|
168 |
||
169 |
// Override all the time operations inherited from java.util.Date; |
|
170 |
||
171 |
/** |
|
172 |
* This method is deprecated and should not be used because SQL Date |
|
173 |
* values do not have a time component. |
|
174 |
* |
|
175 |
* @deprecated |
|
176 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
177 |
* @see #setHours |
|
178 |
*/ |
|
179 |
public int getHours() { |
|
180 |
throw new java.lang.IllegalArgumentException(); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* This method is deprecated and should not be used because SQL Date |
|
185 |
* values do not have a time component. |
|
186 |
* |
|
187 |
* @deprecated |
|
188 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
189 |
* @see #setMinutes |
|
190 |
*/ |
|
191 |
public int getMinutes() { |
|
192 |
throw new java.lang.IllegalArgumentException(); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* This method is deprecated and should not be used because SQL Date |
|
197 |
* values do not have a time component. |
|
198 |
* |
|
199 |
* @deprecated |
|
200 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
201 |
* @see #setSeconds |
|
202 |
*/ |
|
203 |
public int getSeconds() { |
|
204 |
throw new java.lang.IllegalArgumentException(); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* This method is deprecated and should not be used because SQL Date |
|
209 |
* values do not have a time component. |
|
210 |
* |
|
211 |
* @deprecated |
|
212 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
213 |
* @see #getHours |
|
214 |
*/ |
|
215 |
public void setHours(int i) { |
|
216 |
throw new java.lang.IllegalArgumentException(); |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* This method is deprecated and should not be used because SQL Date |
|
221 |
* values do not have a time component. |
|
222 |
* |
|
223 |
* @deprecated |
|
224 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
225 |
* @see #getMinutes |
|
226 |
*/ |
|
227 |
public void setMinutes(int i) { |
|
228 |
throw new java.lang.IllegalArgumentException(); |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* This method is deprecated and should not be used because SQL Date |
|
233 |
* values do not have a time component. |
|
234 |
* |
|
235 |
* @deprecated |
|
236 |
* @exception java.lang.IllegalArgumentException if this method is invoked |
|
237 |
* @see #getSeconds |
|
238 |
*/ |
|
239 |
public void setSeconds(int i) { |
|
240 |
throw new java.lang.IllegalArgumentException(); |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Private serial version unique ID to ensure serialization |
|
245 |
* compatibility. |
|
246 |
*/ |
|
247 |
static final long serialVersionUID = 1511598038487230103L; |
|
248 |
} |