author | herrick |
Wed, 20 Nov 2019 10:17:37 -0500 | |
branch | JDK-8200758-branch |
changeset 59160 | e90068e7afa1 |
parent 48065 | c4f2b6749c86 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
2 |
* Copyright (c) 1996, 2016, 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 |
||
15658 | 28 |
import java.time.Instant; |
29 |
import java.time.LocalTime; |
|
30 |
||
2 | 31 |
/** |
32 |
* <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC |
|
33 |
* API to identify this as an SQL <code>TIME</code> value. The <code>Time</code> |
|
34 |
* class adds formatting and |
|
35 |
* parsing operations to support the JDBC escape syntax for time |
|
36 |
* values. |
|
37 |
* <p>The date components should be set to the "zero epoch" |
|
38 |
* value of January 1, 1970 and should not be accessed. |
|
44256 | 39 |
* |
40 |
* @since 1.1 |
|
2 | 41 |
*/ |
42 |
public class Time extends java.util.Date { |
|
43 |
||
44 |
/** |
|
45 |
* Constructs a <code>Time</code> object initialized with the |
|
46 |
* given values for the hour, minute, and second. |
|
47 |
* The driver sets the date components to January 1, 1970. |
|
48 |
* Any method that attempts to access the date components of a |
|
49 |
* <code>Time</code> object will throw a |
|
50 |
* <code>java.lang.IllegalArgumentException</code>. |
|
51 |
* <P> |
|
52 |
* The result is undefined if a given argument is out of bounds. |
|
53 |
* |
|
54 |
* @param hour 0 to 23 |
|
55 |
* @param minute 0 to 59 |
|
56 |
* @param second 0 to 59 |
|
57 |
* |
|
58 |
* @deprecated Use the constructor that takes a milliseconds value |
|
59 |
* in place of this constructor |
|
60 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
61 |
@Deprecated(since="1.2") |
2 | 62 |
public Time(int hour, int minute, int second) { |
63 |
super(70, 0, 1, hour, minute, second); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Constructs a <code>Time</code> object using a milliseconds time value. |
|
68 |
* |
|
69 |
* @param time milliseconds since January 1, 1970, 00:00:00 GMT; |
|
70 |
* a negative number is milliseconds before |
|
71 |
* January 1, 1970, 00:00:00 GMT |
|
72 |
*/ |
|
73 |
public Time(long time) { |
|
74 |
super(time); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Sets a <code>Time</code> object using a milliseconds time value. |
|
79 |
* |
|
80 |
* @param time milliseconds since January 1, 1970, 00:00:00 GMT; |
|
81 |
* a negative number is milliseconds before |
|
82 |
* January 1, 1970, 00:00:00 GMT |
|
83 |
*/ |
|
84 |
public void setTime(long time) { |
|
85 |
super.setTime(time); |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Converts a string in JDBC time escape format to a <code>Time</code> value. |
|
90 |
* |
|
91 |
* @param s time in format "hh:mm:ss" |
|
92 |
* @return a corresponding <code>Time</code> object |
|
93 |
*/ |
|
94 |
public static Time valueOf(String s) { |
|
26596 | 95 |
if (s == null) throw new java.lang.IllegalArgumentException(); |
96 |
||
2 | 97 |
int hour; |
98 |
int minute; |
|
99 |
int second; |
|
26596 | 100 |
int firstColon = s.indexOf(':'); |
101 |
int secondColon = s.indexOf(':', firstColon + 1); |
|
102 |
int len = s.length(); |
|
103 |
if (firstColon > 0 && secondColon > 0 && |
|
104 |
secondColon < len - 1) { |
|
105 |
hour = Integer.parseInt(s, 0, firstColon, 10); |
|
106 |
minute = Integer.parseInt(s, firstColon + 1, secondColon, 10); |
|
107 |
second = Integer.parseInt(s, secondColon + 1, len, 10); |
|
2 | 108 |
} else { |
109 |
throw new java.lang.IllegalArgumentException(); |
|
110 |
} |
|
111 |
||
112 |
return new Time(hour, minute, second); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Formats a time in JDBC time escape format. |
|
117 |
* |
|
118 |
* @return a <code>String</code> in hh:mm:ss format |
|
119 |
*/ |
|
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
5506
diff
changeset
|
120 |
@SuppressWarnings("deprecation") |
2 | 121 |
public String toString () { |
122 |
int hour = super.getHours(); |
|
123 |
int minute = super.getMinutes(); |
|
124 |
int second = super.getSeconds(); |
|
125 |
||
26597 | 126 |
char[] buf = new char[8]; |
127 |
Date.formatDecimalInt(hour, buf, 0, 2); |
|
128 |
buf[2] = ':'; |
|
129 |
Date.formatDecimalInt(minute, buf, 3, 2); |
|
130 |
buf[5] = ':'; |
|
131 |
Date.formatDecimalInt(second, buf, 6, 2); |
|
132 |
||
48065
c4f2b6749c86
8176188: jdk/internal/misc/JavaLangAccess/NewUnsafeString.java failing since 9-b93
redestad
parents:
47216
diff
changeset
|
133 |
return new String(buf); |
2 | 134 |
} |
135 |
||
136 |
// Override all the date operations inherited from java.util.Date; |
|
137 |
||
138 |
/** |
|
139 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
140 |
* values do not have a year component. |
|
141 |
* |
|
142 |
* @deprecated |
|
143 |
* @exception java.lang.IllegalArgumentException if this |
|
144 |
* method is invoked |
|
145 |
* @see #setYear |
|
146 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
147 |
@Deprecated(since="1.2") |
2 | 148 |
public int getYear() { |
149 |
throw new java.lang.IllegalArgumentException(); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
154 |
* values do not have a month component. |
|
155 |
* |
|
156 |
* @deprecated |
|
157 |
* @exception java.lang.IllegalArgumentException if this |
|
158 |
* method is invoked |
|
159 |
* @see #setMonth |
|
160 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
161 |
@Deprecated(since="1.2") |
2 | 162 |
public int getMonth() { |
163 |
throw new java.lang.IllegalArgumentException(); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
168 |
* values do not have a day component. |
|
169 |
* |
|
170 |
* @deprecated |
|
171 |
* @exception java.lang.IllegalArgumentException if this |
|
172 |
* method is invoked |
|
173 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
174 |
@Deprecated(since="1.2") |
2 | 175 |
public int getDay() { |
176 |
throw new java.lang.IllegalArgumentException(); |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
181 |
* values do not have a date component. |
|
182 |
* |
|
183 |
* @deprecated |
|
184 |
* @exception java.lang.IllegalArgumentException if this |
|
185 |
* method is invoked |
|
186 |
* @see #setDate |
|
187 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
188 |
@Deprecated(since="1.2") |
2 | 189 |
public int getDate() { |
190 |
throw new java.lang.IllegalArgumentException(); |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
195 |
* values do not have a year component. |
|
196 |
* |
|
197 |
* @deprecated |
|
198 |
* @exception java.lang.IllegalArgumentException if this |
|
199 |
* method is invoked |
|
200 |
* @see #getYear |
|
201 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
202 |
@Deprecated(since="1.2") |
2 | 203 |
public void setYear(int i) { |
204 |
throw new java.lang.IllegalArgumentException(); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
209 |
* values do not have a month component. |
|
210 |
* |
|
211 |
* @deprecated |
|
212 |
* @exception java.lang.IllegalArgumentException if this |
|
213 |
* method is invoked |
|
214 |
* @see #getMonth |
|
215 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
216 |
@Deprecated(since="1.2") |
2 | 217 |
public void setMonth(int i) { |
218 |
throw new java.lang.IllegalArgumentException(); |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* This method is deprecated and should not be used because SQL <code>TIME</code> |
|
223 |
* values do not have a date component. |
|
224 |
* |
|
225 |
* @deprecated |
|
226 |
* @exception java.lang.IllegalArgumentException if this |
|
227 |
* method is invoked |
|
228 |
* @see #getDate |
|
229 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
230 |
@Deprecated(since="1.2") |
2 | 231 |
public void setDate(int i) { |
232 |
throw new java.lang.IllegalArgumentException(); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Private serial version unique ID to ensure serialization |
|
237 |
* compatibility. |
|
238 |
*/ |
|
239 |
static final long serialVersionUID = 8397324403548013681L; |
|
15658 | 240 |
|
241 |
/** |
|
242 |
* Obtains an instance of {@code Time} from a {@link LocalTime} object |
|
243 |
* with the same hour, minute and second time value as the given |
|
23707
4580f737d5b9
8038653: Fix Time.toLocalTime, Time.valueOf javadoc clarification
lancea
parents:
23010
diff
changeset
|
244 |
* {@code LocalTime}. The nanosecond field from {@code LocalTime} is |
4580f737d5b9
8038653: Fix Time.toLocalTime, Time.valueOf javadoc clarification
lancea
parents:
23010
diff
changeset
|
245 |
* not part of the newly created {@code Time} object. |
15658 | 246 |
* |
247 |
* @param time a {@code LocalTime} to convert |
|
248 |
* @return a {@code Time} object |
|
249 |
* @exception NullPointerException if {@code time} is null |
|
250 |
* @since 1.8 |
|
251 |
*/ |
|
252 |
@SuppressWarnings("deprecation") |
|
253 |
public static Time valueOf(LocalTime time) { |
|
254 |
return new Time(time.getHour(), time.getMinute(), time.getSecond()); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Converts this {@code Time} object to a {@code LocalTime}. |
|
259 |
* <p> |
|
260 |
* The conversion creates a {@code LocalTime} that represents the same |
|
23707
4580f737d5b9
8038653: Fix Time.toLocalTime, Time.valueOf javadoc clarification
lancea
parents:
23010
diff
changeset
|
261 |
* hour, minute, and second time value as this {@code Time}. The |
4580f737d5b9
8038653: Fix Time.toLocalTime, Time.valueOf javadoc clarification
lancea
parents:
23010
diff
changeset
|
262 |
* nanosecond {@code LocalTime} field will be set to zero. |
15658 | 263 |
* |
264 |
* @return a {@code LocalTime} object representing the same time value |
|
265 |
* @since 1.8 |
|
266 |
*/ |
|
267 |
@SuppressWarnings("deprecation") |
|
268 |
public LocalTime toLocalTime() { |
|
269 |
return LocalTime.of(getHours(), getMinutes(), getSeconds()); |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* This method always throws an UnsupportedOperationException and should |
|
274 |
* not be used because SQL {@code Time} values do not have a date |
|
275 |
* component. |
|
276 |
* |
|
277 |
* @exception java.lang.UnsupportedOperationException if this method is invoked |
|
278 |
*/ |
|
279 |
@Override |
|
280 |
public Instant toInstant() { |
|
281 |
throw new java.lang.UnsupportedOperationException(); |
|
282 |
} |
|
2 | 283 |
} |