MongoDB UTC time problem, mongodbutc time

Source: Internet
Author: User

MongoDB UTC time problem, mongodbutc time

  • When a batch of data is imported by accident, it is found that the date seen in MongoVUE is 8 hours less than the actual date, because MongDB uses UTC time (Coordinated Time)
  • If you want to display it normally, you can set the destination Vue, select Preferences under Tools, and bring up the settings, for example, set it to Local Timezone, and re-open the view.

  • How many questions are raised, the first one is how to query the date? The second is how to operate in the program?
  • First, during the query, in the tool, MongoDB will automatically query the date minus 8 hours, and follow the normal understanding to perform the operation. Pay attention to the red box in the figure, for example:

  • Second, in the program, the read time and the insert time (using the latest 3.0 Drive API ):
Package org. mice; import java. text. dateFormat; import java. text. simpleDateFormat; import java. util. date; import java. util. timeZone; import org. bson. document; import com. mongodb. using client; import com. mongodb. client. collections collection; import com. mongodb. client. export cursor; import com. mongodb. client. using database; public class Test {/*** Test * @ param args */public static void main (String [] args) {using client mongo = new using client ("localhost ", 27017); Relational Database db = mongo. getDatabase ("chen"); Collect collection <Document> collection = db. getCollection ("Test"); // read Data Using cursor <Document> cursor = collection. find (). iterator (); try {while (cursor. hasNext () {Document temp = cursor. next (); System. out. println (Test. getLocalTimeFromUTC (Date) temp. get ("MyDate");} finally {cursor. close () ;}// insert a Document doc = new Document ("MyDate", new Date (); collection. insertOne (doc);} private static DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); public static String getLocalTimeFromUTC (Date UTCDate) {format. setTimeZone (TimeZone. getDefault (); // if no value is added, return format is returned for the read time. format (UTCDate );}}


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.