-2
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM DD HH:mm:ss z yyyy");
try {
      String d1 = list.get(position).getTaskTime(); //here d is "Tue Nov 06 15:37:51 EST 2016"
      date = sdf.parse(d);
      String d2=date.toString();
      Log.d("demo", d2); //here d2 is "Sat Jan 06 15:37:51 EST 2016"
  } catch (ParseException e) {
      e.printStackTrace();
     }

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.

  • 1
    Please don't link to an image. Instead, edit your question to show what the values are and how they're incorrect. – rgettman Nov 07 '18 at 00:41
  • I posted here for the first time. I just used the available option. That link was generated by stackoverflow. Anyways, I updated the post. Thanks! – Teja Karlapudi Nov 07 '18 at 02:18
  • Always search Stack Overflow thoroughly before posting. – Basil Bourque Nov 07 '18 at 07:20
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 07 '18 at 09:07
  • It seems that `getTaskTime(` is returning a `String` where you ought to have a method to return a date-time object so you wouldn’t need to parse at all. Be the return type `Date` or `Instant` (I recommend the latter). – Ole V.V. Nov 07 '18 at 09:11

1 Answers1

3

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

You should use dd.

Docs: SimpleDateFormat

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • Thanks! I didn't know there's an alphabet to specify the day of an year. I assumed D to be the date. I knew it was some silly mistake but didn't know what. – Teja Karlapudi Nov 07 '18 at 00:51