android24hourformat,24 hour clock not working in dateformat android

问题

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

Here is my code.

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

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.

what could be the problem?

thank you in advance.

回答1:

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

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"

回答2:

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

回答3:

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

来源:https://stackoverflow.com/questions/5755073/24-hour-clock-not-working-in-dateformat-android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值