📄 e321. parsing a date using a custom format.txt
字号:
A pattern of special characters is used to specify the format of the date to parse. The same set of pattern characters are used to format and to parse dates. See e320 Formatting a Date Using a Custom Format for a listing of some pattern characters.
Note: This example parses dates using the default locale (which, in the author's case, is Locale.ENGLISH). If the example is run in a different locale, the text (e.g., month names) will not be the same.
try {
// Some examples
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse("29-Jan-02");
// Parse a date and time; see also
// e317 Parsing the Time Using a Custom Format
formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
date = (Date)formatter.parse("2002.01.29.08.36.33");
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
date = (Date)formatter.parse("Tue, 29 Jan 2002 22:14:02 -0500");
} catch (ParseException e) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -