Date, TimeZone, MongoDB, time zone problem for date in Java

Source: Internet
Author: User
Tags time zones time 0

Print new Date (),Fri 13:37:51 CST. The time zone of the Asia/shanghai is displayed, but the time zone shorthand for date toString is CST. What's more, Google CST turns out to be central standard time. Represents North American. I also thought that the date class of the JDK was problematic, and debug date tostring found that the name of Asia/shanghai was abbreviated to CST. Then Google, finally saw another: Cst-china, that is, China standard Time. China's standard time zone of course is Beijing time, but in time is +8, is Asia/shanghai.

Asia/shanghai--CST--China Standard Time

The time zone is only a translation of time, which is unique and definite. Although our Beijing time will be more than UTC time 8h, but this is only the difference, in the meaning of the expression of time is equivalent. This means that UTC time 0 o'clock is equivalent to 8 o'clock GMT. This date class is represented by recording the UTC time and the offset, but the different time zones only display different results, but can be converted to each other.

The reason for this confusion is that there are two stages in the use of time. One is the translation stage, the other is the comparative stage. For example, I passed the parameter 2016-08-12 10:31:20, this time does not specify the timezone, if I want to express the Beijing time, then the corresponding is UTC 2:31:20. There's a problem here. MongoDB time is UTC time, I want to query the data before 10 point 31, and then I use the new date to specify the time, it seems I want to get the data before this time, actually is the data before 2:31:20 UTC. In other words, the result I found was not the data before 10:31 UTC time, but the data before 10:31 GMT. That is, I query the data, date standard is according to Beijing time to inquire.

Therefore, if you determine that Java driver automatically converts the time zone in date. That is, add my incoming parameter 2016-08-12 10:31:20, and the default time zone for this machine is Beijing time, and MongoDB's driver converts the date time zone to UTC when querying, which is equivalent to querying 2016-08-12 The data before 2:31:20z. So, my result is the 31 of the Beijing-GMT. If there is no conversion time zone, MongoDB will pass 10:31 as UTC time, then the result of the query is UTC time. So, the key is whether the driver will make the time zone conversion.

Date Sets the time zone :

    1. Set timezone default
    2. Setting timezone directly

1.

 Date date = new   Date ();        System.out.println ( "Default:" +date.tostring ());        Timezone.setdefault (Timezone.gettimezone ( "UTC"  "UTC:" +date);        Timezone.setdefault (Timezone.gettimezone ( "Asia/shanghai"  "Asia/shanghai:" +date);        Timezone.setdefault (Timezone.gettimezone ( CST "));    System.out.println ( CST: "+date); 
default:  Fri 10:22:51 CSTUTC:  02:22:51 UTC (Asia/shanghai):  Fri 10:22:51 CSTCST:  21:22:51 CDT 2016

2.

        New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");        Sdf.settimezone (Timezone.gettimezone ("UTC"));         = Sdf.parse ("2016-08-12 10:31:20");        System.out.println ("Default:" +parse);        Timezone.setdefault (Timezone.gettimezone ("UTC"));        System.out.println ("UTC:  " +parse);
default: Fri 18:31:20 CSTUTC:  10:31:20 UTC 2016

Date, TimeZone, MongoDB, time zone problem for date in Java

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.