我是Scala的新手,我一直在尝试将JSON字符串解析为Timestamps(long类型)。我一直得到以下错误“单位不符合预期类型长”,我还没有得到线索为什么
import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.Date
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
var timestamp: Timestamp = new Timestamp(0)
var parsedDate: Date = new Date()
var dateString: String = ""
var timestampLong: Long = 0
def extractAscendingTimestamp(element: String): Long = {
implicit val formats = org.json4s.DefaultFormats
dateString = parse(element).extract[SheetFedRecord].begin
try {
parsedDate = dateFormat.parse(dateString)
timestamp = new Timestamp(parsedDate.getTime)
timestampLong = timestamp.getTime.asInstanceOf[Long]
}
catch {
case e: Exception => 0.asInstanceOf[Long]
}
}
在try和catch中我都给方法一个“long”变量来返回。有人可以帮忙吗?
答案 0 :(得分:1)
你尝试捕获的最后一句话应该是
timestamp.getTime.asInstanceOf[Long]
而不是
timestampLong = timestamp.getTime.asInstanceOf[Long]
如果您仍想修改timestampLong变量,那么尝试catch的最后一个语句应该变为以下两行:
timestampLong = timestamp.getTime.asInstanceOf[Long]
timestampLong