Why is SimpleDateFormat parsing Date with wrong Month and Day?

1282 views java
5

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM DD HH:mm:ss z yyyy");
try {
      String d1 = list.get(position).getTaskTime();
      date = sdf.parse(d);
      String d2=date.toString();
      Log.d("demo", d2);
  } catch (ParseException e) {
      e.printStackTrace();
     }

Here is the picture of the debugger which shows input and output values. https://i.stack.imgur.com/1t5kA.png

PS: I used Calendar class to add 10 months and made the date right which is kind of a hack but works. But, I want to know what's wrong with my code.

answered question

Please don't link to an image. Instead, edit your question to show what the values are and how they're incorrect.

1 Answer

13

DD is wrong. D is day of year. Sixth day of year is January 6.

You should use dd.

Docs: SimpleDateFormat

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories