Skip to content

Instantly share code, notes, and snippets.

@chidakiyo
Created January 6, 2015 08:27
Show Gist options
  • Save chidakiyo/8952e08c449573523c78 to your computer and use it in GitHub Desktop.
Save chidakiyo/8952e08c449573523c78 to your computer and use it in GitHub Desktop.
Reverseしても日付になる4桁の数値(社内研修用)
import java.text._
import java.util.Date
object HelloWorld extends App {
val d = { val d = new SimpleDateFormat("yyyyMMdd"); d.setLenient(false); d }
def fmt = (x: Int) => { "%04d".format(x) }
def parse(in: String): Option[Date] = try {
Some(d.parse(s"2000$in"))
} catch {
case e: Exception => None
}
def compParse(num: String): Option[Date] = {
(parse(num), parse(num.reverse)) match {
case (Some(a), Some(b)) => Some(a)
case _ => None
}
}
Range(0, 9999).map(fmt(_)).filter(compParse(_).nonEmpty).foreach(println(_))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment