java.text.ParseException: Unparseable date: ""
我收到上述错误,因为我从excel文件输入一些空值,我正在复制到另一个excel工作簿。对于空字段,我不想要任何输出。 那么,如何继续前进。任何帮助都将受到高度赞赏。
public class excel_read {
public static void main(String[] args) throws IOException, ParseException, RowsExceededException, WriteException
{
File fExcel = new File("C:\\Users\\Master\\Desktop\\fwdautomationforsmb\\new.xls");
WritableWorkbook writableBook = Workbook.createWorkbook(fExcel);
writableBook.createSheet("Data", 0);
WritableSheet writableSheet = writableBook.getSheet(0);
Xls_Reader datatable = new Xls_Reader("C:\\Users\\Master\\Desktop\\fwdautomationforsmb\\Workbook1.xlsx");
int rows_count = datatable.getRowCount("Sheet1");
System.out.println("Total number of rows = " + rows_count);
for(int i=2; i<rows_count;i++)
{
String date_list = datatable.getCellData("Sheet1", "Past/Nearing Expiration Date", i);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, 30);
String output = dateFormat.format(c.getTime());
String dateInString1 = date_list;
Date date1 = dateFormat.parse(dateInString1);
String dateInString2 = output;
Date date2 = dateFormat.parse(dateInString2);
if(date1.compareTo(date2)>0){
// System.out.println("Date1 is after Date2");
}
else if(date1.compareTo(date2)<0){
System.out.println(date_list);
Label data1 = new Label(0, i, date_list);
writableSheet.addCell(data1);
}
else if(date1.compareTo(date2)==0){
System.out.println(date_list);
Label data1 = new Label(0, i, date_list);
writableSheet.addCell(data1);
}
}
writableBook.write();
writableBook.close();
}
}
答案 0 :(得分:1)
在调用解析方法之前,您需要检查空字符串:
if(dateInString1 != null && !dateInString1.isEmpty()) {
Date date1 = dateFormat.parse(dateInString1);
}
如果您确定该字段不为空,则可以省略空检查。
答案 1 :(得分:0)
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, 30);
String output = dateFormat.format(c.getTime());
String dateInString2 = output;
Date date2 = dateFormat.parse(dateInString2);
你不觉得date2 == c.getTime(); ????