I have the following simple code that leads to a RuntimeException error that I can't explain:
import java.text.MessageFormat
import java.util.Date
import org.apache.commons.cli._
import org.joda.time._
import org.joda.time.format.DateTimeFormat
object TestMain {
def doFormat(value: Any): String = {
val ClassOfDouble = classOf[Double]
val ClassOfDate = classOf[Date]
val ClassOfDateTime = classOf[DateTime]
val result: String = value.getClass match {
case ClassOfDouble => MessageFormat.format("{0,number,#.####################}", Array(value.asInstanceOf[DateTime]))
case ClassOfDate => MessageFormat.format("{0,date,yyyy.MM.dd}", Array(value.asInstanceOf[Date]))
case ClassOfDateTime => DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:SSS").print(value.asInstanceOf[DateTime])
case _ => value.toString
}
result
}
def main(args: Array[String]): Unit = {
println(doFormat(new Date()))
}
}
... and the resulting runtime error:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at java.text.MessageFormat.subformat(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at test.TestMain$.doFormat(TestMain.scala:28)
at test.TestMain$.main(TestMain.scala:39)
at test.TestMain.main(TestMain.scala)