Android的格式日期时区 [英] Android Format date with time zone
问题描述
我需要的日期格式为一个特定的字符串。
I need to format the date into a specific string.
我用的SimpleDateFormat
类使用模式 YYYY-MM-dd'T'HH格式化日期:MM:SSZ
返回当前日期作为结果 2013-01-04T15:51:45 + 0530
但我需要的结果 2013-01-04T15:51:45 + 05:30
I used SimpleDateFormat
class to format the date using the pattern "yyyy-MM-dd'T'HH:mm:ssZ
" it returns current date as
"2013-01-04T15:51:45+0530
" but I need as
"2013-01-04T15:51:45+05:30
".
下面是使用的编码,
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
Log.e(C.TAG, "formatted string: "+sdf.format(c.getTime()));
输出:格式化字符串: 2013-01-04T15:51:45 + 0530
我需要的格式为 2013-01-04T15:51:45 + 05:30
只需添加结肠GMT时间之间
I need the format as 2013-01-04T15:51:45+05:30
just adding the colon in between gmt time.
由于我工作的谷歌日历中插入事件时,它仅接受我刚才所说的需要的格式。
Because I'm working on Google calendar to insert an event, it accepts only the required format which I have mentioned.
推荐答案
您可以使用乔达时间代替。它的 DateTimeFormat
有一个 ZZ
格式的属性,它你想要做什么。
You can use Joda Time instead. Its DateTimeFormat
has a ZZ
format attribute which does what you want.
大优势:不像的SimpleDateFormat
, DateTimeFormatter
是线程安全的。用法:
Big advantage: unlike SimpleDateFormat
, DateTimeFormatter
is thread safe. Usage:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ")
.withLocale(Locale.ENGLISH);
这篇关于Android的格式日期时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!