处理后获得的日期不正确

时间:2016-03-23 14:02:52

标签: java date simpledateformat

我运行以下代码并获取不正确的日期(而不是凌晨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();
    }

1 个答案:

答案 0 :(得分:1)

尝试使用它:

SimpleDateFormat _sdf = new SimpleDateFormat("M/d/yyyy  H:mm:ss a");

即如果YYYY,你需要使用yyyy。

检查Oracle Docs

y   Year    Year    1996; 96
Y   Week year   Year    2009; 09