Most of the content of this article is based on official documents and online experience summary, and has been sorted and accumulated through personal practice, for reference only.
java.text.SimpleDateFormat formats and parses dates in a regional locale-sensitive manner, which can format dates to specified strings and parse strings to dates.
java.text.SimpleDateFormat can format dates based on user-defined schema
@Testpublic void test() { Calendar calendar = Calendar.getInstance(); calendar.set(2016, 11, 30, 24, 59, 59); Date date = new Date(calendar.getTimeInMillis()); String pattern = "G yyyy year MM month dd day HH point mm minute ss second SSS milliseconds"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day kk point mm minute ss second SSS milliseconds"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day hh point mm minute ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G yyyy year MM month dd day KK point mm minute ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G YYYY year MM month dd day KK point mm minute ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "G YYY year MM month dd day KK point mm minute ss second SSS millisecond a"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "W Week in 2016"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "W Week in December 2016"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Day D of 2016"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Day d of December 2016"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "December 31, 2016 is the F week of December 2016"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "E"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "December 31, 2016 is the u day of the week"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Atlantic Standard Time: z"; System.out.println(new SimpleDateFormat(pattern).format(date)); pattern = "Z"; System.out.println("RFC822 time zone: " + new SimpleDateFormat(pattern).format(date)); pattern = "X"; System.out.println("ISO8601 time zone: " + new SimpleDateFormat(pattern).format(date));}Running results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.