Detailed explanation of Chinese and English time format conversion in Java simpledateformat

Simpledateformat is a concrete class that formats and parses dates in a locale dependent manner. It allows formatting (date - > text), parsing (text - > date), and normalization.

Simpledateformat allows you to select any user-defined date time format pattern. However, it is still recommended to create a date time formatter through gettimeinstance, getdateinstance, or getdatetimeinstance in dateformat. Each such class method can return a date / time formatter initialized in the default format mode. You can use the applypattern method to modify the format pattern as needed.

Date and time mode

Simpledateformat usage

According to the "date and time mode" above, set the mode to be matched to realize the mutual conversion of string and date types, for example:

String type time is converted to date type time. Several common time formats are converted as follows:

a. Time format: "2015-08-28", mode: "yyyy MM DD"

b. Time format: "2015-08-28 18:28:30", mode: "yyyy MM DD HH: mm: SS"

c. Time format: "August 28, 2015", mode: "yyyy-m-d"

d. Time format: "2015-8-28 18:8:30", mode: "yyyy-m-d H: M: s"

e. Time format: "Aug 282015 6:8:30 PM", mode: "MMM D, yyyy H: M: s AA"

f. Time format: "fri Aug 28 18:08:30 CST 2015", mode: "EEE MMM D HH: mm: Ss' CST 'yyyy"

Time of type date is converted to time of type string

This is the reverse operation of "converting a time of string type to a time of date type", as long as date = dateformat Parse ([string type time]); Replace with string date = dateformat Format ([date type time]); Just. For example, format the current time as [yyyy mm / DD / yyyy]:

Note: when we do time format conversion, we mainly find the right pattern matching the time format; In addition, locale. Is required for time conversion in English format English, otherwise the conversion will fail, because it defaults to the localization setting, unless your operating system is in English. In short, the time format needs to be consistent with the mode during time conversion.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>