在 Scala 中验证数据框中的日期列?

Validating a date column in a dataframe in scala?

我正在使用 spark 从 hbase 读取数据,我在数据框中有日期列,很少有数据字段 corrupted.something 像 10-20176-7 等。我如何检查这些并替换在我进一步处理之前使用一些默认值。

谢谢。

我堆栈跟踪错误,下面是错误。

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '20140218' could not be parsed: 
Unable to obtain LocalDateTime from TemporalAccessor: 
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)

所以我使用 LocalDate 而不是 LocalDateTime 来解决问题。下面是使用的示例代码。

def validateDfsdate(row: Row): Boolean = try {

val a = java.time.LocalDate.parse(row.getString(40), java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT))

true

} catch {
case ex: java.time.format.DateTimeParseException => {
  println("Exception : " + ex)
  false
}

}