Customized date format-simpledateformat

Source: Internet
Author: User

  [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

 

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.