def getTimeStamp(y:Int,m:Int,h:Int) = {
require(m<=12,"Invalid Data")
def leapYr(y:Int) = if(y%100==0) y%400==0 else y%4==0
val monthDays = Array((1,31),(2,28),(3,31),(4,30),(5,31),(6,30),
(7,31),(8,31),(9,30),(10,31),(11,30),(12,31))
val monthDaysLeap = Array((1,31),(2,29),(3,31),(4,30),(5,31),(6,30),
(7,31),(8,31),(9,30),(10,31),(11,30),(12,31))
val days = if(!leapYr(y)){ monthDays(m-1)._2} else { monthDaysLeap(m-1)._2 }
val hoursInMonth = days*24
if(h<=hoursInMonth){ val d:Double = h/24.0; val d1=d.toInt;
val hr = (24*(d-d1)).toInt; y+"-"+m+"-"+d1+" "+hr+":"+"00"
}
else "Invalid Data"
}
Тест:
scala> getTimeStamp(2017,11,570)
res36: String = 2017-11-23 18:00