DateFormat提供的值不正确

MRa*_*ran 2 java

我想转换StringDate.我的代码:

    String maturityDate = "20150722";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyymmdd");
    Date date = formatter.parse(maturityDate);
    System.out.println(date);
Run Code Online (Sandbox Code Playgroud)

预期结果 :

Input 20150722
Output Wed Jul 22 00:07:00 IST 2015
Run Code Online (Sandbox Code Playgroud)

实际结果 :

Input 20150722
Output Thu Jan 22 00:07:00 IST 2015
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

Jor*_*lla 5

可能是什么原因?

原因是m字母在SimpleDateFormat模式中意味着分钟.你的意思M是几个月.

解决方案
将格式从更改yyyymmddyyyyMMdd.

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Run Code Online (Sandbox Code Playgroud)

API中,您将找到完整列表:

M   Month in year   Month   July; Jul; 07
m   Minute in hour  Number  30
Run Code Online (Sandbox Code Playgroud)