Reputation: 9894
Calendar c = Calendar.getInstance();
String myDateString = (String) DateFormat.format("yyyy-MM-dd hh:mm", c.getTime());
dateTv.setText(myDateString);
The output is:
2014-02-13 04:31
The hour is actually not 04, it is 16, i mean it is after noun, not after midnight (4 in the morning) if you know what i mean.
Why is it happening and how coul i fix it?
E D I T:
For the lovely -1 voters:
As somebody suggested:
String myDateString = (String) DateFormat.format("yyyy-MM-dd HH:mm", c.getTime());
This solution gives me this exact String:
2014-02-13 HH:45
YES. There are 2 'H' characters in my hours. It is exactly a H character, not a number.
Upvotes: 0
Views: 85
Reputation: 13855
use HH
hh
refers to hours with the use of PM and AM. of which you do not display
String myDateString = (String) DateFormat.format("yyyy-MM-dd HH:mm", c.getTime());
As per the Android documentation
H = hour in day (0-23)
h = hour in am/pm (1-12)
Edit: There appears to be some kind of issue with H
not being supported on API below 17, so it is preferred to use kk
which is infact the proper HH
implementation.
Upvotes: 5
Reputation: 3373
Try to replace "hh" by "kk" like below:
Calendar c = Calendar.getInstance();
String myDateString = (String) DateFormat.format("yyyy-MM-dd kk:mm", c.getTime());
dateTv.setText(myDateString);
From here: https://stackoverflow.com/a/7078488/2065418
Upvotes: 2