显示24小时内的日期 [英] Display Date in 24-hour issue

查看:209
本文介绍了显示24小时内的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在工作简单的应用程序,我可以得到当前的 DateTime 并将其转换为 24小时格式。 / p>

代码:



  String DATE_yyyy_MM_dd_hh_mm_ss =yyyy-MM-dd hh :mm:ss; 

String DATE_yyyy_MM_dd_HH_mm_ss =yyyy-MM-dd HH:mm:ss;


TextView tv =(TextView)findViewById(R.id.textView);

tv.append(\\\
12小时格式:+ getDateFormatted(bootDate));

tv.append(\\\
24小时格式:+ getDateFormatted2(bootDate));

public String getDateFormatted(Date date){
return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_hh_mm_ss,date));
}

public String getDateFormatted2(Date date){
return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_HH_mm_ss,date));
}



问题:



它在我的设备上完美工作 Samsung Galaxy S3(Android 4.3),S4(Android 4.3)和Nexus 5(Android 4.4)。而在华为Ascend Y330设备(Android 4.2.2)中运行的代码不正确。



Samsung Galaxy S3,S4和Nexus 5中的屏幕截图:





华为Ascend Y330的屏幕截图:





<那么,实际上是什么问题?我不明白是android系统问题吗?或设备问题?



任何想法任何人。

解决方案

所以< a href =http://stackoverflow.com/questions/5755073/24-hour-clock-not-working-in-dateformat-android?answertab=active#tab-top>这篇文章建议使用 SimpleDateFormat ,而不是DateFormat。有关这两个类别之间差异的更多解释,您可以查看这篇文章


I am working simple app where I can get current DateTime and convert that into 24-hour format.

Code:

  String DATE_yyyy_MM_dd_hh_mm_ss = "yyyy-MM-dd hh:mm:ss";

  String DATE_yyyy_MM_dd_HH_mm_ss  = "yyyy-MM-dd HH:mm:ss";  


  TextView tv=(TextView)findViewById(R.id.textView);

  tv.append("\n in 12-hour format: "+getDateFormatted(bootDate));

  tv.append("\n in 24-hour format: "+getDateFormatted2(bootDate));

  public String getDateFormatted(Date date){
    return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_hh_mm_ss, date));
}

public String getDateFormatted2(Date date){
    return String.valueOf(DateFormat.format(DATE_yyyy_MM_dd_HH_mm_ss, date));
}

Problem:

It's perfectly working in my devices Samsung Galaxy S3(Android 4.3), S4(Android 4.3) and Nexus 5(Android 4.4). while the same code I am running in Huawei Ascend Y330 device(Android 4.2.2) it's not display properly.

Screenshot in Samsung Galaxy S3, S4 and Nexus 5:

Screenshot in Huawei Ascend Y330:

So, what is the problem actually? I don't understand. Is it android system issue? or device issue?

Any idea anyone.

解决方案

So this post suggested to use SimpleDateFormat instead of DateFormat. For more explanation about the differences between these two classes, you could take a look at this post.

这篇关于显示24小时内的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆