Java SimpleDateFormat time format conversion in Chinese and English

Source: Internet
Author: User
Tags dateformat days in month locale

SimpleDateFormat is a specific class that formats and parses dates in a language-related way. It allows formatting (date-and text), parsing (text-to-date), and normalization.
SimpleDateFormat allows you to select any user-defined pattern of date-time formats. However, it is still recommended to pass the gettimeinstance,getdateinstance , or getdatetimeinstance in dateformat To create a date-time formatter. Each of these class methods can return a date/time formatter initialized in the default format pattern. You can use the Applypattern method to modify the format pattern as needed.

Date and Time mode
Letters date or time element represents Example
G Era Marker Text
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
SimpleDateFormatHow to use

Depending on the "date and time pattern" above, setting the pattern that needs to be matched, you can implement a string with the date type, for example:

The time of type string is converted to date type time, and several commonly used time format conversions are as follows:

A. Time format: "2015-08-28", Mode: "Yyyy-mm-dd"

new SimpleDateFormat("yyyy-MM-dd");Date date = dateFormat.parse("2015-08-28");
    • 1
    • 2

B. Time format: "2015-08-28 18:28:30", Mode: "Yyyy-mm-dd HH:mm:ss"

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = dateFormat.parse("2015-08-28 18:28:30");
    • 1
    • 2

C. Time format: "2015-8-28", Mode: "Yyyy-m-d"

new SimpleDateFormat("yyyy-M-d");Date date = dateFormat.parse("2015-8-28");
    • 1
    • 2

D. Time format: "2015-8-28 18:8:30", Mode: "Yyyy-m-d h:m:s"

new SimpleDateFormat("yyyy-M-d H:m:s");Date date = dateFormat.parse("2015-8-28 18:8:30");
    • 1
    • 2

E. Time format: "6:8:30 PM", Mode: "MMM d, yyyy h:m:s AA"

new SimpleDateFormat("MMM d, yyyy h:m:s aa", Locale.ENGLISH);Date date = dateFormat.parse("Aug 28, 2015 6:8:30 PM");
    • 1
    • 2

F. Time format: "Fri 18:08:30 CST 2015", Mode: "EEE MMM D HH:mm:ss ' CST ' yyyy"

new SimpleDateFormat("EEE MMM d HH:mm:ss ‘CST‘ yyyy", Locale.ENGLISH);Date date = dateFormat.parse("Fri Aug 28 18:08:30 CST 2015");
    • 1
    • 2
Date type time converted to string type time

This is the inverse of "string type time converted to date type Time", as long as the date date = Dateformat.parse ([string time]); Change to String date = Dateformat.format ([date type Time]); Can. For example, the current time is formatted as [yyyy years M D Day ] in the form of:

new SimpleDateFormat("yyyy年M月d日");String date = dateFormat.format(new Date());
    • 1
    • 2

Note: When we do time format conversion, we mainly find the pattern of matching time format, in addition, the English format time conversion needs to take locale.english, otherwise it will fail, because it defaults to localized settings, unless your operating system is in English, In summary time conversion requires a time format that is consistent with the pattern.

Java SimpleDateFormat time format conversion in Chinese and English

Related Article

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.