• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing Strings to LocalDate in Java8 using DateTimeFormatter

 
Kndarp Patel
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im using LocalDate and DateTimeFormatter to parse dates from String values in Java8 . However, some dates are of MM/dd/yyyy, while others are like M/d/yyyy or M/dd/yyyy or MM/d/yyyy. How can i resolve this?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can simple use this pattern to parse the Strings:
This way the formatter will handle the dates correctly, no matter if the month or day are 2 digits or only 1 digit.

Marco
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!