1

I run the following code and obtain incorrect date (instead of 1/2/2015 6:00:00 AM it prints 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();
    }
JoeBlack
  • 471
  • 2
  • 4
  • 13

1 Answers1

1

Try to use this:

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

i.e, you need to use yyyy instead if YYYY.

Check the Oracle Docs

y   Year    Year    1996; 96
Y   Week year   Year    2009; 09
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331