在Android中从日期开始的小时和分钟 [英] Hours and minutes from date in android
问题描述
我的java util日期为 2013年2月28日星期四15:35:00 GMT + 00:00 c
I have java util date as Thu Feb 28 15:35:00 GMT+00:00 2013
如何将其转换为 HH:MM
。
我正在尝试通过以下代码段进行此操作:
I am trying to do this via below snippet:
SimpleDateFormat formatter = new SimpleDateFormat("HH:MM");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
return formatter.format(objDate);
对于 dd mmm
给出正确的结果,但是对于 HH:MM
Its giving right result for dd mmm
but always giving minutes as 02 for HH:MM
总是给出分钟为02,请告诉我此函数出了什么问题?
Please suggest me what is wrong in this function?
推荐答案
MM
是<$>中的 month 值c $ c> SimpleDateFormat 。您要 mm
。请注意,使用 HH
将为您提供24小时制(00-23)...您可以将 hh
用于12小时制(01-12),但在这种情况下,您可能还需要am / pm指示符。
MM
is the month value in SimpleDateFormat
. You want mm
. Note that using HH
will give you the 24 hour clock (00-23)... you can use hh
for the 12 hour clock (01-12) but you probably also want the am/pm designator in that case.
请参见有关 SimpleDateFormat
的文档
See the documentation for SimpleDateFormat
for more details of the various format specifiers available.
还请注意,出于国际化的目的,您最好使用:
Also note that for internationalization purposes, you might be better off with:
DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
...,以便它将为给定的语言环境使用适当的短时间格式。如果这是出于显示(人类可读)的目的,则是适当的;对于计算机可读格式,可以创建自定义格式,但是我建议显式使用 Locale.US
以避免获取不同的日历和格式符号(例如时间分隔符)
... so that it will use the appropriate "short time format" for the given locale. That's appropriate if this is for display (human-readable) purposes; for computer-readable formats, it's fine to create a custom format but I'd recommend explicitly using Locale.US
to avoid getting different calendars and format symbols (e.g. the time separator).
这篇关于在Android中从日期开始的小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!