javadatedatetimetimezoneutc

SimpleDateFormat ignores "XXX" if timezone is set to "UTC"


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?


Solution

  • 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: