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