how to convert UTC date-time into ISO 8601 format in Java?

后端 未结 1 1253
感情败类
感情败类 2020-12-12 05:17

I want to format a UTC date-time in a specific ISO 8601 format like 2020-02-28T14:10:23+00:00 but not 2020-02-28T14:10:23Z. I don\'t want Z

相关标签:
1条回答
  • 2020-12-12 05:54

    According documentation of DateTimeFormatter

    Offset X and x: This formats the offset based on the number of pattern letters. One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'. Two letters outputs the hour and minute, without a colon, such as '+0130'. Three letters outputs the hour and minute, with a colon, such as '+01:30'. Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. Six or more letters throws IllegalArgumentException. Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.

    Try with xxx like in:

    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx").format(ZonedDateTime.now())
    

    which returns something like

    2020-02-28T12:42:30+00:00
    

    Based on Java version 13, tested with jshell OpenJDK 13; DateTimeFormatter is available in Java 8 or later

    0 讨论(0)
提交回复
热议问题