Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala/Lift check if date is correctly formatted

Tags:

scala

lift

I have a date input box in my lift application, and I want to check that a user-entered date is in correct format: dd/mm/yyyy.

How can I write a regex check for this in scala? I've looked at pattern matching examples - but that seems over-complicated.

PS: I don't have to use regex, any other alternatives are welcome!

like image 874
Andriy Drozdyuk Avatar asked May 12 '11 18:05

Andriy Drozdyuk


1 Answers

It's a good practice to define the DateTimeFormatter instance in an object since it's thread-safe and immutable.

  object DateOfBirth{
    import org.joda.time.format.DateTimeFormat
    import scala.util.Try
    val fmt = DateTimeFormat forPattern "MM/dd/yyyy"
    def validate(date: String) = Try(fmt.parseDateTime(date)).isSuccess
  }
like image 56
Ben Aiad Avatar answered Sep 19 '22 11:09

Ben Aiad