e322. formatting and parsing a date using default formats.txt
来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 33 行
TXT
33 行
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 + =
减小字号Ctrl + -
显示快捷键?