我需要将日期格式化为特定的字符串.
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);
这篇关于Android 格式化日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
解析 ISO 8601 字符串本地日期时间,就像在 UTC 中Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期时间,就像在 UTC 中一样)
如何将公历字符串转换为公历?How to convert Gregorian string to Gregorian Calendar?(如何将公历字符串转换为公历?)
Java:GregorianCalendar 的最大值和最小值是什么/在哪Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
1582 年 10 月 15 日之前日期的日历到日期转换.公历Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日历到日期转换
java日历setFirstDayOfWeek不起作用java Calendar setFirstDayOfWeek not working(java日历setFirstDayOfWeek不起作用)
Java:获取当前星期几的值Java: getting current Day of the Week value(Java:获取当前星期几的值)