Android 格式化日期与时区 [英] Android Format date with time zone

查看:27
本文介绍了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" 但我需要 as
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.

因为我正在使用 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);

这篇关于Android 格式化日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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