Storing java 8 LocalDate in mongo DB

依然范特西╮ 提交于 2019-11-30 20:43:06

The mongo-java client for a date object returns as instance of java.util.Date.

The problem could possibly be that while you save the startDate and the endDate value, its toString() method would probably use the JVM's default time zone to update the value.

The doc here states that The official BSON specification refers to the BSON Date type as the UTC datetime. and that could be the reason your LocalDateTime attributes were converted to the UTC time zone prior to being saved to the DB.

Also to avoid such confusion would suggest using the bson type timestamp to update date fields.

In the MongoDB Java Driver 3.7 release : http://mongodb.github.io/mongo-java-driver/3.7/whats-new/ we can see that the driver now support LocalDate :

JSR-310 Instant, LocalDate & LocalDateTime support Support for Instant, LocalDate and LocalDateTime has been added to the driver.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!