File sourceFile = new File(sourceUrl);
Sheet sourceSheet = WorkbookFactory.create(sourceFile).getSheetAt(0);
Row row= sourceSheet.getRow(1);
Cell createCell = row.getCell(2);
Cell lastCell = row.getCell(4);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dateFormatNew = new SimpleDateFormat("hh:mm:ss");
String createTimeString = dateFormat.format(createCell.getDateCellValue());
String lastTimeString = dateFormat.format(lastCell.getDateCellValue());
LocalDateTime createTime = TimeUtils.parse(createTimeString);
LocalDateTime lastTime = TimeUtils.parse(lastTimeString);
System.out.println(createTime);
System.out.println(dateFormatNew.format(createCell.getDateCellValue()));
System.out.println(lastTime);
System.out.println(dateFormatNew.format(lastCell.getDateCellValue()));
Period period = Period.between(createTime.toLocalDate(),lastTime.toLocalDate());
System.out.println(period.getDays());
注释:source是excel表格路径 TimeUtils 是String转LocalDateTime的工具类,自己编写的。 可自己实现
createCell 是时间格式 如: 2018/8/1 4:12:55
lastCell 是时间格式 如: 2018/8/1 4:12:55
时间格式转换 只需关注上面带下划线的代码即可。