jdk/src/share/classes/java/sql/Timestamp.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7974 676d13726d90
child 9280 14b5e598a0fe
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7974
diff changeset
     2
 * Copyright (c) 1996, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <P>A thin wrapper around <code>java.util.Date</code> that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * the JDBC API to identify this as an SQL <code>TIMESTAMP</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It adds the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to hold the SQL <code>TIMESTAMP</code> fractional seconds value, by allowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the specification of fractional seconds to a precision of nanoseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A Timestamp also provides formatting and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * parsing operations to support the JDBC escape syntax for timestamp values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>The precision of a Timestamp object is calculated to be either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <li><code>19 </code>, which is the number of characters in yyyy-mm-dd hh:mm:ss
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <li> <code> 20 + s </code>, which is the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * 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
    44
 * its fractional seconds precision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <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
    48
 * separate nanoseconds value. Only integral seconds are stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>java.util.Date</code> component. The fractional seconds - the nanos - are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * separate.  The <code>Timestamp.equals(Object)</code> method never returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>true</code> when passed an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * that isn't an instance of <code>java.sql.Timestamp</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * because the nanos component of a date is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * As a result, the <code>Timestamp.equals(Object)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * method is not symmetric with respect to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <code>java.util.Date.equals(Object)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * method.  Also, the <code>hashcode</code> method uses the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>java.util.Date</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * implementation and therefore does not include nanos in its computation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Due to the differences between the <code>Timestamp</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and the <code>java.util.Date</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * class mentioned above, it is recommended that code not view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>Timestamp</code> values generically as an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>java.util.Date</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * inheritance relationship between <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * and <code>java.util.Date</code> really
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * denotes implementation inheritance, and not type inheritance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class Timestamp extends java.util.Date {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a <code>Timestamp</code> object initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * with the given values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param year the year minus 1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param month 0 to 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param date 1 to 31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param hour 0 to 23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param minute 0 to 59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param second 0 to 59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param nano 0 to 999,999,999
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @deprecated instead use the constructor <code>Timestamp(long millis)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception IllegalArgumentException if the nano argument is out of bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Timestamp(int year, int month, int date,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                     int hour, int minute, int second, int nano) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super(year, month, date, hour, minute, second);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (nano > 999999999 || nano < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new IllegalArgumentException("nanos > 999999999 or < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        nanos = nano;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Constructs a <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * using a milliseconds time value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * integral seconds are stored in the underlying date value; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * fractional seconds are stored in the <code>nanos</code> field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param time milliseconds since January 1, 1970, 00:00:00 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *        A negative number is the number of milliseconds before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *         January 1, 1970, 00:00:00 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see java.util.Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public Timestamp(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        super((time/1000)*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        nanos = (int)((time%1000) * 1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (nanos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            nanos = 1000000000 + nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            super.setTime(((time/1000)-1)*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Sets this <code>Timestamp</code> object to represent a point in time that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param time   the number of milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see #getTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see #Timestamp(long time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see java.util.Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void setTime(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        super.setTime((time/1000)*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        nanos = (int)((time%1000) * 1000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (nanos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            nanos = 1000000000 + nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            super.setTime(((time/1000)-1)*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * represented by this <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return  the number of milliseconds since January 1, 1970, 00:00:00 GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *          represented by this date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see #setTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        long time = super.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return (time + (nanos / 1000000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private int nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Converts a <code>String</code> object in JDBC timestamp escape format to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>Timestamp</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   158
     * @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
   159
     * fractional seconds may be omitted. The leading zero for <code>mm</code>
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   160
     * and <code>dd</code> may also be omitted.
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   161
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return corresponding <code>Timestamp</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception java.lang.IllegalArgumentException if the given argument
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   164
     * does not have the format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public static Timestamp valueOf(String s) {
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   167
        final int YEAR_LENGTH = 4;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   168
        final int MONTH_LENGTH = 2;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   169
        final int DAY_LENGTH = 2;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   170
        final int MAX_MONTH = 12;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   171
        final int MAX_DAY = 31;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        String date_s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        String time_s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        String nanos_s;
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   175
        int year = 0;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   176
        int month = 0;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   177
        int day = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int a_nanos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int firstDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int secondDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int dividingSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int firstColon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int secondColon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int period = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String zeros = "000000000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String delimiterDate = "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        String delimiterTime = ":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (s == null) throw new java.lang.IllegalArgumentException("null string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Split the string into date and time components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        s = s.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        dividingSpace = s.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (dividingSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            date_s = s.substring(0,dividingSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            time_s = s.substring(dividingSpace+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Parse the date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        firstDash = date_s.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        secondDash = date_s.indexOf('-', firstDash+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Parse the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (time_s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        firstColon = time_s.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        secondColon = time_s.indexOf(':', firstColon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        period = time_s.indexOf('.', secondColon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // Convert the date
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   217
        boolean parsedDate = false;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   218
        if ((firstDash > 0) && (secondDash > 0) && (secondDash < date_s.length() - 1)) {
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   219
            String yyyy = date_s.substring(0, firstDash);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   220
            String mm = date_s.substring(firstDash + 1, secondDash);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   221
            String dd = date_s.substring(secondDash + 1);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   222
            if (yyyy.length() == YEAR_LENGTH &&
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   223
                    (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   224
                    (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   225
                 year = Integer.parseInt(yyyy);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   226
                 month = Integer.parseInt(mm);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   227
                 day = Integer.parseInt(dd);
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   228
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   229
                if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   230
                    parsedDate = true;
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   231
                }
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   232
            }
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   233
        }
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   234
        if (! parsedDate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // Convert the time; default missing nanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if ((firstColon > 0) & (secondColon > 0) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            (secondColon < time_s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            hour = Integer.parseInt(time_s.substring(0, firstColon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            minute =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                Integer.parseInt(time_s.substring(firstColon+1, secondColon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if ((period > 0) & (period < time_s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                second =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    Integer.parseInt(time_s.substring(secondColon+1, period));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                nanos_s = time_s.substring(period+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (nanos_s.length() > 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (!Character.isDigit(nanos_s.charAt(0)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                a_nanos = Integer.parseInt(nanos_s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } else if (period > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                second = Integer.parseInt(time_s.substring(secondColon+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else {
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   260
            throw new java.lang.IllegalArgumentException(formatError);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   263
        return new Timestamp(year - 1900, month - 1, day, hour, minute, second, a_nanos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Formats a timestamp in JDBC timestamp escape format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *         <code>yyyy-mm-dd hh:mm:ss.fffffffff</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * where <code>ffffffffff</code> indicates nanoseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return a <code>String</code> object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *           <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public String toString () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int year = super.getYear() + 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int month = super.getMonth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int day = super.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int hour = super.getHours();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int minute = super.getMinutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        int second = super.getSeconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        String yearString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String monthString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        String dayString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String hourString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        String minuteString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        String secondString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        String nanosString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        String zeros = "000000000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        String yearZeros = "0000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        StringBuffer timestampBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (year < 1000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // Add leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            yearString = "" + year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            yearString = yearZeros.substring(0, (4-yearString.length())) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                yearString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            yearString = "" + year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (month < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            monthString = "0" + month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            monthString = Integer.toString(month);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (day < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            dayString = "0" + day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            dayString = Integer.toString(day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (hour < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            hourString = "0" + hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            hourString = Integer.toString(hour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (minute < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            minuteString = "0" + minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            minuteString = Integer.toString(minute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (second < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            secondString = "0" + second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            secondString = Integer.toString(second);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (nanos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            nanosString = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            nanosString = Integer.toString(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // Add leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            nanosString = zeros.substring(0, (9-nanosString.length())) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                nanosString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // Truncate trailing zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            char[] nanosChar = new char[nanosString.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            nanosString.getChars(0, nanosString.length(), nanosChar, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int truncIndex = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            while (nanosChar[truncIndex] == '0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                truncIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            nanosString = new String(nanosChar, 0, truncIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // do a string buffer here instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        timestampBuf = new StringBuffer(20+nanosString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        timestampBuf.append(yearString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        timestampBuf.append("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        timestampBuf.append(monthString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        timestampBuf.append("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        timestampBuf.append(dayString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        timestampBuf.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        timestampBuf.append(hourString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        timestampBuf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        timestampBuf.append(minuteString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        timestampBuf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        timestampBuf.append(secondString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        timestampBuf.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        timestampBuf.append(nanosString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return (timestampBuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Gets this <code>Timestamp</code> object's <code>nanos</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return this <code>Timestamp</code> object's fractional seconds component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @see #setNanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public int getNanos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Sets this <code>Timestamp</code> object's <code>nanos</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param n the new fractional seconds component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @exception java.lang.IllegalArgumentException if the given argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *            is greater than 999999999 or less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #getNanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public void setNanos(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (n > 999999999 || n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new IllegalArgumentException("nanos > 999999999 or < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        nanos = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Tests to see if this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * equal to the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @return <code>true</code> if the given <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *         object is equal to this <code>Timestamp</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public boolean equals(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (super.equals(ts)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if  (nanos == ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Tests to see if this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * equal to the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * This version of the method <code>equals</code> has been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * to fix the incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * signature of <code>Timestamp.equals(Timestamp)</code> and to preserve backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * compatibility with existing class files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Note: This method is not symmetric with respect to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>equals(Object)</code> method in the base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param ts the <code>Object</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return <code>true</code> if the given <code>Object</code> is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *         of a <code>Timestamp</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *         is equal to this <code>Timestamp</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public boolean equals(java.lang.Object ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
      if (ts instanceof Timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return this.equals((Timestamp)ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Indicates whether this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * earlier than the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return <code>true</code> if this <code>Timestamp</code> object is earlier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public boolean before(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return compareTo(ts) < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Indicates whether this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * later than the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @return <code>true</code> if this <code>Timestamp</code> object is later;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public boolean after(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return compareTo(ts) > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Compares this <code>Timestamp</code> object to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param   ts   the <code>Timestamp</code> object to be compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *                this <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return  the value <code>0</code> if the two <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *          objects are equal; a value less than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          <code>Timestamp</code> object is before the given argument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          and a value greater than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          <code>Timestamp</code> object is after the given argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public int compareTo(Timestamp ts) {
7974
676d13726d90 7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents: 6540
diff changeset
   476
        long thisTime = this.getTime();
676d13726d90 7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents: 6540
diff changeset
   477
        long anotherTime = ts.getTime();
676d13726d90 7000693: java.sql.Timestamp compareTo() issues using low values
lancea
parents: 6540
diff changeset
   478
        int i = (thisTime<anotherTime ? -1 :(thisTime==anotherTime?0 :1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (nanos > ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            } else if (nanos < ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Compares this <code>Timestamp</code> object to the given
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   492
     * <code>Date</code> object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
6540
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   494
     * @param o the <code>Date</code> to be compared to
a4ae668f6125 6589685: JDBC 4.1 updates
lancea
parents: 5506
diff changeset
   495
     *          this <code>Timestamp</code> object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @return  the value <code>0</code> if this <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *          and the given object are equal; a value less than <code>0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *          if this  <code>Timestamp</code> object is before the given argument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *          and a value greater than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *          <code>Timestamp</code> object is after the given argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public int compareTo(java.util.Date o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
       if(o instanceof Timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // When Timestamp instance compare it with a Timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // Hence it is basically calling this.compareTo((Timestamp))o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // Note typecasting is safe because o is instance of Timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
           return compareTo((Timestamp)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // When Date doing a o.compareTo(this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // will give wrong results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
          Timestamp ts = new Timestamp(o.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
          return this.compareTo(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    static final long serialVersionUID = 2745179027874758501L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}