fork(1) download
  1. import java.time._
  2. import java.time.format._
  3. import scala.util.Try
  4.  
  5. object Main extends App {
  6. val formats = Seq("yyyy-MM-dd", "dd-MM-yyyy").map(DateTimeFormatter.ofPattern)
  7. val strings = Seq("17-10-1984", "1987-03-23")
  8.  
  9. val dates = strings.flatMap { date ⇒
  10. formats
  11. .flatMap(f ⇒ Try(LocalDate.parse(date, f)).toOption)
  12. .headOption
  13. }
  14.  
  15. println(dates)
  16. }
Success #stdin #stdout 0.52s 322176KB
stdin
Standard input is empty
stdout
List(1984-10-17, 1987-03-23)