Timestamp.java revision 3385
3258N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * <P>A thin wrapper around <code>java.util.Date</code> that allows 0N/A * the JDBC API to identify this as an SQL <code>TIMESTAMP</code> value. 0N/A * It adds the ability 0N/A * to hold the SQL <code>TIMESTAMP</code> fractional seconds value, by allowing 0N/A * the specification of fractional seconds to a precision of nanoseconds. 0N/A * A Timestamp also provides formatting and 0N/A * parsing operations to support the JDBC escape syntax for timestamp values. 0N/A * <p>The precision of a Timestamp object is calculated to be either: 0N/A * <li><code>19 </code>, which is the number of characters in yyyy-mm-dd hh:mm:ss 0N/A * <li> <code> 20 + s </code>, which is the number 0N/A * of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and <code>s</code> represents the scale of the given Timestamp, 0N/A * its fractional seconds precision. 0N/A * <P><B>Note:</B> This type is a composite of a <code>java.util.Date</code> and a 0N/A * separate nanoseconds value. Only integral seconds are stored in the 0N/A * <code>java.util.Date</code> component. The fractional seconds - the nanos - are 0N/A * separate. The <code>Timestamp.equals(Object)</code> method never returns 0N/A * <code>true</code> when passed an object 0N/A * that isn't an instance of <code>java.sql.Timestamp</code>, 0N/A * because the nanos component of a date is unknown. 0N/A * As a result, the <code>Timestamp.equals(Object)</code> 0N/A * method is not symmetric with respect to the 0N/A * <code>java.util.Date.equals(Object)</code> 3514N/A * method. Also, the <code>hashcode</code> method uses the underlying 0N/A * <code>java.util.Date</code> 3514N/A * implementation and therefore does not include nanos in its computation. 10N/A * Due to the differences between the <code>Timestamp</code> class 3514N/A * and the <code>java.util.Date</code> 0N/A * class mentioned above, it is recommended that code not view 0N/A * <code>Timestamp</code> values generically as an instance of 0N/A * <code>java.util.Date</code>. The 0N/A * inheritance relationship between <code>Timestamp</code> 0N/A * and <code>java.util.Date</code> really 0N/A * denotes implementation inheritance, and not type inheritance. 0N/A * Constructs a <code>Timestamp</code> object initialized 0N/A * with the given values. 0N/A * @param year the year minus 1900 0N/A * @param month 0 to 11 0N/A * @param date 1 to 31 0N/A * @param hour 0 to 23 0N/A * @param minute 0 to 59 0N/A * @param second 0 to 59 0N/A * @param nano 0 to 999,999,999 0N/A * @deprecated instead use the constructor <code>Timestamp(long millis)</code> 0N/A * @exception IllegalArgumentException if the nano argument is out of bounds 0N/A * Constructs a <code>Timestamp</code> object 1220N/A * using a milliseconds time value. The 1220N/A * integral seconds are stored in the underlying date value; the 1220N/A * fractional seconds are stored in the <code>nanos</code> field of 1220N/A * the <code>Timestamp</code> object. 1220N/A * @param time milliseconds since January 1, 1970, 00:00:00 GMT. 1220N/A * A negative number is the number of milliseconds before 1220N/A * January 1, 1970, 00:00:00 GMT. 0N/A * Sets this <code>Timestamp</code> object to represent a point in time that is 0N/A * <tt>time</tt> milliseconds after January 1, 1970 00:00:00 GMT. 0N/A * @param time the number of milliseconds. 0N/A * @see #Timestamp(long time) 0N/A * @see java.util.Calendar 0N/A * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT 914N/A * represented by this <code>Timestamp</code> object. 0N/A * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT 2664N/A * represented by this date. 0N/A * Converts a <code>String</code> object in JDBC timestamp escape format to a 0N/A * <code>Timestamp</code> value. 0N/A * @param s timestamp in format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code>. The 0N/A * fractional seconds may be omitted. The leading zero for <code>mm</code> 0N/A * and <code>dd</code> may also be omitted. 914N/A * @return corresponding <code>Timestamp</code> value 914N/A * @exception java.lang.IllegalArgumentException if the given argument 914N/A * does not have the format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code> 0N/A // Split the string into date and time components 0N/A // Convert the time; default missing nanos 0N/A * Formats a timestamp in JDBC timestamp escape format. 0N/A * <code>yyyy-mm-dd hh:mm:ss.fffffffff</code>, 0N/A * where <code>ffffffffff</code> indicates nanoseconds. 0N/A * @return a <code>String</code> object in 0N/A * <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> format 0N/A // Add leading zeros 0N/A // Truncate trailing zeros 0N/A // do a string buffer here instead. 2837N/A * Gets this <code>Timestamp</code> object's <code>nanos</code> value. 2837N/A * @return this <code>Timestamp</code> object's fractional seconds component 2837N/A * Sets this <code>Timestamp</code> object's <code>nanos</code> field 3258N/A * @param n the new fractional seconds component 0N/A * @exception java.lang.IllegalArgumentException if the given argument 0N/A * is greater than 999999999 or less than 0 0N/A if (n >
999999999 || n <
0) {
0N/A * Tests to see if this <code>Timestamp</code> object is 0N/A * equal to the given <code>Timestamp</code> object. 0N/A * @param ts the <code>Timestamp</code> value to compare with 0N/A * @return <code>true</code> if the given <code>Timestamp</code> 0N/A * object is equal to this <code>Timestamp</code> object; 0N/A * <code>false</code> otherwise 0N/A * Tests to see if this <code>Timestamp</code> object is 0N/A * equal to the given object. 0N/A * This version of the method <code>equals</code> has been added 0N/A * to fix the incorrect 0N/A * signature of <code>Timestamp.equals(Timestamp)</code> and to preserve backward 1220N/A * compatibility with existing class files. 1220N/A * Note: This method is not symmetric with respect to the 1220N/A * <code>equals(Object)</code> method in the base class. 0N/A * @param ts the <code>Object</code> value to compare with 0N/A * @return <code>true</code> if the given <code>Object</code> is an instance 0N/A * of a <code>Timestamp</code> that 0N/A * is equal to this <code>Timestamp</code> object; 0N/A * <code>false</code> otherwise 533N/A * Indicates whether this <code>Timestamp</code> object is 533N/A * earlier than the given <code>Timestamp</code> object. 533N/A * @param ts the <code>Timestamp</code> value to compare with 533N/A * @return <code>true</code> if this <code>Timestamp</code> object is earlier; 533N/A * <code>false</code> otherwise 533N/A * Indicates whether this <code>Timestamp</code> object is 533N/A * later than the given <code>Timestamp</code> object. 533N/A * @param ts the <code>Timestamp</code> value to compare with 533N/A * @return <code>true</code> if this <code>Timestamp</code> object is later; 533N/A * <code>false</code> otherwise 533N/A * Compares this <code>Timestamp</code> object to the given 533N/A * <code>Timestamp</code> object. 533N/A * @param ts the <code>Timestamp</code> object to be compared to 533N/A * this <code>Timestamp</code> object 533N/A * @return the value <code>0</code> if the two <code>Timestamp</code> 533N/A * objects are equal; a value less than <code>0</code> if this 533N/A * <code>Timestamp</code> object is before the given argument; 533N/A * and a value greater than <code>0</code> if this 533N/A * <code>Timestamp</code> object is after the given argument. 533N/A * Compares this <code>Timestamp</code> object to the given 533N/A * <code>Date</code> object. 0N/A * @param o the <code>Date</code> to be compared to 0N/A * this <code>Timestamp</code> object 0N/A * @return the value <code>0</code> if this <code>Timestamp</code> object 0N/A * and the given object are equal; a value less than <code>0</code> 0N/A * if this <code>Timestamp</code> object is before the given argument; 0N/A * and a value greater than <code>0</code> if this 0N/A * <code>Timestamp</code> object is after the given argument. 0N/A // When Timestamp instance compare it with a Timestamp 0N/A // Hence it is basically calling this.compareTo((Timestamp))o); 0N/A // Note typecasting is safe because o is instance of Timestamp 0N/A // When Date doing a o.compareTo(this) 0N/A // will give wrong results.