如何在Java中将Mongo changestream中的Bson时间戳转换为UTC日期格式?
例如:clusterTime=TimeStamp{value=6948482818288648193,seconds=16754329210,inc=1}如何在Java中将Mongo changestream中的Bson时间戳转换为UTC日期格式?,java,mongodb,timestamp,change-data-capture,Java,Mongodb,Timestamp,Change Data Capture,例如:clusterTime=TimeStamp{value=6948482818288648193,seconds=16754329210,inc=1} 当我从document.getClusterTime().toString()读取值时,返回的值是bson timestamp。我想将其转换为UTC时间格式。BSON时间戳值是一个64位数字,其中前32位表示自1970-01-01 Unix纪元00:00 UTC以来的秒数 以下是mongoDB文档的摘录: 时间标记 BSON有一个特殊的时间戳
当我从document.getClusterTime().toString()读取值时,返回的值是bson timestamp。我想将其转换为UTC时间格式。BSON时间戳值是一个64位数字,其中前32位表示自1970-01-01 Unix纪元00:00 UTC以来的秒数 以下是mongoDB文档的摘录: 时间标记 BSON有一个特殊的时间戳类型供内部MongoDB使用 并且与常规日期类型不关联。这个内部 时间戳类型是64位值,其中:
- 最高有效32位是
值(自 Unix时代)时间\u t
- 最低有效32位是递增的序数 对于给定秒内的操作
long timestampValue = 6_948_482_818_288_648_193L;
long unixTimestamp = timestampValue >> 32;
Instant timestamp = Instant.ofEpochSecond(unixTimestamp);
System.out.println(timestamp);
输出:
2021-04-07T18:22:07Z
打印结果以UTC为单位,由尾部的Z
表示
链接:BSON时间戳值是一个64位数字,其中前32位表示自1970-01-01 Unix纪元00:00 UTC以来的秒数 以下是mongoDB文档的摘录: 时间标记 BSON有一个特殊的时间戳类型供内部MongoDB使用 并且与常规日期类型不关联。这个内部 时间戳类型是64位值,其中:
- 最高有效32位是
值(自 Unix时代)时间\u t
- 最低有效32位是递增的序数 对于给定秒内的操作
long timestampValue = 6_948_482_818_288_648_193L;
long unixTimestamp = timestampValue >> 32;
Instant timestamp = Instant.ofEpochSecond(unixTimestamp);
System.out.println(timestamp);
输出:
2021-04-07T18:22:07Z
打印结果以UTC为单位,由尾部的Z
表示
链接:谢谢你的解决方案你是对的,该死的细节。。。然而,BSON有一种特殊的时间戳类型供内部MongoDB使用,因此
时间戳
非常少见。感谢您的解决方案,该死的细节。。。然而,BSON有一种特殊的时间戳类型供内部MongoDB使用,因此时间戳
非常少见。