I have a date which is stored in string format, under the following convention yyyy-mm-dd, and I want to get the month, so this is what I'm doing
String dateS = j.getString(key);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
if ("null".equals(dateS))
dateS = j.getString("created");
try {
cal.setTime(sdf.parse(dateS));
System.out.println("parsed: " + sdf.parse(dateS));
System.out.println("original: " + dateS);
System.out.println("Month: " + cal.get(Calendar.MONTH));
} catch (ParseException e) {
// null, for now don't do anything, but this should never happen
}
And this is the output:
parsed: Sun Jan 19 00:02:00 EST 2014
original: 2014-02-19
Month: 0
As you can see, it's not parsing it into the right format, what am I doing wrong? is new SimpleDateFormat("yyyy-mm-dd"); not the right way to do it?