I don't think there is a built-in, but you can make your own using the DateTimeFormatterBuilder
class.
You can specify an additional offset enclosed in square brackets, i.e. [XXXXX]
(to match "+HH:MM:ss"
). You can then provide a default offset ( parseDefaulting
) in case it is missing. If you want to set UTC by default, you can set 0 to not indicate an offset; and if you want to use the current virtual machine offset by default, you can get it using OffsetDateTime.now().getLong(ChronoField.OFFSET_SECONDS)
.
public static void main(String[] args) { String[] dates = { "2007-12-03T10:15:30+01:00", "2007-12-03T10:15:30Z", "2016-03-02T17:09:55", "2016-03-02T17:09:55Z" }; DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ss[XXXXX]") .parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
source share