I'm trying to parse a datetime string in the following format:
2019-02-22 19:29:43+00:00
I'm following this guide: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
This particular row seems to be the timestamp string that I'm trying to parse for:
Z zone-offset offset-Z +0000; -0800; -08:00;
Here is what I created, given that guide:
String input = "2019-02-22 19:29:43+00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX");
LocalDateTime parsed = LocalDateTime.parse(input, formatter);
But I get this error:
java.time.format.DateTimeParseException: Text '2019-02-22 19:29:43+00:00' could not be parsed, unparsed text found at index 22
Try yyyy-MM-dd HH:mm:ssXXX
. This should solve your problem.
Offset X and x:
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.
From the description below the table: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
EDIT:
If you want to format dates with that pattern and want the same output string you should use yyyy-MM-dd HH:mm:ssxxx
instead (see Andreas' comment below).