Special characters of Java date formatting

Source: Internet
Author: User

Formatting dates in Java is relatively straightforward, and the usual code is as follows:

<span style= "White-space:pre" ></span>string pattern = "YYYY year mm month DD Day";        SimpleDateFormat SDF = new SimpleDateFormat (pattern);        System.out.println (Sdf.format (New Date ()));

This allows you to simply format the date with the result: January 29, 2015

However, in recent work encountered such a requirement, the format is required to display the style is: Year W week, that is also the above date as an example, the format to be displayed is: 15w05.

There is no way to format a normal method, because W represents the week in the format rule. At that time, two were used to stitch together after formatting.

<span style= "White-space:pre" ></span>string pattern1 = "yy";        SimpleDateFormat sdf1 = new SimpleDateFormat (PATTERN1);        String pattern2 = "ww";        SimpleDateFormat sdf2 = new SimpleDateFormat (pattern2);        System.out.println (Sdf1.format (new Date ()) + "W" +sdf2.format (new Date ()));
Today suddenly found that there is actually a simple way to achieve, just use the single quotation mark in the W can be. The following code

String pattern3 = "yy ' W ' ww";        SimpleDateFormat sdf3 = new SimpleDateFormat (PATTERN3);        System.out.println (Sdf3.format (New Date ()));


Special characters of Java date formatting

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.