【发布时间】:2016-03-09 12:25:54
【问题描述】:
我使用下面的代码来格式化日期。但是当我以不正确的格式提供数据时,它会给出意想不到的结果。
DateFormat inputFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");`
String dateVal = "3/8/2016 12:00:00 AM";
try {
Date date = inputFormat.parse(dateVal);
String formattedVal = outputFormat.format(date);
System.out.println("formattedVal : "+formattedVal);
} catch (ParseException pe) {
throw pe;
}
在上述情况下,输出为 -formattedVal : 0009-02-05。
它不是抛出 Parse 异常,而是解析值并给我一个不正确的输出。有人可以帮我理解这种异常行为吗?
【问题讨论】:
-
预期输出应该如何?