Mongo Java driver date conversion

Source: Internet
Author: User
Https://github.com/ewcmsfree/ewcms/wiki/Help-mongo-java-driver-date

The Mongo storage date is a 64-bit integer number. When the Java driver saves the date, it will automatically convert it to the standard time GMT. For example, in GMT + 8 time zone in China, save 00:00:00 to the database. The query result is 16:00:00, which is inconsistent with the expected result. You can find the root cause of the problem in COM. MongoDB. util. JSON:

 if (o instanceof Date) {            Date d = (Date) o;            SimpleDateFormat format =         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");            format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));        serialize(new BasicDBObject("$date", format.format(d)), buf);            return;  }

You can easily find the question and convert the date by yourself:

Simpledateformat
 SimpleDateFormat format =         new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); Date date = format.parse("2012-01-20 00:00:00");

The date is changed to 08:00:00 + 08, that is, 8 hours are automatically added in GMT + 8 time zone.

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.