Followings is the use of characters in date formatting patterns.
Sr.No. | Class & Description |
---|---|
1 | G To display Era. |
2 | y To display Year. Valid values yy, yyyy. |
3 | M To display Month. Valid values MM, MMM or MMMMM. |
4 | d To display day of month. Valid values d, dd. |
5 | h To display hour of day (1-12 AM/PM). Valid value hh. |
6 | H To display hour of day (0-23). Valid value HH. |
7 | m To display minute of hour (0-59). Valid value mm. |
8 | s To display second of minute (0-59). Valid value ss. |
9 | S To display milliseconds of minute (0-999). Valid value SSS. |
10 | E To display Day in week (e.g Monday, Tuesday etc.) |
11 | D To display Day in year (1-366). |
12 | F To display Day of week in month (e.g. 1st Thursday of December). |
13 | w To display Week in year (1-53). |
14 | W To display Week in month (0-5) |
15 | a To display AM / PM |
16 | k To display Hour in day (1-24). |
17 | K To display Hour in day, AM / PM (0-11). |
18 | z To display Time Zone. |
In this example, we're formatting dates based on different patterns.
IOTester.java
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) throws ParseException { String pattern = "dd-MM-yy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date date = new Date(); System.out.println(simpleDateFormat.format(date)); pattern = "MM-dd-yyyy"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); pattern = "yyyy-MM-dd HH:mm:ss"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); } }
It will print the following result.
29-11-17 11-29-2017 2017-11-29 18:47:42 Wednesday November 2017 18:47:42.787+0530Print