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" should now contain "2016-01-11T14:08:42+00:00" but it contains "2016-01-11T14:08:42Z".
Without the "UTC" timezone setting I get the right format but - of course - in my specific timezone...
Any ideas?
See the documentation for SimpleDateFormat:
For formatting [using an ISO 8601 Time zone], if the offset value from GMT is 0, "Z" is produced.
So, this behaviour is expected.
You can either:
- Use the RFC 822 timezone formatter
ZZZ
; however, this produces "+0000" - Manipulate the string to replace the final
Z
:str.replaceAll("Z$", "+00:00")
来源:https://stackoverflow.com/questions/34723683/simpledateformat-ignores-xxx-if-timezone-is-set-to-utc