Android 格式化日期与时区
我需要将日期格式化为特定的字符串.
I need to format the date into a specific string.
我使用 SimpleDateFormat
类使用模式yyyy-MM-dd'T'HH:mm:ssZ
"来格式化日期它返回当前日期为
"2013-01-04T15:51:45+0530
" 但我需要 as2013-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.
因为我正在使用 Google 日历插入事件,所以它只接受我提到的所需格式.
Because I'm working on Google calendar to insert an event, it accepts only the required format which I have mentioned.
推荐答案
你可以使用 Joda Time反而.它的 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);
相关文章