我有一个 LocalDateTime 的实例,它是从存储库层获取的,我需要将它转换为一个 Timestamp(协议缓冲区)实例。
我已经习惯了以下转换方法:
LocalDateTime localDateTime = LocalDateTime.now();//this can be any date
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
Timestamp timestamp = Timestamp.newBuilder()
.setSeconds(instant.getEpochSecond())
.setNanos(instant.getNano())
.build();
这里使用 ZoneOffset 将 localDateTime 转换为 Instant 的实例,对吗?
我使用了 UTC 偏移量,因为 Timestamp 类中“seconds”属性的注释如下:
表示自 Unix 纪元 1970-01-01T00:00:00Z 以来的 UTC 时间秒数。 必须从 0001-01-01T00:00:00Z 到 9999-12-31T23:59:59Z 包含
我是否使用了正确的 ZoneOffset 以及我的转换方法是否正确?
总的来说,不,你的做法是不正确的。原因是
LocalDateTime
没有关联的时区,所以它本质上是模棱两可的。要将其转换为实际时间戳(绝对时间点,独立于时区),您需要知道它是在哪个时区测量的。
通过调用
localDateTime.toInstant(ZoneOffset.UTC)
,您假设您的 localDateTime
实际上是在 UTC 时区测量的。 相反,您应该使用存储 LocalDateTime
的时区。 如果您不知道,那么您的输入数据本质上是模棱两可的,您需要先修复它。
请注意,这与 Unix 纪元通常以 UTC 指定的事实无关。我们不妨说 Unix 纪元是
1970-01-01T08:00:00+08:00
,它在时间上是同一时刻。
其余部分对我来说似乎是正确的。
这里有一个例程,将问题的评论和这个问题的实际答案汇集在一起:
protected Timestamp convertLocalDateTimeToGoogleTimestamp(LocalDateTime localDateTime) {
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
Timestamp result = Timestamp.newBuilder()
.setSeconds(instant.getEpochSecond())
.setNanos(instant.getNano())
.build();
return result;
}
LocalDateTime localDateTime = LocalDateTime.now();//this can be any date
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Timestamp timestamp = Timestamp.newBuilder()
.setSeconds(instant.getEpochSecond())
.setNanos(instant.getNano())
.build();
Timestamp currentTimeStamp =
Timestamps.fromMillis(System.currentTimeMillis());