case class Widget( id : Int, name : String, created : DateTime = now)val widgets = int("id") ~ str("name") ~ date("created") map { case id~name~created => Widget(id,name,asDateTime(created)) }[RuntimeException: TypeDoesNotMatch(Cannot convert 2014-02-08T21:15:31.941-05:00:class java.lang.String to Date for column ColumnName(widget.created,Some(created)))]
CREATE TABLE widget (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL, created DATETIME NOT NULL);val widgets = int("id") ~ str("name") ~ str("created") map { case id~name~created => Widget(id,name,new DateTime(created)) } /**
* Convert a column value to a joda.time DateTime.
* http://stackoverflow.com/questions/11388301/joda-datetime-field-on-play-framework-2-0s-anorm has
* a bit more code that may be valuable for writing DateTime values to the database.
*/
implicit def rowToDate: Column[DateTime] = Column.nonNull { (value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
case d: java.sql.Date => Right(new DateTime(d.getTime()))
case ts: java.sql.Timestamp => Right(new DateTime(ts.getTime()))
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to DateTime for column " + qualified))
}
}