parse()from SimpleDateFormatclass有助于将String模式转换为Date对象。
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US); String dateStr = "02/25/2016"; // 输入字符串 Date date = dateFormat.parse(dateStr); System.out.println(date.getYear()); // 116
文本格式有4种不同的样式SHORT,,MEDIUM(这是默认设置)LONG和FULL,所有样式均取决于语言环境。如果未指定语言环境,则使用系统默认语言环境。
| 风格 | 语言环境 | 法国 |
|---|---|---|
| 短 | 2009年6月30日 | 30/06/09 |
| 中 | 2009年6月30日 | 2009年6月30日 |
| 长 | 2009年6月30日 | 2009年6月30日 |
| 充分 | 2009年6月30日,星期二 | 2009年3月30日狂欢 |