Android 格式化日期与时区

Android Format date with time zone(Android 格式化日期与时区)
本文介绍了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 格式化日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)