SimpleDateFormat date format (shallow)

Source: Internet
Author: User
Tags alphabetic character

Java uses the SimpleDateFormat class's constructor, SimpleDateFormat (String str), to construct a formatted date format.

Formats the specified Date object as a string in the specified format through the format (date date) method.

I wrote an example myself:

@Test Public voidtestBasicPTFWJG1 () {SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Date Date=NULL; Try{Date= Sdf.parse ("2016-10-20 11:27:08"); } Catch(ParseException e) {e.printstacktrace (); } SimpleDateFormat sdf1=NewSimpleDateFormat ("Yyyymmddhhmmss"); String ABC=Sdf1.format (date); SimpleDateFormat SDF2=NewSimpleDateFormat ("Yyyymmddhhmmss"); String BCD=Sdf2.format (date); System.out.println ("Abc=" +ABC); System.out.println ("Bcd=" +BCD); }

The format of the string in the SimpleDateFormat constructor, and the meaning of the parts represented:

ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classtest{ Public Static voidMain (String args[]) {Date newtime=NewDate (); //set the time formatSimpleDateFormat SDF1 =NewSimpleDateFormat ("y-m-d h:m:s a E"); SimpleDateFormat SDF2=NewSimpleDateFormat ("Yy-mm-dd hh:mm:ss a E"); SimpleDateFormat sdf3=NewSimpleDateFormat ("yyyy-mmm-ddd hhh:mmm:sss a E"); SimpleDateFormat SDF4=NewSimpleDateFormat ("yyyyy-mmmm-dddd hhhh:mmmm:ssss a E"); //Gets the time, is the time of the machineString formatDate1 =Sdf1.format (newtime); String FormatDate2=Sdf2.format (newtime); String FormatDate3=Sdf3.format (newtime); String formatDate4=Sdf4.format (newtime);          System.out.println (FORMATDATE1);         System.out.println (FORMATDATE2);         System.out.println (FormatDate3);     System.out.println (FORMATDATE4); }}

The string "Yyyy-mm-dd Hh:mm:ss", Where:

YYYY: Represents the year (not case sensitive) assuming the year is 2017

"Y", "yyy", "yyyy" match all 4-bit full years such as: "2017"

"YY" matches the following two digits of the year: "15"

More than 4 digits will be preceded by the year with a "0" complement such as "YYYYY" corresponding to "02017"

MM: Represents the month (uppercase only) if the month is 9

"M" corresponds to "9"

"MM" corresponds to "09"

"MMM" corresponds to "Sep"

"MMMM" corresponds to "Sep"

Exceeds 3 bits, still corresponds to "September"

DD: Representative day (lowercase only) assumed to be number 13th

"D", "DD" corresponds to "13"

Exceeding 2 digits, the number is preceded by a "0" complement. For example, "dddd" corresponds to "0013"

HH: When represented (case-sensitive, uppercase 24-timing, lowercase 12-timing) assumed to be 15 o'clock

"H", "HH" corresponds to "15", more than 2 digits, the number is preceded by a "0" complement. For example, "HHHH" corresponds to "0015"

"H" corresponds to "3"

"HH" corresponds to "03", which exceeds 2 digits and adds "0" to the front of the number. For example, "HHHH" corresponds to "0003"

MM: Delegate (lowercase only) assuming 32 points

"M", "MM" corresponds to "32", more than 2 digits, will be preceded by the number of "0" complement. For example, "MMMM" corresponds to "0032"

SS: Represents seconds (lowercase only) assumed to be 15 seconds

"S", "SS" corresponds to "15", more than 2 bits, will be preceded by the number of "0" complement. For example, "SSSS" corresponds to "0015"

E: Delegate week (uppercase only) assumed to be Sunday

"E", "EE", "EEE" all correspond to "Sun"

"Eeee" corresponds to "Sunday", exceeding 4 digits, still corresponding to "Sunday"

A: On behalf of the morning or afternoon, if it is the morning corresponding to "AM", if it is the afternoon corresponding to "PM"

The delimiter "-" can be substituted with any other non-alphabetic character (or kanji), for example:

Partial Modifications:

Operation Result:

Reference: SimpleDateFormat date format (shallow)

SimpleDateFormat date format (shallow)

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.