我正在尝试读取具有日期列的Excel文件。我想验证它,但是在验证日期类型字段时,此方法不起作用。我尝试使用文本文件,并且可以正常工作。根据我的观察,Excel文件默认将日期值保存为日期类型而不是字符串。但我试图解析(字符串类型)。但是我不知道如何将Excel列读为字符串。
我试图读取它以直接读取getCell值,但它对我不起作用。
java.time.LocalDate.parse(row.getCell(2) , java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT))
val DATE_TIME_FORMAT = "MM/dd/yyyy"
def validateDf(row: Row): Boolean = try {
//assume row.getString(1) with give Datetime string
java.time.LocalDate.parse(row.getString(2) , java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT))
java.time.LocalDate.parse(row.getString(3) , java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT))
true
} catch {
case ex: java.time.format.DateTimeParseException => {
// Handle exception if you want
println("Exception : " + ex)
false
}
}
val validDf = sample1.filter(validateDf(_))
val inValidDf = sample1.except(validDf)
Input Excel Data
-------+-----+-----------+-------------+
|AirName|Place|TakeoffDate|arriveoffDate|
+-------+-----+-----------+-------------+
| Delta| Aus| 12/6/2015| 11/6/2015|
| Delta| | 12/6/2016| 01/6/2016|
| Vistra| New| 11/6/2017| 04/6/2017|
| Delta| Aus| 10/6/2018| 04/6/2018|
| JetAir| Aus| 14/6/2019| 04/6/2019|
+-------+-----+-----------+-------------+
Expected valid Data(Based on Valid and Invalid date)
-------+-----+-----------+-------------+
|AirName|Place|TakeoffDate|arriveoffDate|
+-------+-----+-----------+-------------+
| Delta| Aus| 12/6/2015| 11/6/2015|
| Delta| | 12/6/2016| 01/6/2016|
| Vistra| New| 11/6/2017| 04/6/2017|
| Delta| Aus| 10/6/2018| 04/6/2018|
+-------+-----+-----------+-------------+
Expected Invalid Data
-------+-----+-----------+-------------+
|AirName|Place|TakeoffDate|arriveoffDate|
+-------+-----+-----------+-------------+
| Delta| Aus| 14/6/2019| 04/6/2018|
+-------+-----+-----------+-------------+
```
答案 0 :(得分:1)
我建议使用apache poi从excel文件中读取数据。 您可以这样实现:
val DATE_TIME_FORMAT = "MM/dd/yyyy"
val currentCell = row.getString(1)
if (DateUtil.isCellDateFormatted(currentCell)) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT)
val cellValue = sdf.format(currentCell.getDateCellValue())
} catch (ParseException e) {
e.printStackTrace()
}
}