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