DEV Community

Cover image for How to get date or datetime from string in java
Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.land

3

How to get date or datetime from string in java

To get the date or dateime from String since Java 8 you can use the parse method of both LocalDate and LocalDateTime which takes two parameters: the string and a DateFormatter which you can specify as in the following example:

LocalDate date = LocalDate.parse("1969-12-31", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDateTime dateTime = LocalDateTime.parse("1983-01-08 20:54", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever. 👉   use the copy to mine functionality to add it to your personal snippets collection.

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay