将字符串转换为日期
来自 SimpleDateFormat 类的 parse() 有助于将 String 模式转换为 Date 对象。
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
String dateStr = "02/25/2016"; // input String
Date date = dateFormat.parse(dateStr);
System.out.println(date.getYear()); // 116
文本格式有 4 种不同的样式,SHORT,MEDIUM(这是默认值),LONG 和 FULL,所有这些都取决于语言环境。如果未指定语言环境,则使用系统默认语言环境。
| 样式 | Locale.US | Locale.France |
|---|---|---|
SHORT |
09 年 6 月 30 日 | 30/06/09 |
MEDIUM |
2009 年 6 月 30 日 | 2009 年 7 月 30 日 |
LONG |
2009 年 6 月 30 日 | 2009 年 7 月 30 日 |
FULL |
2009 年 6 月 30 日,星期二 | mardi 30 juin 2009 |