时间戳 Timestamp
创建
你可以使用 Timestamp.now() 获取当前的 Unix 时间戳值, 或使用 Timestamp.ofMilliseconds(...) 基于毫秒值获取一个结果。
val now = Timestamp.now()
val time = Timestamp.ofMilliseconds(123456L)
var now = Timestamp.now();
var time = Timestamp.ofMilliseconds(123456L);
使用
Timestamp 通常用于获取各种单位下的时间戳数值结果。 它是对“时间戳值”的统一包装,不负责日期格式化、时区转换或字符串解析。
val milliseconds = timestamp.milliseconds
val seconds = timestamp.timeAs(TimeUnit.SECONDS)
long milliseconds = timestamp.getMilliseconds();
long seconds = timestamp.timeAs(TimeUnit.SECONDS);
与 Kotlin 标准库时间类型转换
Timestamp.now() 通过 kotlin.time.Clock.System 获取当前时间, 也可以向 Timestamp.now(clock) 提供指定的 Clock。 这两种方式都返回基于 kotlin.time.Instant 的 StandardInstantTimestamp。 Timestamp.ofMilliseconds(...) 仍返回 MillisecondTimestamp。
通过 Instant.toTimestamp() 将标准库的时间点包装为 Timestamp, 通过 Timestamp.toInstant() 转回标准库时间点:
import love.forte.simbot.common.time.Timestamp
import love.forte.simbot.common.time.Timestamp.Companion.toTimestamp
import kotlin.time.Clock
val timestamp = Clock.System.now().toTimestamp()
val instant = timestamp.toInstant()
val now = Timestamp.now(Clock.System)
Java 中对应使用 Timestamp.of(instant) 和 timestamp.toInstant()。 StandardInstantTimestamp.toInstant() 直接返回其持有的 Instant; 其他实现默认根据 milliseconds 转换。
29 October 2026