SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-dependent manner. It allows formatting (date -> text), parsing (text -> date) and normalization.
SimpleDateFormat makes it possible to select any user-defined date-time format pattern. However, it is still recommended to create date-time formatters via getTimeInstance , getDateInstance or getDateTimeInstance in DateFormat. Each such class method can return a date/time formatter initialized with the default format mode. The format pattern can be modified as needed using the applyPattern method.
date and time mode
letter | date or time element | Express | Example |
---|---|---|---|
G | Era | marker | Text |
and | year | Year | 1996; 96 |
M | month of the year | Month | July; Jul; 07 |
w | week of the year | Number | 27 |
W | week of month | Number | 2 |
D | days of the year | Number | 189 |
d | the number of days in the month | Number | 10 |
F | week of the month | Number | 2 |
E | days of the week | Text | Tuesday; Tue |
a | am/pm tags | Text | PM |
H | Hour of the day (0-23) | Number | 0 |
k | Hours of the day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hours in am/pm (1-12) | Number | 12 |
m | minutes of hours | Number | 30 |
s | seconds in minutes | Number | 55 |
S | milliseconds | Number | 978 |
with | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
WITH | Time zone | RFC 822 time zone | -0800 |
SimpleDateFormat usage method
According to the above "date and time pattern", set the pattern that needs to be matched, you can realize the mutual conversion between String and Date type, for example:
The time of type String is converted into time of Date type. Several commonly used time formats are converted as follows:
a. Time format: "2015-08-28", Mode: "yyyy-MM-dd"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse("2015-08-28");
- 1
- 2
b. Time format: "2015-08-28 18:28:30", Mode: "yyyy-MM-dd HH:mm:ss"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2015-08-28 18:28:30");
- 1
- 2
c. Time format: "2015-8-28", Mode: "yyyy-Md"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
Date date = dateFormat.parse("2015-8-28");
- 1
- 2
d. Time format: "2015-8-28 18:8:30", mode: "yyyy-Md H:m:s"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
Date date = dateFormat.parse("2015-8-28 18:8:30");
- 1
- 2
e. Time format: "Aug 28, 2015 6:8:30 PM", Mode: "MMM d, yyyy h:m:s aa"
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy h:m:s aa", Locale.ENGLISH);
Date date = dateFormat.parse("Aug 28, 2015 6:8:30 PM");
- 1
- 2
f. Time format: "Fri Aug 28 18:08:30 CST 2015", Mode: "EEE MMM d HH:mm:ss 'CST' yyyy"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss 'CST' yyyy", Locale.ENGLISH);
Date date = dateFormat.parse("Fri Aug 28 18:08:30 CST 2015");
- 1
- 2
Convert Date type time to String type time
This is the reverse operation of "converting a time of type String to a time of Date type", just replace Date date = dateFormat.parse([String type time]); with String date = dateFormat.format([Date type time]); i.e. Can. For example, to format the current time as [yyyy year M month d day]:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日");
String date = dateFormat.format(new Date());
- 1
- 2
Note: When we are doing time format conversion, we are mainly looking for a pattern that matches the time format; in addition, when converting the time in English format, we need to bring Locale.ENGLISH, otherwise the conversion will fail, because it defaults to the localized setting. Unless your operating system is in English, in short, the time format and mode need to be consistent during time conversion.