Java – how to use timezone to parse datetime without t or nanosecond

I am trying to parse the date time string in the following format:

2019-02-22 19:29:43+00:00

I am following this guide: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

This particular line seems to be the timestamp string I'm trying to parse:

Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

This is what I created and gives a 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 got this mistake:

java.time.format.DateTimeParseException: Text '2019-02-22 19:29:43+00:00' Could not be parsed,unparsed text found at index 22

Solution

Try yyyy MM DD HH: mm: ssxxx This should solve your problem

The following description from the table: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Edit:

If you want to format the date in this mode and want the same output string, you should use yyyy MM DD HH: mm: ssxxx (see Andreas comments below)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>