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