+0800
is not a time zone, it is an offset from UTC.
A DateTimeFormatter
distingishes between zone (ZoneId
) and offset (ZoneOffset
) and supports different pattern characters that's why it refuses to parse your text
…
I would parse a String
that contains an offset from UTC to an OffsetDateTime
instead of a LocalDateTime
, because the latter would strip off the information about the offset and simply keep a date and a time of day. But that obviously depends on the specific requirements…
That means you will have to introduce a different pattern if you want to parse this input String
with a DateTimeFormatter
.
You could use xxxx
instead of zzz
in that pattern (the rest stays the same, but better use uuuu
instead of yyyy
).
fun main() {
// input String
val text = "Tue, 07 Mar 2023 15:32:23 +0800"
// pattern for the SimpleDateFormat
val patternSdf = "EEE, dd MMM yyyy HH:mm:ss zzz"
// pattern for the DateTimeFormatter
val patternDtf = "EEE, dd MMM yyyy HH:mm:ss xxxx"
// get a java.util.Date by parsing the String with the SimpleDateFormat
val date = SimpleDateFormat(patternSdf, Locale.ENGLISH).parse(text)
// print the Date
println(date)
// get a LocalDateTime by parsing the String with the DateTimeFormatter
val dateTime = LocalDateTime.parse(
text,
DateTimeFormatter.ofPattern(patternDtf, Locale.ENGLISH)
)
// print it
println(dateTime);
// get an OffsetDateTime using the same approach
val offsetDateTime = OffsetDateTime.parse(
text,
DateTimeFormatter.ofPattern(patternDtf, Locale.ENGLISH)
)
// print it
println(offsetDateTime)
}
Output:
Tue Mar 07 07:32:23 UTC 2023
2023-03-07T15:32:23
2023-03-07T15:32:23+08:00
From JavaDocs 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'.
Offset Z:
This formats the offset based on the number of pattern letters. One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset text if the offset is zero. Five letters outputs the hour, minute, with optional second if non-zero, with colon. It outputs 'Z' if the offset is zero. Six or more letters throws IllegalArgumentException.