Spring Data MongoDB automatically + 8 hours when the results are queried, so we seem to have the right result.
But when we query, and do not automatically + 8 hours, need to deal with their own
Workaround 1 @JsonFormat (timezone = "gmt+8", pattern = "Yyyy-mm-dd HH:mm:ss")
But this note, only for JSON data conversion time processing, if it is a form submitted urlencoded when there is no way to
@Transient = Localdatetimedeserializer. Class) = Localdatetimeserializer. class ) = "Gmt+8", pattern = "Yyyy-mm-dd HH:mm:ss") = "Yyyy-mm-dd HH:mm:ss") Private LocalDateTime CreateDate;
Although we can register the custom format in it, we will handle it automatically when we enter the controller, but maybe we have a different database of MySQL and Mongodb, which is obviously somewhat arbitrary.
@InitBinder publicvoid initbinder (webdatabinder binder)
Workaround 2 When querying MongoDB, manually handle
if(Orderinfo.getcreateenddate ()! =NULL&& orderinfo.getcreatedate ()! =NULL) Query.addcriteria (where ("ObjectId"). GTE (NewObjectId (Localdatetimeutil.localdatetimetoudate (Orderinfo.getcreatedate ().plushours (8))). LTE (NewObjectId (Localdatetimeutil.localdatetimetoudate (Orderinfo.getcreateenddate (). Plushours (8))))); Else{optional.ofnullable (Orderinfo.getcreatedate ()). Ifpresent (CreateDate-Query.addcriteria (WHERE ("ObjectId"). GTE (NewObjectId (Localdatetimeutil.localdatetimetoudate (createdate. Plushours (8)))))); Optional.ofnullable (Orderinfo.getcreateenddate ()). Ifpresent (EndDate-Query.addcriteria (WHERE ("ObjectId"). LTE (NewObjectId (Localdatetimeutil.localdatetimetoudate (endDate. Plushours (8)))))); }
Logback Open Debug Display Spring Data MongoDB query statement, easy to debug
<name= "Org.mongodb.driver" level= "Debug"/> <name= "Org.springframework.data.mongodb.core.MongoTemplate" Level = "Debug" />
{"$gte": {"$date": "2017-11-01t00:00:00.000z"}
JAVA processing Spring Data MongoDB time zone issues