-1

I have a problem in date conversion in my android application. I have date strings like 2017-11-11 11:52 which its date is equal to 2017-Nov-11 but it is parsed as 2017-01-11 in below code snippet:

DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
try {
    Date date = df.parse("2017-11-11 11:52");
    Log.v("DATE_TAG","Date Time:"+date.toString());

} catch (ParseException e) {
    e.printStackTrace();
}

The log output of above code is "Wed Jan 11 11:52:00 GMT+03:30 2017".

Is there anything wrong in my date format string?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
VSB
  • 9,825
  • 16
  • 72
  • 145

4 Answers4

3

You are using a wrong dateformat. DD stands for the day of the year, not the day of the month. You have to use dd instead. You can check the SimpleDateFormat documentation, where it is stated that DD can range from 1 to 365.

So your code should look like this:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
  Date date = df.parse("2017-11-11 11:52");
  Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
  e.printStackTrace();
}
W3hri
  • 1,563
  • 2
  • 18
  • 31
2

D is day in year e.g 189. Use d instead

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

Clearly noted from your output : Read Document

D is Day in year (1-365)
d is day in month (1-31)

Change this

DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");

to

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0

D is used for (Day in year), so in you case you need to use d (is day in month ). please use this Format "yyyy-MM-dd HH:mm"

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39