Package java.sql
Class Timestamp
java.lang.Object
java.util.Date
java.sql.Timestamp
- All Implemented Interfaces:
Serializable,Cloneable,Comparable<Date>
public class Timestamp extends Date
A Java representation of the SQL
TIMESTAMP type. It provides the
capability of representing the SQL TIMESTAMP nanosecond value, in
addition to the regular date/time value which has millisecond resolution.
The Timestamp class consists of a regular date/time value, where only
the integral seconds value is stored, plus a nanoseconds value where the
fractional seconds are stored.
The addition of the nanosecond value field to the Timestamp object
makes it significantly different from the java.util.Date object which
it extends. Users should be aware that Timestamp objects are not
interchangable with java.util.Date objects when used outside the
confines of the java.sql package.
- See Also:
Date,Time,Date, Serialized Form
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description booleanafter(Timestamp theTimestamp)Returnstrueif this timestamp object is later than the supplied timestamp, otherwise returnsfalse.booleanbefore(Timestamp theTimestamp)Returnstrueif thisTimestampobject is earlier than the supplied timestamp, otherwise returnsfalse.intcompareTo(Timestamp theTimestamp)Compares thisTimestampobject with a suppliedTimestampobject.intcompareTo(Date theObject)Compares thisTimestampobject with a suppliedTimestampobject.booleanequals(Object theObject)Tests to see if this timestamp is equal to a supplied object.booleanequals(Timestamp theTimestamp)Tests to see if this timestamp is equal to a supplied timestamp.intgetNanos()Gets thisTimestamp's nanosecond valuelonggetTime()Returns the time represented by thisTimestampobject, as a long value containing the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).voidsetNanos(int n)Sets the nanosecond value for thisTimestamp.voidsetTime(long theTime)Sets the time represented by thisTimestampobject to the supplied time, defined as the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).StringtoString()Returns the timestamp formatted as a String in the JDBC Timestamp Escape format, which is"yyyy-MM-dd HH:mm:ss.nnnnnnnnn".static TimestampvalueOf(String s)Creates aTimestampobject with a time value equal to the time specified by a supplied String holding the time in JDBC timestamp escape format, which is"yyyy-MM-dd HH:mm:ss.nnnnnnnnn"Methods inherited from class java.util.Date
after, before, clone, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setYear, toGMTString, toLocaleString, UTC
-
Constructor Details
-
Timestamp
@Deprecated public Timestamp(int theYear, int theMonth, int theDate, int theHour, int theMinute, int theSecond, int theNano) throws IllegalArgumentExceptionDeprecated.Use the constructorTimestamp(long)instead.Returns aTimestampcorresponding to the time specified by the supplied values for Year, Month, Date, Hour, Minutes, Seconds and Nanoseconds.- Parameters:
theYear- specified as the year minus 1900.theMonth- specified as an integer in the range [0,11].theDate- specified as an integer in the range [1,31].theHour- specified as an integer in the range [0,23].theMinute- specified as an integer in the range [0,59].theSecond- specified as an integer in the range [0,59].theNano- which defines the nanosecond value of the timestamp specified as an integer in the range [0,999'999'999]- Throws:
IllegalArgumentException- if any of the parameters is out of range.
-
Timestamp
public Timestamp(long theTime)Returns aTimestampobject corresponding to the time represented by a supplied time value.- Parameters:
theTime- a time value in the format of milliseconds since the Epoch (January 1 1970 00:00:00.000 GMT).
-
-
Method Details
-
after
Returnstrueif this timestamp object is later than the supplied timestamp, otherwise returnsfalse.- Parameters:
theTimestamp- the timestamp to compare with this timestamp object.- Returns:
trueif thisTimestampobject is later than the supplied timestamp,falseotherwise.
-
before
Returnstrueif thisTimestampobject is earlier than the supplied timestamp, otherwise returnsfalse.- Parameters:
theTimestamp- the timestamp to compare with thisTimestampobject.- Returns:
trueif thisTimestampobject is earlier than the supplied timestamp,falseotherwise.
-
compareTo
Compares thisTimestampobject with a suppliedTimestampobject.- Specified by:
compareToin interfaceComparable<Date>- Overrides:
compareToin classDate- Parameters:
theObject- the timestamp to compare with thisTimestampobject, passed as anObject.- Returns:
0if the twoTimestampobjects are equal in time-
a value
< 0if thisTimestampobject is before the suppliedTimestampand a value> 0if thisTimestampobject is after the suppliedTimestamp- Throws:
ClassCastException- if the supplied object is not aTimestampobject.
-
compareTo
Compares thisTimestampobject with a suppliedTimestampobject.- Parameters:
theTimestamp- the timestamp to compare with thisTimestampobject, passed in as aTimestamp.- Returns:
- one of the following:
0, if the twoTimestampobjects are equal in time< 0, if thisTimestampobject is before the suppliedTimestamp-
> 0, if thisTimestampobject is after the suppliedTimestamp
-
equals
Tests to see if this timestamp is equal to a supplied object.- Overrides:
equalsin classDate- Parameters:
theObject- the object to which this timestamp is compared.- Returns:
trueif thisTimestampobject is equal to the suppliedTimestampobjectfalseif the object is not aTimestampobject or if the object is aTimestampbut represents a different instant in time.- See Also:
Date.hashCode()
-
equals
Tests to see if this timestamp is equal to a supplied timestamp.- Parameters:
theTimestamp- the timestamp to compare with thisTimestampobject, passed as anObject.- Returns:
trueif thisTimestampobject is equal to the suppliedTimestampobject,falseotherwise.
-
getNanos
public int getNanos()Gets thisTimestamp's nanosecond value- Returns:
- The timestamp's nanosecond value, an integer between 0 and 999,999,999.
-
getTime
public long getTime()Returns the time represented by thisTimestampobject, as a long value containing the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT). -
setNanos
Sets the nanosecond value for thisTimestamp.- Parameters:
n- number of nanoseconds.- Throws:
IllegalArgumentException- if number of nanoseconds smaller than 0 or greater than 999,999,999.
-
setTime
public void setTime(long theTime)Sets the time represented by thisTimestampobject to the supplied time, defined as the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT). -
toString
Returns the timestamp formatted as a String in the JDBC Timestamp Escape format, which is"yyyy-MM-dd HH:mm:ss.nnnnnnnnn". -
valueOf
Creates aTimestampobject with a time value equal to the time specified by a supplied String holding the time in JDBC timestamp escape format, which is"yyyy-MM-dd HH:mm:ss.nnnnnnnnn"- Parameters:
s- theStringcontaining a time in JDBC timestamp escape format.- Returns:
- A
Timestampobject with time value as defined by the suppliedString. - Throws:
IllegalArgumentException- if the provided string isnull.
-
Timestamp(long)instead.