日历提供-12小时

时间:2014-02-13 15:35:02

标签: android android-date

Calendar c = Calendar.getInstance(); 
String myDateString = (String) DateFormat.format("yyyy-MM-dd hh:mm", c.getTime());

dateTv.setText(myDateString);

输出结果为:

2014-02-13 04:31

小时实际上不是04,它是16,我的意思是它是在名词之后,而不是在午夜之后(早上4点)如果你知道我的意思。

为什么会发生这种情况以及如何解决它?

E D I T:

对于可爱的-1选民:

有人建议:

  String myDateString = (String) DateFormat.format("yyyy-MM-dd HH:mm", c.getTime());

这个解决方案给了我这个确切的字符串:

2014-02-13 HH:45

YES。我的小时里有2个'H'字符。它只是一个H字符,而不是数字。

2 个答案:

答案 0 :(得分:5)

使用HH

hh指使用PM和AM的小时数。你没有显示

String myDateString = (String) DateFormat.format("yyyy-MM-dd HH:mm", c.getTime());

根据Android documentation

  

H =白天(0-23)

     

h =上午/下午(1-12)小时

编辑:似乎存在某种问题,{17}以下的API不支持H,因此最好使用kk,这实际上是正确的HH实施。

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.

答案 1 :(得分:2)

尝试替换" hh" by" kk"如下所示:

Calendar c = Calendar.getInstance(); 
String myDateString = (String) DateFormat.format("yyyy-MM-dd kk:mm", c.getTime());

dateTv.setText(myDateString);

从这里开始:https://stackoverflow.com/a/7078488/2065418