e317. parsing the time using a custom format.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 16 行
TXT
16 行
A pattern of special characters is used to specify the format of the time to parse. The same set of pattern characters are used to parse times. See e316 Formatting the Time Using a Custom Format for a listing of some pattern characters.
Note: This example formats 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("hh.mm.ss a");
Date date = (Date)formatter.parse("02.36.33 PM");
formatter = new SimpleDateFormat("HH:mm:ss Z");
date = (Date)formatter.parse("22:14:02 -0500");
} catch (ParseException e) {
}
Related Examples
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?