Java 使用poi检查excel数据的正确性
我正在使用POI阅读excel表格,POI有一个日期列。我想检查日期的正确性,例如,如果excel中输入的日期是2015年5月45日。现在没有一个月有45天。我想知道是否有任何内置的方法可用 这没有打印无效日期Java 使用poi检查excel数据的正确性,java,apache-poi,Java,Apache Poi,我正在使用POI阅读excel表格,POI有一个日期列。我想检查日期的正确性,例如,如果excel中输入的日期是2015年5月45日。现在没有一个月有45天。我想知道是否有任何内置的方法可用 这没有打印无效日期 if(DateUtil.isCellDateFormatted(cell)) { System.out.println("cell value-->"+cell.getDateCellValue()); } else{ System.out.println("
if(DateUtil.isCellDateFormatted(cell))
{
System.out.println("cell value-->"+cell.getDateCellValue());
}
else{
System.out.println("invalid date");
}
此代码将日期从2015年6月45日转换为2015年6月30日
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YYYY");
String s = sdf.format(cell.getDateCellValue());
System.out.println(s);
我不想要这样的转换。你可以
SimpleDateFormat
class
//String date = "05/45/2015";
String date = DateFormatter.formatCellValue(cell);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
Date d= df.parse(date);
//if you didnt get any exception then date is in correct format
}catch (Exception e) {
//date not in correct format
return false;
}
您可以
SimpleDateFormat
class
//String date = "05/45/2015";
String date = DateFormatter.formatCellValue(cell);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
try {
Date d= df.parse(date);
//if you didnt get any exception then date is in correct format
}catch (Exception e) {
//date not in correct format
return false;
}
这可能对你有帮助。您可以尝试-这将返回一个布尔值,告诉您单元格的格式字符串是否表示数据。这可能会对您有所帮助。您可以尝试-这将返回一个布尔值,告诉您单元格的格式字符串是否表示数据。我需要将cell.getDateCellValue()传递到日期,这是不可能的,因为日期变量的类型为string。如果日期格式正确,我打算执行以下操作:“SimpleDataFormat sdf=新SimpleDataFormat(“MM/dd/YYYY”);字符串s=sdf.format(cell.getDateCellValue());系统输出打印项次(s);'您可以使用DateFormatter.formatCellValue(cell)返回单元格的字符串值。它说DateFormatter的formatCellValue方法未定义您正在使用的apache poi的哪个版本…它的错误…我需要将cell.getDateCellValue()传递到日期,这是不可能的,因为日期变量的类型为string。如果日期格式正确,我打算执行以下操作:“SimpleDataFormat sdf=新SimpleDataFormat(“MM/dd/YYYY”);字符串s=sdf.format(cell.getDateCellValue());系统输出打印项次(s);'您可以使用DateFormatter.formatCellValue(单元格)返回单元格的字符串值。它说formatCellValue方法对于DateFormatter是未定义的,您使用的apache poi版本是…,它的错误…,即使日期是2015年6月45日,它也会为真,我不需要它,即使日期是2015年6月45日,它也会为真,如果您在Excel中键入其中一个无效日期,然后尝试将单元格格式设置为数字而不是日期,excel会为单元格显示什么?如果在excel中键入其中一个无效日期,则尝试将单元格格式设置为数字而不是日期,excel会为单元格显示什么?