我想转换String成Date.我的代码:
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)
可能是什么原因?
可能是什么原因?
原因是m字母在SimpleDateFormat模式中意味着分钟.你的意思M是几个月.
解决方案
将格式从更改yyyymmdd为yyyyMMdd.
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)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |