Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Timestamp

public class Timestamp
extends Date

java.lang.Object
   ↳ java.util.Date
     ↳ java.sql.Timestamp


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:

Summary

Public constructors

Timestamp(int theYear, int theMonth, int theDate, int theHour, int theMinute, int theSecond, int theNano)

This constructor was deprecated in API level 1. Use the constructor Timestamp(long) instead.

Timestamp(long theTime)

Returns a Timestamp object corresponding to the time represented by a supplied time value.

Public methods

boolean after(Timestamp theTimestamp)

Returns true if this timestamp object is later than the supplied timestamp, otherwise returns false.

boolean before(Timestamp theTimestamp)

Returns true if this Timestamp object is earlier than the supplied timestamp, otherwise returns false.

int compareTo(Date theObject)

Compares this Timestamp object with a supplied Timestamp object.

int compareTo(Timestamp theTimestamp)

Compares this Timestamp object with a supplied Timestamp object.

boolean equals(Timestamp theTimestamp)

Tests to see if this timestamp is equal to a supplied timestamp.

boolean equals(Object theObject)

Tests to see if this timestamp is equal to a supplied object.

int getNanos()

Gets this Timestamp's nanosecond value

long getTime()

Returns the time represented by this Timestamp object, as a long value containing the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).

void setNanos(int n)

Sets the nanosecond value for this Timestamp.

void setTime(long theTime)

Sets the time represented by this Timestamp object to the supplied time, defined as the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).

String toString()

Returns the timestamp formatted as a String in the JDBC Timestamp Escape format, which is "yyyy-MM-dd HH:mm:ss.nnnnnnnnn".

static Timestamp valueOf(String s)

Creates a Timestamp object 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"

Inherited methods

From class java.util.Date
From class java.lang.Object
From interface java.lang.Comparable

Public constructors

Timestamp

Added in API level 1
Timestamp (int theYear, 
                int theMonth, 
                int theDate, 
                int theHour, 
                int theMinute, 
                int theSecond, 
                int theNano)

This constructor was deprecated in API level 1.
Use the constructor Timestamp(long) instead.

Returns a Timestamp corresponding to the time specified by the supplied values for Year, Month, Date, Hour, Minutes, Seconds and Nanoseconds.

Parameters
theYear int: specified as the year minus 1900.
theMonth int: specified as an integer in the range [0,11].
theDate int: specified as an integer in the range [1,31].
theHour int: specified as an integer in the range [0,23].
theMinute int: specified as an integer in the range [0,59].
theSecond int: specified as an integer in the range [0,59].
theNano int: 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

Added in API level 1
Timestamp (long theTime)

Returns a Timestamp object corresponding to the time represented by a supplied time value.

Parameters
theTime long: a time value in the format of milliseconds since the Epoch (January 1 1970 00:00:00.000 GMT).

Public methods

after

Added in API level 1
boolean after (Timestamp theTimestamp)

Returns true if this timestamp object is later than the supplied timestamp, otherwise returns false.

Parameters
theTimestamp Timestamp: the timestamp to compare with this timestamp object.
Returns
boolean true if this Timestamp object is later than the supplied timestamp, false otherwise.

before

Added in API level 1
boolean before (Timestamp theTimestamp)

Returns true if this Timestamp object is earlier than the supplied timestamp, otherwise returns false.

Parameters
theTimestamp Timestamp: the timestamp to compare with this Timestamp object.
Returns
boolean true if this Timestamp object is earlier than the supplied timestamp, false otherwise.

compareTo

Added in API level 1
int compareTo (Date theObject)

Compares this Timestamp object with a supplied Timestamp object.

Parameters
theObject Date: the timestamp to compare with this Timestamp object, passed as an Object.
Returns
int
0 if the two Timestamp objects are equal in time
a value < 0 if this Timestamp object is before the supplied Timestamp and a value
> 0 if this Timestamp object is after the supplied Timestamp
Throws
ClassCastException if the supplied object is not a Timestamp object.

compareTo

Added in API level 1
int compareTo (Timestamp theTimestamp)

Compares this Timestamp object with a supplied Timestamp object.

Parameters
theTimestamp Timestamp: the timestamp to compare with this Timestamp object, passed in as a Timestamp.
Returns
int one of the following:
  • 0, if the two Timestamp objects are equal in time
  • < 0, if this Timestamp object is before the supplied Timestamp
  • > 0, if this Timestamp object is after the supplied Timestamp

equals

Added in API level 1
boolean equals (Timestamp theTimestamp)

Tests to see if this timestamp is equal to a supplied timestamp.

Parameters
theTimestamp Timestamp: the timestamp to compare with this Timestamp object, passed as an Object.
Returns
boolean true if this Timestamp object is equal to the supplied Timestamp object, false otherwise.

equals

Added in API level 1
boolean equals (Object theObject)

Tests to see if this timestamp is equal to a supplied object.

Parameters
theObject Object: the object to which this timestamp is compared.
Returns
boolean true if this Timestamp object is equal to the supplied Timestamp object
false if the object is not a Timestamp object or if the object is a Timestamp but represents a different instant in time.

getNanos

Added in API level 1
int getNanos ()

Gets this Timestamp's nanosecond value

Returns
int The timestamp's nanosecond value, an integer between 0 and 999,999,999.

getTime

Added in API level 1
long getTime ()

Returns the time represented by this Timestamp object, as a long value containing the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).

Returns
long the number of milliseconds that have passed since January 1 1970, 00:00:00.000 GMT.

setNanos

Added in API level 1
void setNanos (int n)

Sets the nanosecond value for this Timestamp.

Parameters
n int: number of nanoseconds.
Throws
IllegalArgumentException if number of nanoseconds smaller than 0 or greater than 999,999,999.

setTime

Added in API level 1
void setTime (long theTime)

Sets the time represented by this Timestamp object to the supplied time, defined as the number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).

Parameters
theTime long: number of milliseconds since the Epoch (January 1 1970, 00:00:00.000 GMT).

toString

Added in API level 1
String toString ()

Returns the timestamp formatted as a String in the JDBC Timestamp Escape format, which is "yyyy-MM-dd HH:mm:ss.nnnnnnnnn".

Returns
String A string representing the instant defined by the Timestamp, in JDBC Timestamp escape format.

valueOf

Added in API level 1
Timestamp valueOf (String s)

Creates a Timestamp object 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 String: the String containing a time in JDBC timestamp escape format.
Returns
Timestamp A Timestamp object with time value as defined by the supplied String.
Throws
IllegalArgumentException if the provided string is null.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.