"scala/lift check if date is correctly formatted" Code Answer
2
simpledateformat is ugly and (more disturbingly) non-thread-safe. if you try to simultaneously use the same instance in 2 or more threads then expect things to blow up in a most unpleasant fashion.
jodatime is far nicer:
import org.joda.time.format._
val fmt = datetimeformat forpattern "dd/mm/yyyy"
val input = "12/05/2009"
val output = fmt parsedatetime input
if it throws an illegalargumentexception, then the date wasn't valid.
as i suspect you'll want to know the actual date if it was valid, you may want to return an option[datetime], with none if it was invalid.
simpledateformat
is ugly and (more disturbingly) non-thread-safe. if you try to simultaneously use the same instance in 2 or more threads then expect things to blow up in a most unpleasant fashion.jodatime is far nicer:
if it throws an
illegalargumentexception
, then the date wasn't valid.as i suspect you'll want to know the actual date if it was valid, you may want to return an
option[datetime]
, withnone
if it was invalid.alternatively, use an
either
to capture the actual exception if formatting wasn't possible:update
to then use the
either
, you have two main tactics:map one of the two sides:
fold it:
of course, the two can be composed: