前言
最近在对excel表格数据进行操作过程中,遇到读取日期为数字的问题,这个给大家分享出来我的解决方法,希望可以帮助到一些小伙伴们!
Exception in thread “main” java.lang.NumberFormatException: For input string: “4472.0”
下面看具体代码解决方法:
// 读取excel的日期 , 得到time1的值为:4472.0
String time1 = String.valueOf(row.getCell(0).getNumericCellValue());
// 从后面截取.0 , 得到结果为:4472
String time = time1.substring(0,time1.length()-2);
在这里插入代码片
Calendar calendar = new GregorianCalendar(1900,0,-1);
Date d = calendar.getTime();
if (null != time){
String constant = StringUtils.isBlank(time.toString()) ? "" : time.toString().trim();
if (!"".equals(time)){
Date dd = DateUtils.addDays(d,Integer.valueOf(constant));
incomeModel.setPaymentTime(dd); //付款日期
}
}
数据库结果:
总结
读取日期出来的是4472.0,因而Integer.valueOf(constant)无法将4472.0进行Integer转换