Daniel Haughton
Daniel Haughton

Reputation: 1215

Convert String to Calender- Calender is the wrong date

trying to parse the String "2019-08-21 9:04:08" into a Calender object.

I am getting the result Mon Dec 31 09:04:08 GMT 2018 from calender.getTime()

Here is how I parse it

Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
    try {
        cal.setTime(sdf.parse("2019-08-21 9:04:08"));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }// all done
    return cal;
}

Upvotes: 0

Views: 50

Answers (1)

Alain Cruz
Alain Cruz

Reputation: 5097

You are not using the right parser format. The one you need is "yyyy-MM-dd hh:mm:ss". The format "YYYY" is week-based calendar year, which is not what you want. As @rajah9 points out, the documentation can guide further regarding the use of SimpleDateFormat.

Upvotes: 3

Related Questions