GMT time and date format

GMT & time and date format

GMT: Greenwich Mean Time

Beijing time = GMT time + 8 hours

Greenwich Mean Time (GMT, Greenwich Mean Time)

Greenwich is a place on the south bank of the Thames in London, England. Since the beginning of the 19th century, countries around the world have frequent exchanges, and the European, American and Asian continents have their own time zones, so in order to avoid confusion, representatives of all countries are here In 1884, an international conference was held in Washington, USA, and Greenwich in London was selected through an agreement as the center point of global time, and Greenwich Mean Time was born. So a watch with GMT function means that the watch has an hour dial that can display GMT time.

GMT time is the British Greenwich Mean Time, which is the world standard time. It is the local time on the original meridian, which is the zone time of time zone 0, which is 8 hours away from my country's standard time Beijing time (East Eighth District), which is 8 pm hour.

Overview of the SimpleDateFormat class

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-related way. It allows formatting (date -> text), parsing (text -> date) and normalization.

SimpleDateFormat makes it possible to select any user-defined date-time format pattern.

Date and time mode

The date and time format is specified by the date and time pattern string. In the date and time pattern string, the unquoted letters'A' to'Z' and'a' to'z' are interpreted as pattern letters and are used to represent date or time string elements. The text can be enclosed in single quotation marks (') to avoid explanation. "''" means single quotation mark. All other characters are not interpreted; they are simply copied to the output string during formatting, or matched with the input string during parsing.

The following pattern letters are defined (all other characters'A' to'Z' and'a' to'z' are reserved)
Insert picture description hereInsert picture description here
examples

The following example shows how to interpret the date and time pattern in the US locale. The given date and time are 2001-07-04 12:08:56 local time in the US Pacific time zone.
Insert picture description here
Convert GMT and GST dates of type String into Date objects

String stringDate = “Thu Oct 16 07:13:48 GMT 2014”;
SimpleDateFormat sdf = newSimpleDateFormat(“EEE MMM ddHH:mm:ss ‘GMT’ yyyy”,Locale.US);
Date date =sdf.parse(stringDate);
// System.out.println(date.toString());
sdf=newSimpleDateFormat(“yyyy-MM-ddHH:mm:ss”);
System.out.println(sdf.format(date));
代码分析:

When instantiating the SimpleDateFormat class, you need to specify two parameters. The first parameter is the date pattern to be parsed, and the second parameter is to specify the locale. In some cases, the second parameter can be omitted, but when parsing the GMT or GST date of the String type, the second parameter needs to be specified as Locale.US or Locale.UK or Locale.ENGLISH otherwise a compilation error will occur.

Regarding the first parameter, the date mode is different according to the String type date format to be parsed. EEE represents the week, MMM represents the month, dd represents the day, HH represents the hour, mm represents the minute, and ss represents the second. Special attention should be paid here. It is'GMT', which represents the time when the String you want to parse is in GMT format. If GST format, you can use'GST' instead of'GMT'.

Guess you like

Origin blog.csdn.net/qq_41661800/article/details/103968924