Java Timestamp from() 方法

2025年3月25日 | 1 分钟阅读

Timestamp 类的 from() 方法从 Instant 对象获取 Timestamp 实例。

语法

参数

参数 'instant' 表示要转换的瞬时。

返回值

Timestamp 类的 from() 方法返回一个 Timestamp,它表示与 instant 相同的时刻。

抛出

Timestamp 类的 from() 方法会抛出

NullPointerException- 如果实例为 null。

IllegalArgumentException- 如果瞬时太大,无法表示为 Timestamp

示例 1

输出

from() method will return 2018-09-02 22:05:31.689

示例 2

输出

from() method will return 1970-01-01 05:41:18.0

示例 3

输出

Exception in thread "main" java.lang.NullPointerException
	at java.sql.Timestamp.from(Timestamp.java:598)
	at com.javaTpoint.JavaTimestampFromExample3.main(JavaTimestampFromExample3.java:8)