📄 e318. formatting and parsing a time for a locale using default formats.txt
字号:
Every locale has four default formats for formatting and parsing times. They are called SHORT, MEDIUM, LONG, and FULL. The SHORT format consists entirely of numbers while the FULL format contains most of the time components. There is also a default format called DEFAULT and is the same as MEDIUM.
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.
// Format
Locale locale = Locale.ITALIAN;
Date date = new Date();
String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
// 22.33
s = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale).format(date);
// 22.33.03
s = DateFormat.getTimeInstance(DateFormat.LONG, locale).format(date);
// 22.33.03 PST
s = DateFormat.getTimeInstance(DateFormat.FULL, locale).format(date);
// 22.33.03 PST
s = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale).format(date);
// 22.33.03
// Parse
try {
date = DateFormat.getTimeInstance(
DateFormat.DEFAULT, locale).parse("22.33.03");
} catch (ParseException e) {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -