【发布时间】:2017-10-28 20:49:51
【问题描述】:
我已经玩了将近 6 个小时,并查看了其他问题,但似乎无法弄清楚我做错了什么,这给了我这些奇怪的结果。下面是我的代码
String dateStr = "20171230";
StringBuilder sb = new StringBuilder();
sb.append(dateStr.subSequence(0, 4))
.append("/")
.append(dateStr.substring(4,6))
.append("/")
.append(dateStr.substring(6,8));
dateStr = sb.toString();
System.out.println("date string is " + dateStr);
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
df.setLenient(false);
Date Date = null;
try {
Date = df.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Date is " + Date);
输出是
date string is 2017/12/30
Date is Mon Jan 30 00:12:00 CST 2017
日期应该是 12 月 30 日,而不是 1 月 30 日。谁能告诉我为什么会这样。
【问题讨论】:
标签: java date simpledateformat date-parsing