24小时制不在dateformat android中工作

use*_*849 8 java time android date

我已经提出了一个应用程序,我需要以24小时格式执行日期转换.这是我的代码.

GregorianCalendar c = new GregorianCalendar(Locale.GERMANY);
            c.set(2011, 04, 29,0,0,0);
            String cdate = (String) DateFormat.format("yyyy-MM-dd HH:mm:ss", c.getTime());
            Log.i(tag,cdate);
Run Code Online (Sandbox Code Playgroud)

现在,当我在这里检查我的LOG是输出:

04-22 12:44:15.956:INFO/GridCellAdapter(30248):2011-04-29 HH:00:00

为什么小时字段没有设置.我在制作日历对象时显式传递了0,仍然在LOG中显示HH.我已经尝试过,当时正在设置,但是HH没有被设置.

可能是什么问题呢?

先感谢您.

小智 15

String cdate = (String) DateFormat.format("yyyy-MM-dd kk:mm:ss", c.getTime());
Run Code Online (Sandbox Code Playgroud)

  • 根据文档,HH应该是0-23,但是对于我来说,在字符串中返回HH,如果你使用kk它会给你0-23而不是1-24.http://developer.android.com/reference/java/text/SimpleDateFormat.html (2认同)

raj*_*ath 10

尝试使用SimpleForDmatFormat,它是DateFormat的子类.这可能会纠正你遇到的问题.

例如,打印当前日期时间

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_H-mm-ss",Locale.US);
String formattedDateString = dateFormat.format(new java.util.Date());
// result is something like "2013-02-14_18-44-15"
Run Code Online (Sandbox Code Playgroud)