SimpleDateFormat忽略“XXX”如果时区设置为“UTC”

编程入门 行业动态 更新时间:2024-10-10 10:28:21
本文介绍了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”

本文发布于:2023-11-23 22:21:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   时区   SimpleDateFormat   XXX   UTC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!