时间戳
时间戳
在java.sql.Timestamp
来表示时间戳对象,可以通过以下方式创建与获取对象:
// 利用系统标准时间创建
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// 从 Date 对象中创建
new Timestamp((new Date()).getTime());
// 获取自 1970-01-01 00:00:00 GMT 以来的毫秒数
timestamp.getTime();
在java.time.Instant
来表示自从
// 基于静态函数创建
Instant instant = Instant.now();
// 基于 Date 或者毫秒数转换
Instant someInstant = someDate.toInstant();
Instant someInstant = Instant.ofEpochMilli(someDate.getTime());
// 基于 TimeStamp 转换
Instant instant = timestamp.toInstant();
// 从 LocalDate 转化而来
LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC)
// 从 LocalDateTime 转化而来
ldt.atZone(ZoneId.systemDefault()).toInstant();
// 获取毫秒
long timeStampMillis = instant.toEpochMilli();
// 获取秒
long timeStampSeconds = instant.getEpochSecond();
System.currentTimeMillis()
的替代。这种指向时间轴的对象即是Instant
类。java.util.Date
对象。
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
Instant instant = clock.instant();
Date legacyDate = Date.from(instant); // legacy java.util.Date