public class Timestamp
extends Date
| java.lang.Object | ||
| java.util.Date | ||
| java.sql.Timestamp | ||
围绕java.util.Date一个薄包装器,它允许JDBC API将其标识为SQL TIMESTAMP值。 它通过允许将小数秒的指定精确到几纳秒,增加了保留SQL TIMESTAMP小数秒值的功能。 Timestamp还提供格式化和解析操作以支持时间戳值的JDBC转义语法。
Timestamp对象的精度计算为:
19 , which is the number of characters in yyyy-mm-dd hh:mm:ss 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision. 注意:这种类型是java.util.Date和单独纳秒值的组合。 只有积分秒存储在java.util.Date组件中。 小数秒 - 纳米 - 是分开的。 当传递一个不是java.sql.Timestamp实例的对象时, Timestamp.equals(Object)方法永远不会返回true ,因为日期的java.sql.Timestamp组件是未知的。 因此, Timestamp.equals(Object)方法相对于java.util.Date.equals(Object)方法java.util.Date.equals(Object) 。 另外, hashCode方法使用底层java.util.Date实现,因此在其计算中不包含java.util.Date 。
由于Timestamp类和上述java.util.Date类之间的差异,建议代码不将Timestamp值视为java.util.Date的实例。 Timestamp和java.util.Date之间的继承关系实际上表示实现继承,而不是类型继承。
Public constructors |
|
|---|---|
Timestamp(int year, int month, int date, int hour, int minute, int second, int nano) 此构造函数在API级别1中已弃用。请改用构造函数 |
|
Timestamp(long time) 使用毫秒时间值构造一个 |
|
公共方法(Public methods) |
|
|---|---|
boolean |
after(Timestamp ts) 指示此 |
boolean |
before(Timestamp ts) 指示此 |
int |
compareTo(Date o) 将此 |
int |
compareTo(Timestamp ts) 将此 |
boolean |
equals(Timestamp ts) 测试以查看此 |
boolean |
equals(Object ts) 测试以查看此 |
int |
getNanos() 获取此 |
long |
getTime() 返回此 |
int |
hashCode() 返回此对象的哈希码值。 |
void |
setNanos(int n) 将此 |
void |
setTime(long time) 将此 |
String |
toString() 使用JDBC时间戳转义格式格式化时间戳。 |
static Timestamp |
valueOf(String s) 将JDBC时间戳转义格式中的 |
继承方法(Inherited methods) |
|
|---|---|
java.util.Date
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
Timestamp (int year,
int month,
int date,
int hour,
int minute,
int second,
int nano)
此构造函数在API级别1中已弃用。
改为使用构造函数Timestamp(long millis)
构造一个用给定值初始化的 Timestamp对象。
| 参数(Parameters) | |
|---|---|
year |
int: the year minus 1900 |
month |
int: 0 to 11 |
date |
int: 1 to 31 |
hour |
int: 0 to 23 |
minute |
int: 0 to 59 |
second |
int: 0 to 59 |
nano |
int: 0 to 999,999,999 |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the nano argument is out of bounds |
Timestamp (long time)
使用毫秒时间值构造一个Timestamp对象。 积分秒存储在基础日期值中; 小数秒存储在Timestamp对象的nanos字段中。
| 参数(Parameters) | |
|---|---|
time |
long: milliseconds since January 1, 1970, 00:00:00 GMT. A negative number is the number of milliseconds before January 1, 1970, 00:00:00 GMT. |
也可以看看:
boolean after (Timestamp ts)
指示此 Timestamp对象是否晚于给定的 Timestamp对象。
| 参数(Parameters) | |
|---|---|
ts |
Timestamp: the Timestamp value to compare with |
| 返回(Returns) | |
|---|---|
boolean |
true if this Timestamp object is later; false otherwise |
boolean before (Timestamp ts)
指示此 Timestamp对象是否早于给定的 Timestamp对象。
| 参数(Parameters) | |
|---|---|
ts |
Timestamp: the Timestamp value to compare with |
| 返回(Returns) | |
|---|---|
boolean |
true if this Timestamp object is earlier; false otherwise |
int compareTo (Date o)
将此 Timestamp对象与给定的 Date对象进行比较。
| 参数(Parameters) | |
|---|---|
o |
Date: the Date to be compared to this Timestamp object |
| 返回(Returns) | |
|---|---|
int |
the value 0 if this Timestamp object and the given object are equal; a value less than 0 if this Timestamp object is before the given argument; and a value greater than 0 if this Timestamp object is after the given argument. |
int compareTo (Timestamp ts)
将此 Timestamp对象与给定的 Timestamp对象进行比较。
| 参数(Parameters) | |
|---|---|
ts |
Timestamp: the Timestamp object to be compared to this Timestamp object |
| 返回(Returns) | |
|---|---|
int |
the value 0 if the two Timestamp objects are equal; a value less than 0 if this Timestamp object is before the given argument; and a value greater than 0 if this Timestamp object is after the given argument. |
boolean equals (Timestamp ts)
测试以查看此 Timestamp对象是否与给定的 Timestamp对象相同。
| 参数(Parameters) | |
|---|---|
ts |
Timestamp: the Timestamp value to compare with |
| 返回(Returns) | |
|---|---|
boolean |
true if the given Timestamp object is equal to this Timestamp object; false otherwise |
boolean equals (Object ts)
测试这个Timestamp对象是否等于给定的对象。 已添加此版本的方法equals以修复Timestamp.equals(Timestamp)的不正确签名,并保留与现有类文件的向后兼容性。 注意:此方法与基类中的equals(Object)方法不对称。
| 参数(Parameters) | |
|---|---|
ts |
Object: the Object value to compare with |
| 返回(Returns) | |
|---|---|
boolean |
true if the given Object is an instance of a Timestamp that is equal to this Timestamp object; false otherwise |
int getNanos ()
获取此 Timestamp对象 nanos值。
| 返回(Returns) | |
|---|---|
int |
this Timestamp object's fractional seconds component |
也可以看看:
long getTime ()
返回此 Timestamp对象表示的自1970年1月1日00:00:00 GMT以来的毫秒数。
| 返回(Returns) | |
|---|---|
long |
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date. |
也可以看看:
int hashCode ()
返回此对象的哈希码值。 结果是由getTime()方法返回的原语long值的两半的异或。 也就是说,哈希码是表达式的值:
The(int)(this.getTime()^(this.getTime() >>> 32))
hashCode method uses the underlying
java.util.Date implementation and therefore does not include nanos in its computation.
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
void setNanos (int n)
将此 Timestamp对象的 nanos字段设置为给定值。
| 参数(Parameters) | |
|---|---|
n |
int: the new fractional seconds component |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the given argument is greater than 999999999 or less than 0 |
也可以看看:
void setTime (long time)
将此 Timestamp对象设置为表示1970年1月1日00:00:00 GMT之后 time毫秒的时间点。
| 参数(Parameters) | |
|---|---|
time |
long: the number of milliseconds. |
也可以看看:
String toString ()
使用JDBC时间戳转义格式格式化时间戳。 yyyy-mm-dd hh:mm:ss.fffffffff ,其中ffffffffff表示纳秒。
| 返回(Returns) | |
|---|---|
String |
a String object in yyyy-mm-dd hh:mm:ss.fffffffff format |
Timestamp valueOf (String s)
将JDBC时间戳转义格式中的 String对象转换为 Timestamp值。
| 参数(Parameters) | |
|---|---|
s |
String: timestamp in format yyyy-[m]m-[d]d hh:mm:ss[.f...]. The fractional seconds may be omitted. The leading zero for mm and dd may also be omitted. |
| 返回(Returns) | |
|---|---|
Timestamp |
corresponding Timestamp value |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the given argument does not have the format yyyy-[m]m-[d]d hh:mm:ss[.f...] |