[英]How to convert google.protobuf.Timestamp to a MongoDB ISO date in Spring Boot?
In my Spring Boot project I get timestamps in of the type google.protobuf.Timestamp
in my gRPC Service.在我的 Spring Boot 项目中,我在我的 gRPC 服务中获得类型为google.protobuf.Timestamp
的时间戳。 How do I insert that timestamp into my MongoDB in the following format:如何以以下格式将该时间戳插入到我的 MongoDB 中:
Example: 2022-01-14T13:00:00.000+02:00
The timestamp has a seconds and a nanos field.时间戳有一个seconds和一个nanos字段。 I've tried to say dataList.getTime.getNanos()
but this inserts a time in from year 1971. How do I get the correct time?我试图说dataList.getTime.getNanos()
但这会插入 1971 年的时间。如何获得正确的时间?
I don't know the answer as I don't use Java|Spring or MongoDB but...我不知道答案,因为我不使用 Java|Spring 或 MongoDB,但是......
MongoDB uses milliseconds and supports RFC3339 MongoDB 使用毫秒并支持 RFC3339
Timestamp's Java implementation (4.0.0-rc2) supports toMillis
and toString
methods. Timestamp 的 Java 实现 (4.0.0-rc2) 支持toMillis
和toString
方法。
I think you've at least 2 options:我认为您至少有两个选择:
Either new Date(dataList.getTime.toMillis())
assuming that there's aa constructor for Date
that accepts milliseconds.要么new Date(dataList.getTime.toMillis())
假设有一个Date
的构造函数接受毫秒。
Or new ISODate(dataList.getTime.String())
return a Date
constructed from the RFC3339 string representation.或者new ISODate(dataList.getTime.String())
返回从 RFC3339 字符串表示构造的Date
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.