[Preface]
Recently, the project requires a special date format, which exactly belongs to the string type. In the past, simpledateformat was not used to format the date. The idea in my mind is to first give the date to tostring, then cut the string slowly! Oh, my God, there are all unscientific methods. After reading the notes, you can use simpledateformat to process them. Here we will summarize the usage of this class!
[Post the summary first]
In simpledateformat, letters have special meanings, such as "YYYY" and "mm". Therefore, it is necessary to remember these mappings. The following lists the commonly used ing relationships:
Meaning |
Symbol |
Ad |
G |
Chinese day of the week |
Eee |
Chinese month |
Mmm |
Abbreviated year, for example, 14 years |
YY |
Morning |
A |
Normal numeric date |
Yyyy: mm: dd'at' hh: mm: SS |
Effect |
Corresponding format |
A.m. CST |
Yyyy. Mm. dd g'at' hh: mm: SS Z |
|
|
Saturday, July 14, '14 |
Eee, mmm D, ''yy |
|
|
10 AM |
H: mm |
|
|
10 AM, CST |
K: Mm A, Z |
|
|
A.m. CST |
Yyyy. Mm. dd g'at' hh: mm: SS Z |
|
|
02014. August a.m. |
Yyyyy. Mmmmm. dd ggg hh: Mm aaa |
|
|
Saturday, 2 August 14 10:10:23 + 0800 |
Eee, d Mmm yyyy hh: mm: SS Z |
|
|
140802101023 + 0800 |
Yymmddhhmmssz |
|
|
2014-08-02t10: 10: 23.164 + 0800 |
Yyyy-mm-dd't'hh: mm: Ss. sssz |
[Use demo]
1 public class Test { 2 3 public static void testSiampleDateFormat(){ 4 Date date = new Date(); 5 6 SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy.MM.dd G ‘at‘ HH:mm:ss z"); 7 String s1 = simpleDateFormat1.format(date); 8 System.out.println("yyyy.MM.dd G ‘at‘ HH:mm:ss z-----------------"+s1); 9 10 SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("EEE, MMM d, ‘‘yy");11 String s2 = simpleDateFormat2.format(date);12 System.out.println("EEE, MMM d, ‘‘yy-----------------"+s2);13 14 SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("h:mm a");15 String s3 = simpleDateFormat3.format(date);16 System.out.println("h:mm a-----------------"+s3);17 18 SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("K:mm a, z");19 String s4 = simpleDateFormat4.format(date);20 System.out.println("K:mm a, z-----------------"+s4);21 22 SimpleDateFormat simpleDateFormat5 = new SimpleDateFormat("yyyy.MM.dd G ‘at‘ HH:mm:ss z");23 String s5 = simpleDateFormat5.format(date);24 System.out.println("yyyy.MM.dd G ‘at‘ HH:mm:ss z-----------------"+s5);25 26 SimpleDateFormat simpleDateFormat6 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");27 String s6 = simpleDateFormat6.format(date);28 System.out.println("yyyyy.MMMMM.dd GGG hh:mm aaa-----------------"+s6);29 30 SimpleDateFormat simpleDateFormat7 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");31 String s7 = simpleDateFormat7.format(date);32 System.out.println("EEE, d MMM yyyy HH:mm:ss Z-----------------"+s7);33 34 SimpleDateFormat simpleDateFormat8 = new SimpleDateFormat("yyMMddHHmmssZ");35 String s8 = simpleDateFormat8.format(date);36 System.out.println("yyMMddHHmmssZ-----------------"+s8);37 38 SimpleDateFormat simpleDateFormat9 = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSSZ");39 String s9 = simpleDateFormat9.format(date);40 System.out.println("yyyy-MM-dd‘T‘HH:mm:ss.SSSZ-----------------"+s9);41 }42 43 public static void main(String[] args) {44 45 testSiampleDateFormat();46 }47 }
View code
[Output result]
1 YYYY. mm. dd g 'at' hh: mm: ss z ----------------- 2014.08.02 ad at 10:10:23 cst2 EEE, mmm D, ''yy --------------- Saturday, July 22, 143, '2017 h: mm a ----------------- am 4 K: Mm A, Z ----------------- AM, cst5 YYYY. mm. dd g 'at' hh: mm: ss z ----------------- 2014.08.02 ad at 10:10:23 cst6 yyyyy. mmmmm. dd ggg hh: Mm AAA ----------------- 02014. august. 02 10:10:23 a.m. 7 EEE, d Mmm yyyy hh: mm: ss z ----------------- Saturday, 2 August 14 + 08008 yymmddhhmmssz ----------------- 140802101023 + 08009 yyyy-mm-dd 't'hh: MM: Ss. SSSZ-----------------2014-08-02T10: 10: 23.164 + 0800
View code