author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44256 | jdk/src/java.sql/share/classes/java/sql/Timestamp.java@12050b22e372 |
child 48065 | c4f2b6749c86 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43218 | 2 |
* Copyright (c) 1996, 2017, 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.LocalDateTime; |
|
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
32210
diff
changeset
|
30 |
import jdk.internal.misc.SharedSecrets; |
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
32210
diff
changeset
|
31 |
import jdk.internal.misc.JavaLangAccess; |
2 | 32 |
|
33 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
34 |
* <P>A thin wrapper around {@code java.util.Date} that allows |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
35 |
* the JDBC API to identify this as an SQL {@code TIMESTAMP} value. |
2 | 36 |
* It adds the ability |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
37 |
* to hold the SQL {@code TIMESTAMP} fractional seconds value, by allowing |
2 | 38 |
* the specification of fractional seconds to a precision of nanoseconds. |
39 |
* A Timestamp also provides formatting and |
|
40 |
* parsing operations to support the JDBC escape syntax for timestamp values. |
|
41 |
* |
|
42 |
* <p>The precision of a Timestamp object is calculated to be either: |
|
43 |
* <ul> |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
44 |
* <li>{@code 19 }, which is the number of characters in yyyy-mm-dd hh:mm:ss |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
45 |
* <li> {@code 20 + s }, which is the number |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
46 |
* of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and {@code s} represents the scale of the given Timestamp, |
2 | 47 |
* its fractional seconds precision. |
48 |
*</ul> |
|
49 |
* |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
50 |
* <P><B>Note:</B> This type is a composite of a {@code java.util.Date} and a |
2 | 51 |
* separate nanoseconds value. Only integral seconds are stored in the |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
52 |
* {@code java.util.Date} component. The fractional seconds - the nanos - are |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
53 |
* separate. The {@code Timestamp.equals(Object)} method never returns |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
54 |
* {@code true} when passed an object |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
55 |
* that isn't an instance of {@code java.sql.Timestamp}, |
2 | 56 |
* because the nanos component of a date is unknown. |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
57 |
* As a result, the {@code Timestamp.equals(Object)} |
2 | 58 |
* method is not symmetric with respect to the |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
59 |
* {@code java.util.Date.equals(Object)} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
60 |
* method. Also, the {@code hashCode} method uses the underlying |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
61 |
* {@code java.util.Date} |
2 | 62 |
* implementation and therefore does not include nanos in its computation. |
63 |
* <P> |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
64 |
* Due to the differences between the {@code Timestamp} class |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
65 |
* and the {@code java.util.Date} |
2 | 66 |
* class mentioned above, it is recommended that code not view |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
67 |
* {@code Timestamp} values generically as an instance of |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
68 |
* {@code java.util.Date}. The |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
69 |
* inheritance relationship between {@code Timestamp} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
70 |
* and {@code java.util.Date} really |
2 | 71 |
* denotes implementation inheritance, and not type inheritance. |
44256 | 72 |
* |
73 |
* @since 1.1 |
|
2 | 74 |
*/ |
75 |
public class Timestamp extends java.util.Date { |
|
76 |
||
26597 | 77 |
private static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess(); |
78 |
||
2 | 79 |
/** |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
80 |
* Constructs a {@code Timestamp} object initialized |
2 | 81 |
* with the given values. |
82 |
* |
|
83 |
* @param year the year minus 1900 |
|
84 |
* @param month 0 to 11 |
|
85 |
* @param date 1 to 31 |
|
86 |
* @param hour 0 to 23 |
|
87 |
* @param minute 0 to 59 |
|
88 |
* @param second 0 to 59 |
|
89 |
* @param nano 0 to 999,999,999 |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
90 |
* @deprecated instead use the constructor {@code Timestamp(long millis)} |
2 | 91 |
* @exception IllegalArgumentException if the nano argument is out of bounds |
92 |
*/ |
|
41889
54d1ff9312ce
8169020: Add since element to JDBC deprecated methods
lancea
parents:
32834
diff
changeset
|
93 |
@Deprecated(since="1.2") |
2 | 94 |
public Timestamp(int year, int month, int date, |
95 |
int hour, int minute, int second, int nano) { |
|
96 |
super(year, month, date, hour, minute, second); |
|
97 |
if (nano > 999999999 || nano < 0) { |
|
98 |
throw new IllegalArgumentException("nanos > 999999999 or < 0"); |
|
99 |
} |
|
100 |
nanos = nano; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
104 |
* Constructs a {@code Timestamp} object |
2 | 105 |
* using a milliseconds time value. The |
106 |
* integral seconds are stored in the underlying date value; the |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
107 |
* fractional seconds are stored in the {@code nanos} field of |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
108 |
* the {@code Timestamp} object. |
2 | 109 |
* |
110 |
* @param time milliseconds since January 1, 1970, 00:00:00 GMT. |
|
111 |
* A negative number is the number of milliseconds before |
|
112 |
* January 1, 1970, 00:00:00 GMT. |
|
113 |
* @see java.util.Calendar |
|
114 |
*/ |
|
115 |
public Timestamp(long time) { |
|
116 |
super((time/1000)*1000); |
|
117 |
nanos = (int)((time%1000) * 1000000); |
|
118 |
if (nanos < 0) { |
|
119 |
nanos = 1000000000 + nanos; |
|
120 |
super.setTime(((time/1000)-1)*1000); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
125 |
* Sets this {@code Timestamp} object to represent a point in time that is |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
126 |
* {@code time} milliseconds after January 1, 1970 00:00:00 GMT. |
2 | 127 |
* |
128 |
* @param time the number of milliseconds. |
|
129 |
* @see #getTime |
|
130 |
* @see #Timestamp(long time) |
|
131 |
* @see java.util.Calendar |
|
132 |
*/ |
|
133 |
public void setTime(long time) { |
|
134 |
super.setTime((time/1000)*1000); |
|
135 |
nanos = (int)((time%1000) * 1000000); |
|
136 |
if (nanos < 0) { |
|
137 |
nanos = 1000000000 + nanos; |
|
138 |
super.setTime(((time/1000)-1)*1000); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
144 |
* represented by this {@code Timestamp} object. |
2 | 145 |
* |
146 |
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT |
|
147 |
* represented by this date. |
|
148 |
* @see #setTime |
|
149 |
*/ |
|
150 |
public long getTime() { |
|
151 |
long time = super.getTime(); |
|
152 |
return (time + (nanos / 1000000)); |
|
153 |
} |
|
154 |
||
155 |
||
156 |
/** |
|
157 |
* @serial |
|
158 |
*/ |
|
159 |
private int nanos; |
|
160 |
||
161 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
162 |
* Converts a {@code String} object in JDBC timestamp escape format to a |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
163 |
* {@code Timestamp} value. |
2 | 164 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
165 |
* @param s timestamp in format {@code yyyy-[m]m-[d]d hh:mm:ss[.f...]}. The |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
166 |
* fractional seconds may be omitted. The leading zero for {@code mm} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
167 |
* and {@code dd} may also be omitted. |
6540 | 168 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
169 |
* @return corresponding {@code Timestamp} value |
2 | 170 |
* @exception java.lang.IllegalArgumentException if the given argument |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
171 |
* does not have the format {@code yyyy-[m]m-[d]d hh:mm:ss[.f...]} |
2 | 172 |
*/ |
173 |
public static Timestamp valueOf(String s) { |
|
6540 | 174 |
final int YEAR_LENGTH = 4; |
175 |
final int MONTH_LENGTH = 2; |
|
176 |
final int DAY_LENGTH = 2; |
|
177 |
final int MAX_MONTH = 12; |
|
178 |
final int MAX_DAY = 31; |
|
179 |
int year = 0; |
|
180 |
int month = 0; |
|
181 |
int day = 0; |
|
2 | 182 |
int hour; |
183 |
int minute; |
|
184 |
int second; |
|
185 |
int a_nanos = 0; |
|
186 |
int firstDash; |
|
187 |
int secondDash; |
|
188 |
int dividingSpace; |
|
26596 | 189 |
int firstColon; |
190 |
int secondColon; |
|
191 |
int period; |
|
2 | 192 |
String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]"; |
193 |
||
194 |
if (s == null) throw new java.lang.IllegalArgumentException("null string"); |
|
195 |
||
196 |
// Split the string into date and time components |
|
197 |
s = s.trim(); |
|
198 |
dividingSpace = s.indexOf(' '); |
|
26596 | 199 |
if (dividingSpace < 0) { |
2 | 200 |
throw new java.lang.IllegalArgumentException(formatError); |
201 |
} |
|
202 |
||
203 |
// Parse the date |
|
26596 | 204 |
firstDash = s.indexOf('-'); |
205 |
secondDash = s.indexOf('-', firstDash+1); |
|
2 | 206 |
|
207 |
// Parse the time |
|
26596 | 208 |
firstColon = s.indexOf(':', dividingSpace + 1); |
209 |
secondColon = s.indexOf(':', firstColon + 1); |
|
210 |
period = s.indexOf('.', secondColon + 1); |
|
2 | 211 |
|
212 |
// Convert the date |
|
6540 | 213 |
boolean parsedDate = false; |
26596 | 214 |
if (firstDash > 0 && secondDash > 0 && secondDash < dividingSpace - 1) { |
215 |
if (firstDash == YEAR_LENGTH && |
|
216 |
(secondDash - firstDash > 1 && secondDash - firstDash <= MONTH_LENGTH + 1) && |
|
217 |
(dividingSpace - secondDash > 1 && dividingSpace - secondDash <= DAY_LENGTH + 1)) { |
|
218 |
year = Integer.parseInt(s, 0, firstDash, 10); |
|
219 |
month = Integer.parseInt(s, firstDash + 1, secondDash, 10); |
|
220 |
day = Integer.parseInt(s, secondDash + 1, dividingSpace, 10); |
|
6540 | 221 |
|
222 |
if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) { |
|
223 |
parsedDate = true; |
|
224 |
} |
|
225 |
} |
|
226 |
} |
|
227 |
if (! parsedDate) { |
|
2 | 228 |
throw new java.lang.IllegalArgumentException(formatError); |
229 |
} |
|
230 |
||
231 |
// Convert the time; default missing nanos |
|
26596 | 232 |
int len = s.length(); |
233 |
if (firstColon > 0 && secondColon > 0 && secondColon < len - 1) { |
|
234 |
hour = Integer.parseInt(s, dividingSpace + 1, firstColon, 10); |
|
235 |
minute = Integer.parseInt(s, firstColon + 1, secondColon, 10); |
|
236 |
if (period > 0 && period < len - 1) { |
|
237 |
second = Integer.parseInt(s, secondColon + 1, period, 10); |
|
238 |
int nanoPrecision = len - (period + 1); |
|
239 |
if (nanoPrecision > 9) |
|
2 | 240 |
throw new java.lang.IllegalArgumentException(formatError); |
26596 | 241 |
if (!Character.isDigit(s.charAt(period + 1))) |
2 | 242 |
throw new java.lang.IllegalArgumentException(formatError); |
26596 | 243 |
int tmpNanos = Integer.parseInt(s, period + 1, len, 10); |
244 |
while (nanoPrecision < 9) { |
|
245 |
tmpNanos *= 10; |
|
246 |
nanoPrecision++; |
|
247 |
} |
|
248 |
a_nanos = tmpNanos; |
|
2 | 249 |
} else if (period > 0) { |
250 |
throw new java.lang.IllegalArgumentException(formatError); |
|
251 |
} else { |
|
26596 | 252 |
second = Integer.parseInt(s, secondColon + 1, len, 10); |
2 | 253 |
} |
254 |
} else { |
|
6540 | 255 |
throw new java.lang.IllegalArgumentException(formatError); |
2 | 256 |
} |
257 |
||
6540 | 258 |
return new Timestamp(year - 1900, month - 1, day, hour, minute, second, a_nanos); |
2 | 259 |
} |
260 |
||
261 |
/** |
|
262 |
* Formats a timestamp in JDBC timestamp escape format. |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
263 |
* {@code yyyy-mm-dd hh:mm:ss.fffffffff}, |
43218 | 264 |
* where {@code fffffffff} indicates nanoseconds. |
23590 | 265 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
266 |
* @return a {@code String} object in |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
267 |
* {@code yyyy-mm-dd hh:mm:ss.fffffffff} format |
2 | 268 |
*/ |
11129
f9ad1aadf3fa
7116445: Miscellaneous warnings in the JDBC/RowSet classes
lancea
parents:
9280
diff
changeset
|
269 |
@SuppressWarnings("deprecation") |
26597 | 270 |
public String toString() { |
2 | 271 |
int year = super.getYear() + 1900; |
272 |
int month = super.getMonth() + 1; |
|
273 |
int day = super.getDate(); |
|
274 |
int hour = super.getHours(); |
|
275 |
int minute = super.getMinutes(); |
|
276 |
int second = super.getSeconds(); |
|
277 |
||
26597 | 278 |
int trailingZeros = 0; |
279 |
int tmpNanos = nanos; |
|
280 |
if (tmpNanos == 0) { |
|
281 |
trailingZeros = 8; |
|
2 | 282 |
} else { |
26597 | 283 |
while (tmpNanos % 10 == 0) { |
284 |
tmpNanos /= 10; |
|
285 |
trailingZeros++; |
|
2 | 286 |
} |
287 |
} |
|
288 |
||
26621
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
289 |
// 8058429: To comply with current JCK tests, we need to deal with year |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
290 |
// being any number between 0 and 292278995 |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
291 |
int count = 10000; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
292 |
int yearSize = 4; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
293 |
do { |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
294 |
if (year < count) { |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
295 |
break; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
296 |
} |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
297 |
yearSize++; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
298 |
count *= 10; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
299 |
} while (count < 1000000000); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
300 |
|
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
301 |
char[] buf = new char[25 + yearSize - trailingZeros]; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
302 |
Date.formatDecimalInt(year, buf, 0, yearSize); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
303 |
buf[yearSize] = '-'; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
304 |
Date.formatDecimalInt(month, buf, yearSize + 1, 2); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
305 |
buf[yearSize + 3] = '-'; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
306 |
Date.formatDecimalInt(day, buf, yearSize + 4, 2); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
307 |
buf[yearSize + 6] = ' '; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
308 |
Date.formatDecimalInt(hour, buf, yearSize + 7, 2); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
309 |
buf[yearSize + 9] = ':'; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
310 |
Date.formatDecimalInt(minute, buf, yearSize + 10, 2); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
311 |
buf[yearSize + 12] = ':'; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
312 |
Date.formatDecimalInt(second, buf, yearSize + 13, 2); |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
313 |
buf[yearSize + 15] = '.'; |
91ab384d2f49
8058429: JCK test api/java_sql/Timestamp/descriptions.html start failing after 8058230
redestad
parents:
26597
diff
changeset
|
314 |
Date.formatDecimalInt(tmpNanos, buf, yearSize + 16, 9 - trailingZeros); |
2 | 315 |
|
26597 | 316 |
return jla.newStringUnsafe(buf); |
2 | 317 |
} |
318 |
||
319 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
320 |
* Gets this {@code Timestamp} object's {@code nanos} value. |
2 | 321 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
322 |
* @return this {@code Timestamp} object's fractional seconds component |
2 | 323 |
* @see #setNanos |
324 |
*/ |
|
325 |
public int getNanos() { |
|
326 |
return nanos; |
|
327 |
} |
|
328 |
||
329 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
330 |
* Sets this {@code Timestamp} object's {@code nanos} field |
2 | 331 |
* to the given value. |
332 |
* |
|
333 |
* @param n the new fractional seconds component |
|
334 |
* @exception java.lang.IllegalArgumentException if the given argument |
|
335 |
* is greater than 999999999 or less than 0 |
|
336 |
* @see #getNanos |
|
337 |
*/ |
|
338 |
public void setNanos(int n) { |
|
339 |
if (n > 999999999 || n < 0) { |
|
340 |
throw new IllegalArgumentException("nanos > 999999999 or < 0"); |
|
341 |
} |
|
342 |
nanos = n; |
|
343 |
} |
|
344 |
||
345 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
346 |
* Tests to see if this {@code Timestamp} object is |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
347 |
* equal to the given {@code Timestamp} object. |
2 | 348 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
349 |
* @param ts the {@code Timestamp} value to compare with |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
350 |
* @return {@code true} if the given {@code Timestamp} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
351 |
* object is equal to this {@code Timestamp} object; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
352 |
* {@code false} otherwise |
2 | 353 |
*/ |
354 |
public boolean equals(Timestamp ts) { |
|
355 |
if (super.equals(ts)) { |
|
356 |
if (nanos == ts.nanos) { |
|
357 |
return true; |
|
358 |
} else { |
|
359 |
return false; |
|
360 |
} |
|
361 |
} else { |
|
362 |
return false; |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
367 |
* Tests to see if this {@code Timestamp} object is |
2 | 368 |
* equal to the given object. |
369 |
* |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
370 |
* This version of the method {@code equals} has been added |
2 | 371 |
* to fix the incorrect |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
372 |
* signature of {@code Timestamp.equals(Timestamp)} and to preserve backward |
2 | 373 |
* compatibility with existing class files. |
374 |
* |
|
375 |
* Note: This method is not symmetric with respect to the |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
376 |
* {@code equals(Object)} method in the base class. |
2 | 377 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
378 |
* @param ts the {@code Object} value to compare with |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
379 |
* @return {@code true} if the given {@code Object} is an instance |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
380 |
* of a {@code Timestamp} that |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
381 |
* is equal to this {@code Timestamp} object; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
382 |
* {@code false} otherwise |
2 | 383 |
*/ |
384 |
public boolean equals(java.lang.Object ts) { |
|
385 |
if (ts instanceof Timestamp) { |
|
386 |
return this.equals((Timestamp)ts); |
|
387 |
} else { |
|
388 |
return false; |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
393 |
* Indicates whether this {@code Timestamp} object is |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
394 |
* earlier than the given {@code Timestamp} object. |
2 | 395 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
396 |
* @param ts the {@code Timestamp} value to compare with |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
397 |
* @return {@code true} if this {@code Timestamp} object is earlier; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
398 |
* {@code false} otherwise |
2 | 399 |
*/ |
400 |
public boolean before(Timestamp ts) { |
|
401 |
return compareTo(ts) < 0; |
|
402 |
} |
|
403 |
||
404 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
405 |
* Indicates whether this {@code Timestamp} object is |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
406 |
* later than the given {@code Timestamp} object. |
2 | 407 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
408 |
* @param ts the {@code Timestamp} value to compare with |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
409 |
* @return {@code true} if this {@code Timestamp} object is later; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
410 |
* {@code false} otherwise |
2 | 411 |
*/ |
412 |
public boolean after(Timestamp ts) { |
|
413 |
return compareTo(ts) > 0; |
|
414 |
} |
|
415 |
||
416 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
417 |
* Compares this {@code Timestamp} object to the given |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
418 |
* {@code Timestamp} object. |
2 | 419 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
420 |
* @param ts the {@code Timestamp} object to be compared to |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
421 |
* this {@code Timestamp} object |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
422 |
* @return the value {@code 0} if the two {@code Timestamp} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
423 |
* objects are equal; a value less than {@code 0} if this |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
424 |
* {@code Timestamp} object is before the given argument; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
425 |
* and a value greater than {@code 0} if this |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
426 |
* {@code Timestamp} object is after the given argument. |
2 | 427 |
* @since 1.4 |
428 |
*/ |
|
429 |
public int compareTo(Timestamp ts) { |
|
7974
676d13726d90
7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents:
6540
diff
changeset
|
430 |
long thisTime = this.getTime(); |
676d13726d90
7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents:
6540
diff
changeset
|
431 |
long anotherTime = ts.getTime(); |
676d13726d90
7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents:
6540
diff
changeset
|
432 |
int i = (thisTime<anotherTime ? -1 :(thisTime==anotherTime?0 :1)); |
2 | 433 |
if (i == 0) { |
434 |
if (nanos > ts.nanos) { |
|
435 |
return 1; |
|
436 |
} else if (nanos < ts.nanos) { |
|
437 |
return -1; |
|
438 |
} |
|
439 |
} |
|
440 |
return i; |
|
441 |
} |
|
442 |
||
443 |
/** |
|
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
444 |
* Compares this {@code Timestamp} object to the given |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
445 |
* {@code Date} object. |
2 | 446 |
* |
32210
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
447 |
* @param o the {@code Date} to be compared to |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
448 |
* this {@code Timestamp} object |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
449 |
* @return the value {@code 0} if this {@code Timestamp} object |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
450 |
* and the given object are equal; a value less than {@code 0} |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
451 |
* if this {@code Timestamp} object is before the given argument; |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
452 |
* and a value greater than {@code 0} if this |
958d823579c3
8133480: replace some <tt> tags (obsolete in html5) in core-libs docs
avstepan
parents:
26621
diff
changeset
|
453 |
* {@code Timestamp} object is after the given argument. |
2 | 454 |
* |
455 |
* @since 1.5 |
|
456 |
*/ |
|
457 |
public int compareTo(java.util.Date o) { |
|
458 |
if(o instanceof Timestamp) { |
|
459 |
// When Timestamp instance compare it with a Timestamp |
|
460 |
// Hence it is basically calling this.compareTo((Timestamp))o); |
|
461 |
// Note typecasting is safe because o is instance of Timestamp |
|
462 |
return compareTo((Timestamp)o); |
|
463 |
} else { |
|
464 |
// When Date doing a o.compareTo(this) |
|
465 |
// will give wrong results. |
|
466 |
Timestamp ts = new Timestamp(o.getTime()); |
|
467 |
return this.compareTo(ts); |
|
468 |
} |
|
469 |
} |
|
470 |
||
9280
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
471 |
/** |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
472 |
* {@inheritDoc} |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
473 |
* |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
474 |
* The {@code hashCode} method uses the underlying {@code java.util.Date} |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
475 |
* implementation and therefore does not include nanos in its computation. |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
476 |
* |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
477 |
*/ |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
478 |
@Override |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
479 |
public int hashCode() { |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
480 |
return super.hashCode(); |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
481 |
} |
14b5e598a0fe
7037085: Add hashCode() to Timestamp to address Findbugs warning
lancea
parents:
9035
diff
changeset
|
482 |
|
2 | 483 |
static final long serialVersionUID = 2745179027874758501L; |
484 |
||
15658 | 485 |
private static final int MILLIS_PER_SECOND = 1000; |
486 |
||
487 |
/** |
|
488 |
* Obtains an instance of {@code Timestamp} from a {@code LocalDateTime} |
|
489 |
* object, with the same year, month, day of month, hours, minutes, |
|
490 |
* seconds and nanos date-time value as the provided {@code LocalDateTime}. |
|
491 |
* <p> |
|
492 |
* The provided {@code LocalDateTime} is interpreted as the local |
|
493 |
* date-time in the local time zone. |
|
494 |
* |
|
495 |
* @param dateTime a {@code LocalDateTime} to convert |
|
496 |
* @return a {@code Timestamp} object |
|
497 |
* @exception NullPointerException if {@code dateTime} is null. |
|
498 |
* @since 1.8 |
|
499 |
*/ |
|
500 |
@SuppressWarnings("deprecation") |
|
501 |
public static Timestamp valueOf(LocalDateTime dateTime) { |
|
502 |
return new Timestamp(dateTime.getYear() - 1900, |
|
503 |
dateTime.getMonthValue() - 1, |
|
504 |
dateTime.getDayOfMonth(), |
|
505 |
dateTime.getHour(), |
|
506 |
dateTime.getMinute(), |
|
507 |
dateTime.getSecond(), |
|
508 |
dateTime.getNano()); |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Converts this {@code Timestamp} object to a {@code LocalDateTime}. |
|
513 |
* <p> |
|
514 |
* The conversion creates a {@code LocalDateTime} that represents the |
|
515 |
* same year, month, day of month, hours, minutes, seconds and nanos |
|
516 |
* date-time value as this {@code Timestamp} in the local time zone. |
|
517 |
* |
|
518 |
* @return a {@code LocalDateTime} object representing the same date-time value |
|
519 |
* @since 1.8 |
|
520 |
*/ |
|
521 |
@SuppressWarnings("deprecation") |
|
522 |
public LocalDateTime toLocalDateTime() { |
|
523 |
return LocalDateTime.of(getYear() + 1900, |
|
524 |
getMonth() + 1, |
|
525 |
getDate(), |
|
526 |
getHours(), |
|
527 |
getMinutes(), |
|
528 |
getSeconds(), |
|
529 |
getNanos()); |
|
530 |
} |
|
531 |
||
532 |
/** |
|
533 |
* Obtains an instance of {@code Timestamp} from an {@link Instant} object. |
|
534 |
* <p> |
|
535 |
* {@code Instant} can store points on the time-line further in the future |
|
536 |
* and further in the past than {@code Date}. In this scenario, this method |
|
537 |
* will throw an exception. |
|
538 |
* |
|
539 |
* @param instant the instant to convert |
|
540 |
* @return an {@code Timestamp} representing the same point on the time-line as |
|
541 |
* the provided instant |
|
542 |
* @exception NullPointerException if {@code instant} is null. |
|
543 |
* @exception IllegalArgumentException if the instant is too large to |
|
25976
4de01a56e3ee
8054555: javadoc cleanup for java.sql and javax.sql
lancea
parents:
23590
diff
changeset
|
544 |
* represent as a {@code Timestamp} |
15658 | 545 |
* @since 1.8 |
546 |
*/ |
|
547 |
public static Timestamp from(Instant instant) { |
|
548 |
try { |
|
549 |
Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND); |
|
550 |
stamp.nanos = instant.getNano(); |
|
551 |
return stamp; |
|
552 |
} catch (ArithmeticException ex) { |
|
553 |
throw new IllegalArgumentException(ex); |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* Converts this {@code Timestamp} object to an {@code Instant}. |
|
559 |
* <p> |
|
560 |
* The conversion creates an {@code Instant} that represents the same |
|
561 |
* point on the time-line as this {@code Timestamp}. |
|
562 |
* |
|
563 |
* @return an instant representing the same point on the time-line |
|
564 |
* @since 1.8 |
|
565 |
*/ |
|
566 |
@Override |
|
567 |
public Instant toInstant() { |
|
568 |
return Instant.ofEpochSecond(super.getTime() / MILLIS_PER_SECOND, nanos); |
|
569 |
} |
|
2 | 570 |
} |