【问题标题】:How to convert google.protobuf.Timestamp to a MongoDB ISO date in Spring Boot?如何在 Spring Boot 中将 google.protobuf.Timestamp 转换为 MongoDB ISO 日期?
【发布时间】:2022-07-07 00:12:22
【问题描述】:

在我的 Spring Boot 项目中,我在我的 gRPC 服务中获得了 google.protobuf.Timestamp 类型的时间戳。如何按以下格式将该时间戳插入我的 MongoDB:

Example: 2022-01-14T13:00:00.000+02:00

时间戳有一个 seconds 和一个 nanos 字段。我试着说 dataList.getTime.getNanos() 但这会插入 1971 年的时间。我如何获得正确的时间?

【问题讨论】:

    标签: mongodb spring-boot timestamp grpc nano


    【解决方案1】:

    我不知道答案,因为我不使用 Java|Spring 或 MongoDB,但是...

    MongoDB 使用milliseconds and supports RFC3339

    Timestamp 的 Java 实现 (4.0.0-rc2) 支持 toMillis and toString 方法。

    我认为你至少有两个选择:

    new Date(dataList.getTime.toMillis()) 假设有一个接受毫秒的Date 的构造函数。

    或者new ISODate(dataList.getTime.String())返回一个从RFC3339字符串表示构造的Date

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 2015-03-25
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多