0N/A/*
3909N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.sql;
0N/A
0N/Aimport java.util.StringTokenizer;
0N/A
0N/A/**
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 *
0N/A * <p>The precision of a Timestamp object is calculated to be either:
0N/A * <ul>
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 *</ul>
0N/A *
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>
4022N/A * method. Also, the <code>hashCode</code> method uses the underlying
0N/A * <code>java.util.Date</code>
0N/A * implementation and therefore does not include nanos in its computation.
0N/A * <P>
0N/A * Due to the differences between the <code>Timestamp</code> class
0N/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 */
0N/Apublic class Timestamp extends java.util.Date {
0N/A
0N/A /**
0N/A * Constructs a <code>Timestamp</code> object initialized
0N/A * with the given values.
0N/A *
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 */
0N/A @Deprecated
0N/A public Timestamp(int year, int month, int date,
0N/A int hour, int minute, int second, int nano) {
0N/A super(year, month, date, hour, minute, second);
0N/A if (nano > 999999999 || nano < 0) {
0N/A throw new IllegalArgumentException("nanos > 999999999 or < 0");
0N/A }
0N/A nanos = nano;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>Timestamp</code> object
0N/A * using a milliseconds time value. The
0N/A * integral seconds are stored in the underlying date value; the
0N/A * fractional seconds are stored in the <code>nanos</code> field of
0N/A * the <code>Timestamp</code> object.
0N/A *
0N/A * @param time milliseconds since January 1, 1970, 00:00:00 GMT.
0N/A * A negative number is the number of milliseconds before
0N/A * January 1, 1970, 00:00:00 GMT.
0N/A * @see java.util.Calendar
0N/A */
0N/A public Timestamp(long time) {
0N/A super((time/1000)*1000);
0N/A nanos = (int)((time%1000) * 1000000);
0N/A if (nanos < 0) {
0N/A nanos = 1000000000 + nanos;
0N/A super.setTime(((time/1000)-1)*1000);
0N/A }
0N/A }
0N/A
0N/A /**
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 *
0N/A * @param time the number of milliseconds.
0N/A * @see #getTime
0N/A * @see #Timestamp(long time)
0N/A * @see java.util.Calendar
0N/A */
0N/A public void setTime(long time) {
0N/A super.setTime((time/1000)*1000);
0N/A nanos = (int)((time%1000) * 1000000);
0N/A if (nanos < 0) {
0N/A nanos = 1000000000 + nanos;
0N/A super.setTime(((time/1000)-1)*1000);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
0N/A * represented by this <code>Timestamp</code> object.
0N/A *
0N/A * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
0N/A * represented by this date.
0N/A * @see #setTime
0N/A */
0N/A public long getTime() {
0N/A long time = super.getTime();
0N/A return (time + (nanos / 1000000));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * @serial
0N/A */
0N/A private int nanos;
0N/A
0N/A /**
0N/A * Converts a <code>String</code> object in JDBC timestamp escape format to a
0N/A * <code>Timestamp</code> value.
0N/A *
2751N/A * @param s timestamp in format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code>. The
2751N/A * fractional seconds may be omitted. The leading zero for <code>mm</code>
2751N/A * and <code>dd</code> may also be omitted.
2751N/A *
0N/A * @return corresponding <code>Timestamp</code> value
0N/A * @exception java.lang.IllegalArgumentException if the given argument
2751N/A * does not have the format <code>yyyy-[m]m-[d]d hh:mm:ss[.f...]</code>
0N/A */
0N/A public static Timestamp valueOf(String s) {
2751N/A final int YEAR_LENGTH = 4;
2751N/A final int MONTH_LENGTH = 2;
2751N/A final int DAY_LENGTH = 2;
2751N/A final int MAX_MONTH = 12;
2751N/A final int MAX_DAY = 31;
0N/A String date_s;
0N/A String time_s;
0N/A String nanos_s;
2751N/A int year = 0;
2751N/A int month = 0;
2751N/A int day = 0;
0N/A int hour;
0N/A int minute;
0N/A int second;
0N/A int a_nanos = 0;
0N/A int firstDash;
0N/A int secondDash;
0N/A int dividingSpace;
0N/A int firstColon = 0;
0N/A int secondColon = 0;
0N/A int period = 0;
0N/A String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]";
0N/A String zeros = "000000000";
0N/A String delimiterDate = "-";
0N/A String delimiterTime = ":";
0N/A
0N/A if (s == null) throw new java.lang.IllegalArgumentException("null string");
0N/A
0N/A // Split the string into date and time components
0N/A s = s.trim();
0N/A dividingSpace = s.indexOf(' ');
0N/A if (dividingSpace > 0) {
0N/A date_s = s.substring(0,dividingSpace);
0N/A time_s = s.substring(dividingSpace+1);
0N/A } else {
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A }
0N/A
0N/A // Parse the date
0N/A firstDash = date_s.indexOf('-');
0N/A secondDash = date_s.indexOf('-', firstDash+1);
0N/A
0N/A // Parse the time
0N/A if (time_s == null)
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A firstColon = time_s.indexOf(':');
0N/A secondColon = time_s.indexOf(':', firstColon+1);
0N/A period = time_s.indexOf('.', secondColon+1);
0N/A
0N/A // Convert the date
2751N/A boolean parsedDate = false;
2751N/A if ((firstDash > 0) && (secondDash > 0) && (secondDash < date_s.length() - 1)) {
2751N/A String yyyy = date_s.substring(0, firstDash);
2751N/A String mm = date_s.substring(firstDash + 1, secondDash);
2751N/A String dd = date_s.substring(secondDash + 1);
2751N/A if (yyyy.length() == YEAR_LENGTH &&
2751N/A (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&
2751N/A (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {
2751N/A year = Integer.parseInt(yyyy);
2751N/A month = Integer.parseInt(mm);
2751N/A day = Integer.parseInt(dd);
2751N/A
2751N/A if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
2751N/A parsedDate = true;
2751N/A }
2751N/A }
2751N/A }
2751N/A if (! parsedDate) {
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A }
0N/A
0N/A // Convert the time; default missing nanos
0N/A if ((firstColon > 0) & (secondColon > 0) &
0N/A (secondColon < time_s.length()-1)) {
0N/A hour = Integer.parseInt(time_s.substring(0, firstColon));
0N/A minute =
0N/A Integer.parseInt(time_s.substring(firstColon+1, secondColon));
0N/A if ((period > 0) & (period < time_s.length()-1)) {
0N/A second =
0N/A Integer.parseInt(time_s.substring(secondColon+1, period));
0N/A nanos_s = time_s.substring(period+1);
0N/A if (nanos_s.length() > 9)
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A if (!Character.isDigit(nanos_s.charAt(0)))
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
0N/A a_nanos = Integer.parseInt(nanos_s);
0N/A } else if (period > 0) {
0N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A } else {
0N/A second = Integer.parseInt(time_s.substring(secondColon+1));
0N/A }
0N/A } else {
2751N/A throw new java.lang.IllegalArgumentException(formatError);
0N/A }
0N/A
2751N/A return new Timestamp(year - 1900, month - 1, day, hour, minute, second, a_nanos);
0N/A }
0N/A
0N/A /**
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 * <P>
0N/A * @return a <code>String</code> object in
0N/A * <code>yyyy-mm-dd hh:mm:ss.fffffffff</code> format
0N/A */
0N/A public String toString () {
0N/A
0N/A int year = super.getYear() + 1900;
0N/A int month = super.getMonth() + 1;
0N/A int day = super.getDate();
0N/A int hour = super.getHours();
0N/A int minute = super.getMinutes();
0N/A int second = super.getSeconds();
0N/A String yearString;
0N/A String monthString;
0N/A String dayString;
0N/A String hourString;
0N/A String minuteString;
0N/A String secondString;
0N/A String nanosString;
0N/A String zeros = "000000000";
0N/A String yearZeros = "0000";
0N/A StringBuffer timestampBuf;
0N/A
0N/A if (year < 1000) {
0N/A // Add leading zeros
0N/A yearString = "" + year;
0N/A yearString = yearZeros.substring(0, (4-yearString.length())) +
0N/A yearString;
0N/A } else {
0N/A yearString = "" + year;
0N/A }
0N/A if (month < 10) {
0N/A monthString = "0" + month;
0N/A } else {
0N/A monthString = Integer.toString(month);
0N/A }
0N/A if (day < 10) {
0N/A dayString = "0" + day;
0N/A } else {
0N/A dayString = Integer.toString(day);
0N/A }
0N/A if (hour < 10) {
0N/A hourString = "0" + hour;
0N/A } else {
0N/A hourString = Integer.toString(hour);
0N/A }
0N/A if (minute < 10) {
0N/A minuteString = "0" + minute;
0N/A } else {
0N/A minuteString = Integer.toString(minute);
0N/A }
0N/A if (second < 10) {
0N/A secondString = "0" + second;
0N/A } else {
0N/A secondString = Integer.toString(second);
0N/A }
0N/A if (nanos == 0) {
0N/A nanosString = "0";
0N/A } else {
0N/A nanosString = Integer.toString(nanos);
0N/A
0N/A // Add leading zeros
0N/A nanosString = zeros.substring(0, (9-nanosString.length())) +
0N/A nanosString;
0N/A
0N/A // Truncate trailing zeros
0N/A char[] nanosChar = new char[nanosString.length()];
0N/A nanosString.getChars(0, nanosString.length(), nanosChar, 0);
0N/A int truncIndex = 8;
0N/A while (nanosChar[truncIndex] == '0') {
0N/A truncIndex--;
0N/A }
0N/A
0N/A nanosString = new String(nanosChar, 0, truncIndex + 1);
0N/A }
0N/A
0N/A // do a string buffer here instead.
0N/A timestampBuf = new StringBuffer(20+nanosString.length());
0N/A timestampBuf.append(yearString);
0N/A timestampBuf.append("-");
0N/A timestampBuf.append(monthString);
0N/A timestampBuf.append("-");
0N/A timestampBuf.append(dayString);
0N/A timestampBuf.append(" ");
0N/A timestampBuf.append(hourString);
0N/A timestampBuf.append(":");
0N/A timestampBuf.append(minuteString);
0N/A timestampBuf.append(":");
0N/A timestampBuf.append(secondString);
0N/A timestampBuf.append(".");
0N/A timestampBuf.append(nanosString);
0N/A
0N/A return (timestampBuf.toString());
0N/A }
0N/A
0N/A /**
0N/A * Gets this <code>Timestamp</code> object's <code>nanos</code> value.
0N/A *
0N/A * @return this <code>Timestamp</code> object's fractional seconds component
0N/A * @see #setNanos
0N/A */
0N/A public int getNanos() {
0N/A return nanos;
0N/A }
0N/A
0N/A /**
0N/A * Sets this <code>Timestamp</code> object's <code>nanos</code> field
0N/A * to the given value.
0N/A *
0N/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 * @see #getNanos
0N/A */
0N/A public void setNanos(int n) {
0N/A if (n > 999999999 || n < 0) {
0N/A throw new IllegalArgumentException("nanos > 999999999 or < 0");
0N/A }
0N/A nanos = n;
0N/A }
0N/A
0N/A /**
0N/A * Tests to see if this <code>Timestamp</code> object is
0N/A * equal to the given <code>Timestamp</code> object.
0N/A *
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 */
0N/A public boolean equals(Timestamp ts) {
0N/A if (super.equals(ts)) {
0N/A if (nanos == ts.nanos) {
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tests to see if this <code>Timestamp</code> object is
0N/A * equal to the given object.
0N/A *
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
0N/A * compatibility with existing class files.
0N/A *
0N/A * Note: This method is not symmetric with respect to the
0N/A * <code>equals(Object)</code> method in the base class.
0N/A *
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
0N/A */
0N/A public boolean equals(java.lang.Object ts) {
0N/A if (ts instanceof Timestamp) {
0N/A return this.equals((Timestamp)ts);
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether this <code>Timestamp</code> object is
0N/A * earlier than the given <code>Timestamp</code> object.
0N/A *
0N/A * @param ts the <code>Timestamp</code> value to compare with
0N/A * @return <code>true</code> if this <code>Timestamp</code> object is earlier;
0N/A * <code>false</code> otherwise
0N/A */
0N/A public boolean before(Timestamp ts) {
0N/A return compareTo(ts) < 0;
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether this <code>Timestamp</code> object is
0N/A * later than the given <code>Timestamp</code> object.
0N/A *
0N/A * @param ts the <code>Timestamp</code> value to compare with
0N/A * @return <code>true</code> if this <code>Timestamp</code> object is later;
0N/A * <code>false</code> otherwise
0N/A */
0N/A public boolean after(Timestamp ts) {
0N/A return compareTo(ts) > 0;
0N/A }
0N/A
0N/A /**
0N/A * Compares this <code>Timestamp</code> object to the given
0N/A * <code>Timestamp</code> object.
0N/A *
0N/A * @param ts the <code>Timestamp</code> object to be compared to
0N/A * this <code>Timestamp</code> object
0N/A * @return the value <code>0</code> if the two <code>Timestamp</code>
0N/A * objects are equal; a value less than <code>0</code> if this
0N/A * <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 * @since 1.4
0N/A */
0N/A public int compareTo(Timestamp ts) {
3385N/A long thisTime = this.getTime();
3385N/A long anotherTime = ts.getTime();
3385N/A int i = (thisTime<anotherTime ? -1 :(thisTime==anotherTime?0 :1));
0N/A if (i == 0) {
0N/A if (nanos > ts.nanos) {
0N/A return 1;
0N/A } else if (nanos < ts.nanos) {
0N/A return -1;
0N/A }
0N/A }
0N/A return i;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Compares this <code>Timestamp</code> object to the given
2751N/A * <code>Date</code> object.
0N/A *
2751N/A * @param o the <code>Date</code> to be compared to
2751N/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 *
0N/A * @since 1.5
0N/A */
0N/A public int compareTo(java.util.Date o) {
0N/A if(o instanceof Timestamp) {
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 return compareTo((Timestamp)o);
0N/A } else {
0N/A // When Date doing a o.compareTo(this)
0N/A // will give wrong results.
0N/A Timestamp ts = new Timestamp(o.getTime());
0N/A return this.compareTo(ts);
0N/A }
0N/A }
0N/A
4022N/A /**
4022N/A * {@inheritDoc}
4022N/A *
4022N/A * The {@code hashCode} method uses the underlying {@code java.util.Date}
4022N/A * implementation and therefore does not include nanos in its computation.
4022N/A *
4022N/A */
4022N/A @Override
4022N/A public int hashCode() {
4022N/A return super.hashCode();
4022N/A }
4022N/A
0N/A static final long serialVersionUID = 2745179027874758501L;
0N/A
0N/A}