SimpleDateFormat is a concrete class that formats and parses dates in a locale-related way. It allows formatting (date->text), parsing (text->date), and normalization.
SimpleDateFormat enables you to select any user-defined date-time format pattern. However, it is still recommended to create a date-time formatter through getTimeInstance, getDateInstance or getDateTimeInstance in DateFormat. Each such class method can return a date/time formatter initialized in the default format mode. The format pattern can be modified using the applyPattern method as needed.
Date and time mode
SimpleDateFormat usage method
According to the "Date and Time Mode" above, set the pattern that needs to be matched, and the interchange between String and Date types can be achieved, for example:
The time of String type is converted to Date type time. Several commonly used time formats are 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");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");c. Time format: “2015-8-28”, mode: “yyyy-Md”
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-Md");Date date = dateFormat.parse("2015-8-28");d. Time format: “2015-8-28 18:8:30”, Mode: “yyyy-Md H:m:s”
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-Md H:m:s");Date date = dateFormat.parse("2015-8-28 18:8:30");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");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");Convert Date type time to String type time
This is the reverse operation of "Converting the time of type String to Date type time". Just change Date date = dateFormat.parse([String type time]); to String date = dateFormat.format([Date type time]);. For example, format the current time into the form [yyyyy year M month d day]:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyy year M month d day"); String date = dateFormat.format(new Date());Note: When we do time format conversion, we mainly find the right pattern matching the time format; in addition, when converting time in English format, you need to bring Locale.ENGLISH, otherwise the conversion will fail because it defaults to localized settings, unless your operating system is in English, in short, the time format and mode need to be consistent during time conversion.
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.