Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created January 24, 2013 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rirakkumya/4619788 to your computer and use it in GitHub Desktop.
Save rirakkumya/4619788 to your computer and use it in GitHub Desktop.
いつものやつ
def toDate[A:DateMagnet](x:A) = implicitly[DateMagnet[A]].convert(x)
trait DateMagnet[A]{
type Result
def convert(x:A):Result
}
object DateMagnet {
implicit def fromInt = new DateMagnet[(Int, Int, Int)] {
type Result = java.util.Calendar
def convert(tuple:(Int, Int, Int)):Result = {
val (year, month, date) = tuple
val c = java.util.Calendar.getInstance
c.set(year, month - 1, date, 0, 0, 0)
c
}
}
implicit def fromString = new DateMagnet[String] {
type Result = java.util.Date
def convert(s:String):Result =
(new java.text.SimpleDateFormat("yyyy/MM/dd")).parse(s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment