Conversion of GMT and CST

Source: Internet
Author: User

GMT time is Greenwich Mean time and CST time is four time zones including China, USA, Brazil, Australia.

in JavaScript, the default CST refers to central US time, which is 14 hours apart if GMT is converted to CST in JavaScript. In Java background, the default is Beijing time, GMT conversion to CST is 8 hours. Various places may be different from CST time, so in order to avoid programming errors, GMT time is generally used. Here are three ways to convert from other locations.

  1. The first way:
    Date date = new Date (); Date.togmtstring ();
    Therefore, the method has been invalidated in the high-version JDK and is not recommended for use.
  2. The second way
    DateFormat Cstformat = new SimpleDateFormat (); DateFormat Gmtformat = new SimpleDateFormat (); TimeZone gmttime = Timezone.gettimezone ("GMT"); TimeZone csttime = Timezone.gettimezone ("CST");       
    Get the format is very monotonous, only month and Day + on the afternoon + hours. Not good to use, not recommended
  3. The Third Way
    Public Date GETCST (String strgmt) throws ParseException {    DateFormat df = new SimpleDateFormat ("EEE, d-mmm-yyyy hh:mm: SS Z ", Locale.english);    Return Df.parse (STRGMT); Public String getgmt (Date datecst) {    DateFormat df = new SimpleDateFormat ("EEE, d-mmm-yyyy HH:mm:ss z", Locale.engli SH);    Df.settimezone (Timezone.gettimezone ("GMT")); Modify Time Zone.    
    in general, the date format in the request header of our Web request resembles the following:Thu, 05:49:30 GMT, you can adjust the above format accordingly:
    DateFormat df = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss z", locale.english);
    This method can flexibly control the format of the time, the information is more comprehensive, recommended to use.
  4. Here are the results of three ways to test:





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Conversion of GMT and CST

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.