JAVA processing Spring Data MongoDB time zone issues

Source: Internet
Author: User
Tags mongodb query

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.