Scala / Check the box if the date is formatted correctly

I have a date input window in my elevator application and I want to check that the date entered by the user is in the correct format: dd / mm / yyyy .

How can I write a regex check for this in scala? I looked at pattern matching examples, but it seems complicated.

PS: I do not need to use regex, any other options are welcome!

+5
source share
5 answers

SimpleDateFormatis ugly and (more disturbing) unrelated to the flow. If you try to use the same instance at the same time in 2 or more threads, then expect things to explode in the most unpleasant way.

JodaTime is much nicer:

import org.joda.time.format._
val fmt = DateTimeFormat forPattern "dd/MM/yyyy"
val input = "12/05/2009"
val output = fmt parseDateTime input

If he chose IllegalArgumentException, the date is invalid.

As I suspect, you will want to know the actual date, if it was valid, you can return Option[DateTime]with Noneif it was invalid.

def parseDate(input: String) = try {
  Some(fmt parseDateTime input)
} catch {
  case e: IllegalArgumentException => None
}

Alternatively, use Eitherto catch the actual exception if formatting is not possible:

def parseDate(input: String) = try {
  Right(fmt parseDateTime input)
} catch {
  case e: IllegalArgumentException => Left(e)
}

UPDATE

To use Either, you have two main tactics:

match one of two sides:

parseDate(input).left map (_.getMessage)
//will convert the Either[IllegalArgumentException, DateTime]
//to an Either[String, DateTime]

fold it:

parseDate(input) fold (
  _ => S.error(
    "birthdate",
    "Invalid date. Please enter date in the form dd/mm/yyyy."),
  dt => successFunc(dt)
)

Of course, they can be composed:

parseDate(input).left map (_.getMessage) fold (
  errMsg => S.error("birthdate", errMsg), //if failure (Left by convention)
  dt => successFunc(dt) //if success (Right by convention)
)
+10
source

, SimpleDateFormat ( , ).

, 28 30 , 38, , , .

val df = new java.text.SimpleDateFormat ("dd/MM/yyyy")

( , M , ).

:

scala> df.parse ("09/13/2001")                                
res255: java.util.Date = Wed Jan 09 00:00:00 CET 2002

hoppla - . :

scala> val sInput = "13/09/2001"
sInput: java.lang.String = 13/09/2001

scala> sInput.equals (df.format (df.parse (sInput))) 
res259: Boolean = true

scala> val sInput = "09/13/2001"                     
sInput: java.lang.String = 09/13/2001

scala> sInput.equals (df.format (df.parse (sInput))) 
res260: Boolean = false

, .

+1

, , , , .

SimpleDateFormat , . , setLenient(false) . , SimpleDateFormat , , :

def validate(date: String) = try {
    val df = new SimpleDateFormat("dd/MM/yyyy")
    df.setLenient(false)
    df.parse(date)
    true
  } catch {
    case e: ParseException => false
  }

Joda Time, , Java Date API, :

val format = DateTimeFormat.forPattern("dd/MM/yyyy")

def validate(date: String) = try {
    format.parseMillis(date)
    true
  }
  catch {
    case e: IllegalArgumentException => false
  }
+1

, "dd/MM/yyyy".

  try {  val format = DateTimeFormat.forPattern("dd/MM/yyyy")
  format.parseMillis(dateInString)
  val df = new SimpleDateFormat("dd/MM/yyyy")
  val newDate = df.parse(dateInString)
  true
    } catch {
      case e: ParseException => false

      case e: IllegalArgumentException => false
    }
+1

It is good practice to define an instance DateTimeFormatterin an object since it is 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
  }
0
source

All Articles