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.
- 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.
- 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
- 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.
- 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