Scala/Lift check if date is correctly formatted

前端 未结 5 1141
半阙折子戏
半阙折子戏 2020-12-31 17:20

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 reg

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 17:53

    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
      }
    

提交回复
热议问题