Java date formatting converts a gmt,gst date of type string to date type

Source: Internet
Author: User
Tags days in month local time locale sqlite database

In the actual development process will often need to encapsulate the date type of data in XML or JSON format on the network for transmission, in addition, when the date type of data stored in the SQLite database and then fetched only the date of the string type, because SQLite is untyped. This has to do with the problem of converting a date of type string to date type. Here's how to convert a string-type GMT, GST date to a Date object.

You can use Newdate ("") to convert a date of type string to an object of date type before JDK1.1. But since the beginning of JDK1.1 it is not recommended to do so. The following is an introduction to using the SimpleDateFormat class to convert a date of type string to date object.

SimpleDateFormat 类的概述:

SimpleDateFormatis a specific class that formats and resolves dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization.

SimpleDateFormatAllows you to select any user-defined pattern of date-time formats.

Date and Time mode

Date and time formats are specified by a date and time pattern string. In the date and time pattern strings, the unquoted letters ' a ' to ' Z ' and ' a ' to ' Z ' are interpreted as pattern letters, which are used to represent date or time string elements. Text can be caused by using single quotation marks (') to avoid interpretation. "'" denotes single quotation marks. All other characters are not interpreted; Simply copy them to the output string when they are formatted, or match the input string when parsing.

The following pattern letters are defined (all other characters ' a ' to ' Z ' and ' a ' to ' Z ' are preserved):

Letters

Date or time element

Said

Example

G

Era Marker

Text

AD

Y

Years

Year

1996; 96

M

Month of the Year

Month

July; Jul; 07

W

Number of weeks in the year

Number

27

W

Number of weeks in the month

Number

2

D

Number of days in the year

Number

189

D

Number of days in month

Number

10

F

Week of the Month

Number

2

E

Days of the Week

Text

Tuesday; Tue

A

AM/PM Mark

Text

Pm

H

Number of hours in the day (0-23)

Number

0

K

Number of hours in the day (1-24)

Number

24

K

Number of hours in am/pm (0-11)

Number

0

H

Number of hours in am/pm (1-12)

Number

12

M

Number of minutes in hours

Number

30

S

Number of seconds in minutes

Number

55

S

Number of milliseconds

Number

978

Z

Time

General Time Zone

Pacific Standard Time; PST; gmt-08:00

Z

Time

RFC 822 Time Zone

-0800

Example

The following example shows how to interpret the date and time patterns in the U.S. locale. Given the date and time of the United States Pacific Time zone for local time 2001-07-04 12:08:56.

Date and Time mode

Results

"YYYY. Mm.dd G ' at ' HH:mm:ss z '

2001.07.04 AD at 12:08:56 PDT

"EEE, MMM D, ' yy"

Wed, Jul 4, ' 01

"H:mm a"

12:08 PM

"HH ' o ' clock ' A, zzzz"

O ' clock PM, Pacific Daylight Time

"K:mm A, z"

0:08 PM, PDT

"Yyyyy. MMMMM.DD GGG hh:mm AAA "

02001.july.04 AD 12:08 PM

"EEE, D MMM yyyy HH:mm:ss Z"

Wed, 4 Jul 2001 12:08:56-0700

"Yymmddhhmmssz"

010704120856-0700

"Yyyy-mm-dd ' T ' HH:mm:ss. Sssz "

2001-07-04t12:08:56.235-0700

Converts a String type GMT, GST date to a Date object

String stringdate = "Thu Oct 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));

Code Analysis:

When instantiating the SimpleDateFormat class, you need to specify two parameters, the first parameter is the date pattern to parse, and the second parameter is the locale of the specified language. At some point the second parameter can be omitted, but the second parameter needs to be specified as locale when parsing the GMT or GST date of type string. US or locale. UK or locale. English Otherwise a compilation error will occur.

About the first parameter date pattern is to be resolved according to the string type date format is different, where the Eee represents the week, MMM for the month, DD for the day, HH for the hour, mm for minutes, SS for the second, here to pay special attention to the ' GMT ', It represents the time that you want to parse the string in GMT format, if the GST format can use ' GST ' instead of ' GMT '.

Http://site.leshou.com/s/9584077.html
Http://site.leshou.com/s/9584083.html
Http://site.leshou.com/s/9584089.html
Http://site.leshou.com/s/9584094.html
Http://site.leshou.com/s/9584097.html
Http://site.leshou.com/s/9584111.html
Http://site.leshou.com/s/9584122.html
Http://site.leshou.com/s/9584125.html
Http://site.leshou.com/s/9584128.html
Http://site.leshou.com/s/9584134.html
Http://site.leshou.com/s/9584155.html
Http://site.leshou.com/s/9584161.html
Http://site.leshou.com/s/9584167.html
Http://site.leshou.com/s/9584173.html
Http://site.leshou.com/s/9584178.html
Http://site.leshou.com/s/9584198.html
Http://site.leshou.com/s/9584203.html
Http://site.leshou.com/s/9584208.html
Http://site.leshou.com/s/9584211.html
Http://site.leshou.com/s/9584213.html
Http://site.leshou.com/s/9584236.html
Http://site.leshou.com/s/9584241.html
Http://site.leshou.com/s/9584243.html
Http://site.leshou.com/s/9584248.html
Http://site.leshou.com/s/9584253.html
Http://site.leshou.com/s/9584279.html
Http://site.leshou.com/s/9584284.html
Http://site.leshou.com/s/9584287.html
Http://site.leshou.com/s/9584290.html
Http://site.leshou.com/s/9584295.html
Http://site.leshou.com/s/9583393.html
Http://site.leshou.com/s/9583806.html
Http://site.leshou.com/s/9583814.html
Http://site.leshou.com/s/9583817.html
Http://site.leshou.com/s/9583823.html
Http://site.leshou.com/s/9583827.html
Http://site.leshou.com/s/9583838.html
Http://site.leshou.com/s/9583857.html
Http://site.leshou.com/s/9583859.html
Http://site.leshou.com/s/9583863.html
Http://site.leshou.com/s/9583866.html
Http://site.leshou.com/s/9583884.html
Http://site.leshou.com/s/9583889.html
Http://site.leshou.com/s/9583892.html
Http://site.leshou.com/s/9583896.html
Http://site.leshou.com/s/9583909.html
Http://site.leshou.com/s/9583930.html
Http://site.leshou.com/s/9583935.html
Http://site.leshou.com/s/9583937.html
Http://site.leshou.com/s/9583940.html
Http://site.leshou.com/s/9583944.html
Http://site.leshou.com/s/9583971.html
Http://site.leshou.com/s/9583999.html
Http://site.leshou.com/s/9584003.html
Http://site.leshou.com/s/9584009.html
Http://site.leshou.com/s/9584017.html
Http://site.leshou.com/s/9584048.html
Http://site.leshou.com/s/9584049.html
Http://site.leshou.com/s/9584053.html
Http://site.leshou.com/s/9584057.html
Http://site.leshou.com/s/9582679.html
Http://site.leshou.com/s/9582680.html
Http://site.leshou.com/s/9582681.html
Http://site.leshou.com/s/9582682.html
Http://site.leshou.com/s/9582684.html
Http://site.leshou.com/s/9582885.html
Http://site.leshou.com/s/9582891.html
Http://site.leshou.com/s/9582894.html
Http://site.leshou.com/s/9582900.html
Http://site.leshou.com/s/9582921.html
Http://site.leshou.com/s/9582934.html
Http://site.leshou.com/s/9583191.html
Http://site.leshou.com/s/9583192.html
Http://site.leshou.com/s/9583193.html
Http://site.leshou.com/s/9583195.html
Http://site.leshou.com/s/9583197.html
Http://site.leshou.com/s/9583265.html
Http://site.leshou.com/s/9583267.html
Http://site.leshou.com/s/9583268.html
Http://site.leshou.com/s/9583271.html
Http://site.leshou.com/s/9583274.html
Http://site.leshou.com/s/9583314.html
Http://site.leshou.com/s/9583318.html
Http://site.leshou.com/s/9583325.html
Http://site.leshou.com/s/9583327.html
Http://site.leshou.com/s/9583330.html
Http://site.leshou.com/s/9583359.html
Http://site.leshou.com/s/9583366.html
Http://site.leshou.com/s/9583375.html
Http://site.leshou.com/s/9583386.html
Http://site.leshou.com/s/9580742.html
Http://site.leshou.com/s/9580744.html
Http://site.leshou.com/s/9580747.html
Http://site.leshou.com/s/9580749.html
Http://site.leshou.com/s/9580750.html
Http://site.leshou.com/s/9580765.html
Http://site.leshou.com/s/9580766.html
Http://site.leshou.com/s/9580767.html
Http://site.leshou.com/s/9580769.html
Http://site.leshou.com/s/9580770.html
Http://site.leshou.com/s/9580784.html
Http://site.leshou.com/s/9580785.html
Http://site.leshou.com/s/9580787.html
Http://site.leshou.com/s/9580788.html
Http://site.leshou.com/s/9580791.html
Http://site.leshou.com/s/9580808.html
Http://site.leshou.com/s/9580810.html
Http://site.leshou.com/s/9580812.html
Http://site.leshou.com/s/9580814.html
Http://site.leshou.com/s/9580815.html
Http://site.leshou.com/s/9582060.html
Http://site.leshou.com/s/9582117.html
Http://site.leshou.com/s/9582120.html
Http://site.leshou.com/s/9582124.html
Http://site.leshou.com/s/9582125.html
Http://site.leshou.com/s/9582390.html
Http://site.leshou.com/s/9582393.html
Http://site.leshou.com/s/9582399.html
Http://site.leshou.com/s/9582401.html
Http://site.leshou.com/s/9582409.html
Http://site.leshou.com/s/9580736.html
Http://site.leshou.com/s/9580735.html
Http://site.leshou.com/s/9580734.html
Http://site.leshou.com/s/9580731.html
Http://site.leshou.com/s/9580730.html
Http://site.leshou.com/s/9580724.html
Http://site.leshou.com/s/9580723.html
Http://site.leshou.com/s/9580722.html
Http://site.leshou.com/s/9580721.html
Http://site.leshou.com/s/9580719.html
Http://site.leshou.com/s/9580713.html
Http://site.leshou.com/s/9580712.html
Http://site.leshou.com/s/9580711.html
Http://site.leshou.com/s/9580710.html
Http://site.leshou.com/s/9580709.html
Http://site.leshou.com/s/9580186.html
Http://site.leshou.com/s/9580185.html
Http://site.leshou.com/s/9580182.html
Http://site.leshou.com/s/9580180.html
Http://site.leshou.com/s/9580178.html
Http://site.leshou.com/s/9580162.html
Http://site.leshou.com/s/9580160.html
Http://site.leshou.com/s/9580157.html
Http://site.leshou.com/s/9580143.html
Http://site.leshou.com/s/9580131.html
Http://site.leshou.com/s/9580105.html
Http://site.leshou.com/s/9580099.html
Http://site.leshou.com/s/9580094.html
Http://site.leshou.com/s/9580086.html
Http://site.leshou.com/s/9580081.html
Http://site.leshou.com/s/9580059.html
Http://site.leshou.com/s/9580056.html
Http://site.leshou.com/s/9580049.html
Http://site.leshou.com/s/9580027.html
Http://site.leshou.com/s/9580012.html
Http://site.leshou.com/s/9579882.html
Http://site.leshou.com/s/9579876.html
Http://site.leshou.com/s/9579868.html
Http://site.leshou.com/s/9579863.html
Http://site.leshou.com/s/9579858.html
Http://site.leshou.com/s/9578444.html
Http://site.leshou.com/s/9578429.html
Http://site.leshou.com/s/9578422.html
Http://site.leshou.com/s/9578410.html
Http://site.leshou.com/s/9578406.html
Http://site.leshou.com/s/9578373.html
Http://site.leshou.com/s/9578360.html
Http://site.leshou.com/s/9578353.html
Http://site.leshou.com/s/9578345.html
Http://site.leshou.com/s/9578311.html
Http://site.leshou.com/s/9578293.html
Http://site.leshou.com/s/9578279.html
Http://site.leshou.com/s/9578266.html
Http://site.leshou.com/s/9578258.html
Http://site.leshou.com/s/9578245.html
Http://site.leshou.com/s/9578220.html
Http://site.leshou.com/s/9578199.html
Http://site.leshou.com/s/9578179.html
Http://site.leshou.com/s/9578167.html
Http://site.leshou.com/s/9578155.html
Http://site.leshou.com/s/9578136.html
Http://site.leshou.com/s/9578113.html
Http://site.leshou.com/s/9578107.html
Http://site.leshou.com/s/9578100.html
Http://site.leshou.com/s/9578080.html
Http://site.leshou.com/s/9578038.html
Http://site.leshou.com/s/9578027.html
Http://site.leshou.com/s/9578010.html
Http://site.leshou.com/s/9577997.html
Http://site.leshou.com/s/9577974.html
Http://site.leshou.com/s/9577937.html
Http://site.leshou.com/s/9577886.html
Http://site.leshou.com/s/9577878.html
Http://site.leshou.com/s/9577865.html
Http://site.leshou.com/s/9577843.html
Http://site.leshou.com/s/9577103.html
Http://site.leshou.com/s/9577099.html
Http://site.leshou.com/s/9577076.html
Http://site.leshou.com/s/9577061.html
Http://site.leshou.com/s/9576662.html

Java date formatting converts a gmt,gst date of type string to date type

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.