java 24 小时时钟在 dateformat android 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5755073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:36:17  来源:igfitidea点击:

24 hour clock not working in dateformat android

javaandroiddatetime

提问by user590849

i have made an application in which i need to perform date conversion in 24 hour format. Here is my code.

我制作了一个应用程序,我需要在其中执行 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);

now when i check my LOG here is the output:

现在,当我检查我的日志时,这里是输出:

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

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

why is the hour field not getting set. i have explicitly passed 0 when i was making the calendar object, still it is display HH in the LOG. i have tried while passing hh, that is being set, but HH is not being set.

为什么小时字段没有设置。我在制作日历对象时明确传递了 0,但它仍然在日志中显示 HH。我在通过 hh 时尝试过,正在设置,但未设置 HH。

what could be the problem?

可能是什么问题呢?

thank you in advance.

先感谢您。

回答by kp666

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

回答by rajath

Try using SimpleDateFormat, a subclass of DateFormat. That might correct the problems you're having.

尝试使用SimpleDateFormat,它是 DateFormat 的一个子类。这可能会纠正您遇到的问题。

e.g. to print current datetime

例如打印当前日期时间

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"

回答by Norfeldt

Try this

试试这个

Long someLongDate = 240000L 
// I was working with a Long in my own code. 
// I'm sure you can use something like varDate.getLong()
TextView textView = (TextView) view;                 // your view of interest
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");     
textView.setText(sdf.format(new Date(someLongDate)));

If it helped - don't forget to check my post as the answer..

如果有帮助 - 不要忘记检查我的帖子作为答案..