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