SimpleDateFormat API显示错误的日期

时间:2015-06-02 17:51:30

标签: java simpledateformat

尝试使用SimpleDateFormat API格式化几个日期

String[] dates={"18-01-2015","9-02-2015","21-03-2015"};
for(String s:dates){
    SimpleDateFormat format=new SimpleDateFormat("DD-MM-yyyy");
    Date date=format.parse(s);
    System.out.println(date);
}

输出:

Sun Jan 18 00:00:00 IST 2015
Fri Jan 09 00:00:00 IST 2015
Wed Jan 21 00:00:00 IST 2015

您可以注意到它显示的所有日期是JAN而不是FEB / MAR。

1 个答案:

答案 0 :(得分:6)

构建dd时,您需要DD而不是SimpleDateFormatDD表示"一年中的某一天",而不是"一天中的某一天"。 每个时间SimpleDateFormat看起来都在做错事,你应该查阅documentation并真正仔细检查你的模式文本 - 特别是大写。

(当然还有其他可能出错的事情 - 我最常见的是blog post。)

相关问题