我运行以下代码并获取不正确的日期(而不是凌晨1/2/2015 6:00:00 AM打印12/28/2015 6:00:00 AM):
SimpleDateFormat _sdf = new SimpleDateFormat("M/d/YYYY H:mm:ss a");
_time = "02/01/2015 6:00:00 AM";
Date date;
try {
date = _sdf.parse(_time);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
_time = _sdf.format(calendar.getTime());
System.out.println(_time); // 12/28/2015 6:00:00 AM !!!
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:1)
尝试使用它:
SimpleDateFormat _sdf = new SimpleDateFormat("M/d/yyyy H:mm:ss a");
即如果YYYY,你需要使用yyyy。
y Year Year 1996; 96
Y Week year Year 2009; 09