SimpleDateFormat忽略“XXX”如果时区设置为“UTC” [英] SimpleDateFormat ignores "XXX" if timezone is set to "UTC"

查看:469
本文介绍了SimpleDateFormat忽略“XXX”如果时区设置为“UTC”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下列格式输出UTC的当前日期时间:
2016-01-11T14:08:42 + 00:00

I am trying to output the current datetime as UTC in the following format: 2016-01-11T14:08:42+00:00

final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

final String dateString = formatter.format(new Date());

dateString现在应包含2016-01-11T14:08:42 + 00:00但是它包含2016-01-11T14:08:42Z。

"dateString" should now contain "2016-01-11T14:08:42+00:00" but it contains "2016-01-11T14:08:42Z".

没有UTC时区设置,我得到正确的格式,但当然 - 在我的具体时区...

Without the "UTC" timezone setting I get the right format but - of course - in my specific timezone...

任何想法?

推荐答案

SimpleDateFormat


对于格式化[使用ISO 8601时区],如果GMT的偏移值为0,则会生成Z。

For formatting [using an ISO 8601 Time zone], if the offset value from GMT is 0, "Z" is produced.

所以,这个行为是预期的。

So, this behaviour is expected.

你可以:


  • 使用RFC 822时区格式化程序 ZZZ ;然而,这会产生+0000

  • 操纵字符串以替换最终的 Z str.replaceAll (Z $,+00:00)

  • Use the RFC 822 timezone formatter ZZZ; however, this produces "+0000"
  • Manipulate the string to replace the final Z: str.replaceAll("Z$", "+00:00")

这篇关于SimpleDateFormat忽略“XXX”如果时区设置为“UTC”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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