Created
January 6, 2015 08:27
-
-
Save chidakiyo/8952e08c449573523c78 to your computer and use it in GitHub Desktop.
Reverseしても日付になる4桁の数値(社内研修用)
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
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