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.
SimpleDateFormat
and friends, and adding ThreeTenABP to your Android project in order to usejava.time
, the modern Java date and time API. It is so much nicer to work with. – CrosseyegetTaskTime(
is returning aString
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 typeDate
orInstant
(I recommend the latter). – Crosseye