0
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.

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

2 Answers2

5

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.

Up to API level 17, only {@code adEhkMmszy} were supported. Note that this class incorrectly implements {@code k} as if it were {@code H} for backwards compatibility.

Community
  • 1
  • 1
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • I believe that using `aaa` would show you the AM / PM on the end. - `"yyyy-MM-dd hh:mm aaa"` – IAmGroot Feb 13 '14 at 15:45
  • @AdamVarhegyi I notice from your comments that `kk` is working but `HH` does not. If you read through the documentation i linked, you will see that `kk` and `HH` are practically the same thing, except one is 0-23 vs 1-24. to whether you want midnight displayed as 24:00 or 00:00 i assume. As is `KK` the same as `hh` with the slight varience. The fact you get `HH` in string instead, is not functioning correctly. (`HH` is the norm, not `kk`) – IAmGroot Feb 13 '14 at 15:55
  • @AdamVarhegyi With some digging it seems to be an API bug. `H` has since been supported on API 17 and above. and `k` is implemented as if it is `H` for backward compatibility. http://stackoverflow.com/questions/16763968/android-text-format-dateformat-hh-is-not-recognized-like-with-java-text-simple – IAmGroot Feb 13 '14 at 16:01
2

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

Community
  • 1
  • 1
Damien R.
  • 3,383
  • 1
  • 21
  • 32