Pergunta

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.

Foi útil?

Solução 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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top