DjFenomen
DjFenomen

Reputation: 87

How to parse LocalDateTime string with special pattern?

I have String of time like: 2021-04-23T20:18:48.442826841Z How can I parse it to LocalDateTime? Which DateTimeFormatter I need to specify?

Upvotes: 1

Views: 122

Answers (2)

samabcde
samabcde

Reputation: 8114

To begin, please read What's the difference between Instant and LocalDateTime?.

Referring to What exactly does the T and Z mean in timestamp?, the given string represent an Instant, and suppose you want a LocalDateTime with system default timezone, you may parse as following.

LocalDateTime.ofInstant(Instant.parse("2021-04-23T20:18:48.442826841Z"), ZoneId.systemDefault());

Or with DateTimeFormatter.ISO_INSTANT

LocalDateTime.parse("2021-04-23T20:18:48.442826841Z", DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()))

Upvotes: 2

sproketboy
sproketboy

Reputation: 9452

LocalDateTime.parse("2021-04-23T20:18:48.442826841Z", DateTimeFormatter.ISO_LOCAL_DATE);

Works for me.

Upvotes: 0

Related Questions