jdk/src/share/classes/java/sql/Timestamp.java
author lancea
Sat, 04 Sep 2010 15:37:26 -0400
changeset 6531 30b5903e8301
parent 5506 202f599c92aa
child 6540 a4ae668f6125
permissions -rw-r--r--
6982510: Updated SQLException subclasses from the outback for 6861385 so that the copyrights only have 2 years Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2006, 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param s timestamp in format <code>yyyy-mm-dd hh:mm:ss[.f...]</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * fractional seconds may be omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return corresponding <code>Timestamp</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception java.lang.IllegalArgumentException if the given argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * does not have the format <code>yyyy-mm-dd hh:mm:ss[.f...]</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static Timestamp valueOf(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String date_s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String time_s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        String nanos_s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int a_nanos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int firstDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int secondDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int dividingSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int firstColon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int secondColon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int period = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String zeros = "000000000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String delimiterDate = "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        String delimiterTime = ":";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        StringTokenizer stringTokeninzerDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        StringTokenizer stringTokeninzerTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (s == null) throw new java.lang.IllegalArgumentException("null string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int counterD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int intDate[] = {4,2,2};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int counterT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int intTime[] = {2,2,12};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Split the string into date and time components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        s = s.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        dividingSpace = s.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (dividingSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            date_s = s.substring(0,dividingSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            time_s = s.substring(dividingSpace+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        stringTokeninzerTime = new StringTokenizer(time_s, delimiterTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        stringTokeninzerDate = new StringTokenizer(date_s, delimiterDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        while(stringTokeninzerDate.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
             String tokenDate = stringTokeninzerDate.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
             if(tokenDate.length() != intDate[counterD] ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             counterD++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         //Commenting this portion out for checking of time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        while(stringTokeninzerTime.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             String tokenTime = stringTokeninzerTime.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
             if (counterT < 2 && tokenTime.length() != intTime[counterT]  ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
             counterT++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // Parse the date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        firstDash = date_s.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        secondDash = date_s.indexOf('-', firstDash+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // Parse the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (time_s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        firstColon = time_s.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        secondColon = time_s.indexOf(':', firstColon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        period = time_s.indexOf('.', secondColon+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Convert the date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if ((firstDash > 0) && (secondDash > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            (secondDash < date_s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            month =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                Integer.parseInt(date_s.substring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                 (firstDash+1, secondDash)) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            day = Integer.parseInt(date_s.substring(secondDash+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // Convert the time; default missing nanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if ((firstColon > 0) & (secondColon > 0) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            (secondColon < time_s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            hour = Integer.parseInt(time_s.substring(0, firstColon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            minute =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                Integer.parseInt(time_s.substring(firstColon+1, secondColon));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if ((period > 0) & (period < time_s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                second =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    Integer.parseInt(time_s.substring(secondColon+1, period));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                nanos_s = time_s.substring(period+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (nanos_s.length() > 9)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (!Character.isDigit(nanos_s.charAt(0)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                a_nanos = Integer.parseInt(nanos_s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else if (period > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                throw new java.lang.IllegalArgumentException(formatError);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                second = Integer.parseInt(time_s.substring(secondColon+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return new Timestamp(year, month, day, hour, minute, second, a_nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Formats a timestamp in JDBC timestamp escape format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *         <code>yyyy-mm-dd hh:mm:ss.fffffffff</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * where <code>ffffffffff</code> indicates nanoseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return a <code>String</code> object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *           <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public String toString () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int year = super.getYear() + 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int month = super.getMonth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int day = super.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int hour = super.getHours();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int minute = super.getMinutes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int second = super.getSeconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        String yearString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String monthString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        String dayString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        String hourString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        String minuteString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String secondString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        String nanosString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        String zeros = "000000000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String yearZeros = "0000";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        StringBuffer timestampBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (year < 1000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // Add leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            yearString = "" + year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            yearString = yearZeros.substring(0, (4-yearString.length())) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                yearString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            yearString = "" + year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (month < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            monthString = "0" + month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            monthString = Integer.toString(month);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (day < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            dayString = "0" + day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            dayString = Integer.toString(day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (hour < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            hourString = "0" + hour;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            hourString = Integer.toString(hour);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (minute < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            minuteString = "0" + minute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            minuteString = Integer.toString(minute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (second < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            secondString = "0" + second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            secondString = Integer.toString(second);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (nanos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            nanosString = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            nanosString = Integer.toString(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // Add leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            nanosString = zeros.substring(0, (9-nanosString.length())) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                nanosString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // Truncate trailing zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            char[] nanosChar = new char[nanosString.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            nanosString.getChars(0, nanosString.length(), nanosChar, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            int truncIndex = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            while (nanosChar[truncIndex] == '0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                truncIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            nanosString = new String(nanosChar, 0, truncIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // do a string buffer here instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        timestampBuf = new StringBuffer(20+nanosString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        timestampBuf.append(yearString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        timestampBuf.append("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        timestampBuf.append(monthString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        timestampBuf.append("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        timestampBuf.append(dayString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        timestampBuf.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        timestampBuf.append(hourString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        timestampBuf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        timestampBuf.append(minuteString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        timestampBuf.append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        timestampBuf.append(secondString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        timestampBuf.append(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        timestampBuf.append(nanosString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return (timestampBuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Gets this <code>Timestamp</code> object's <code>nanos</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return this <code>Timestamp</code> object's fractional seconds component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see #setNanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int getNanos() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return nanos;
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
     * Sets this <code>Timestamp</code> object's <code>nanos</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param n the new fractional seconds component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @exception java.lang.IllegalArgumentException if the given argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *            is greater than 999999999 or less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see #getNanos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public void setNanos(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (n > 999999999 || n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new IllegalArgumentException("nanos > 999999999 or < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        nanos = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Tests to see if this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * equal to the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return <code>true</code> if the given <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         object is equal to this <code>Timestamp</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public boolean equals(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (super.equals(ts)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if  (nanos == ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Tests to see if this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * equal to the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * This version of the method <code>equals</code> has been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * to fix the incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * signature of <code>Timestamp.equals(Timestamp)</code> and to preserve backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * compatibility with existing class files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Note: This method is not symmetric with respect to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <code>equals(Object)</code> method in the base class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param ts the <code>Object</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @return <code>true</code> if the given <code>Object</code> is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         of a <code>Timestamp</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         is equal to this <code>Timestamp</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public boolean equals(java.lang.Object ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
      if (ts instanceof Timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return this.equals((Timestamp)ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Indicates whether this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * earlier than the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return <code>true</code> if this <code>Timestamp</code> object is earlier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public boolean before(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return compareTo(ts) < 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Indicates whether this <code>Timestamp</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * later than the given <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param ts the <code>Timestamp</code> value to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return <code>true</code> if this <code>Timestamp</code> object is later;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    public boolean after(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return compareTo(ts) > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Compares this <code>Timestamp</code> object to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <code>Timestamp</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param   ts   the <code>Timestamp</code> object to be compared to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *                this <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return  the value <code>0</code> if the two <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *          objects are equal; a value less than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *          <code>Timestamp</code> object is before the given argument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *          and a value greater than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *          <code>Timestamp</code> object is after the given argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public int compareTo(Timestamp ts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int i = super.compareTo(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (nanos > ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            } else if (nanos < ts.nanos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Compares this <code>Timestamp</code> object to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * <code>Date</code>, which must be a <code>Timestamp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * object. If the argument is not a <code>Timestamp</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * this method throws a <code>ClassCastException</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * (<code>Timestamp</code> objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * comparable only to other <code>Timestamp</code> objects.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param o the <code>Date</code> to be compared, which must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *        <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @return  the value <code>0</code> if this <code>Timestamp</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *          and the given object are equal; a value less than <code>0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *          if this  <code>Timestamp</code> object is before the given argument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *          and a value greater than <code>0</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *          <code>Timestamp</code> object is after the given argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public int compareTo(java.util.Date o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
       if(o instanceof Timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // When Timestamp instance compare it with a Timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // Hence it is basically calling this.compareTo((Timestamp))o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            // Note typecasting is safe because o is instance of Timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
           return compareTo((Timestamp)o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // When Date doing a o.compareTo(this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            // will give wrong results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
          Timestamp ts = new Timestamp(o.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
          return this.compareTo(ts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    static final long serialVersionUID = 2745179027874758501L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}