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 );}}