Created
August 26, 2013 11:02
-
-
Save jakob85/6340324 to your computer and use it in GitHub Desktop.
To handle dates in correct format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import org.joda.time._ | |
import org.joda.time.format._ | |
import anorm._ | |
object AnormExtension { | |
val dateFormatGeneration: DateTimeFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmssSS"); | |
implicit def rowToDateTime: Column[DateTime] = Column.nonNull { (value, meta) => | |
val MetaDataItem(qualified, nullable, clazz) = meta | |
value match { | |
case ts: java.sql.Timestamp => Right(new DateTime(ts.getTime)) | |
case d: java.sql.Date => Right(new DateTime(d.getTime)) | |
case str: java.lang.String => Right(dateFormatGeneration.parseDateTime(str)) | |
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass) ) | |
} | |
} | |
implicit val dateTimeToStatement = new ToStatement[DateTime] { | |
def set(s: java.sql.PreparedStatement, index: Int, aValue: DateTime): Unit = { | |
s.setTimestamp(index, new java.sql.Timestamp(aValue.withMillisOfSecond(0).getMillis()) ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment