JAVA导入Excel后,时间变为数字,转日期方法

您所在的位置:网站首页 日期格式变成数字转换不回来 JAVA导入Excel后,时间变为数字,转日期方法

JAVA导入Excel后,时间变为数字,转日期方法

2024-07-05 18:41| 来源: 网络整理| 查看: 265

一、先说一下通常的Excel 导入: Excel 导入后,获取时间数据:

private static String getCellStringVal(Cell cell, String format) { CellType cellType = cell.getCellTypeEnum(); switch (cellType) { case NUMERIC: String value; if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); value = TimeTool.dateToFormatTime(date, format); } else { double dValue = cell.getNumericCellValue(); DecimalFormat df = new DecimalFormat("0"); value = df.format(dValue); } return value; case STRING: return cell.getStringCellValue(); case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); case BLANK: return ""; case ERROR: return String.valueOf(cell.getErrorCellValue()); default: return ""; } }

二、如果说我们使用的不是poi,或者其他。导入后日期为数字: getTime();// ditNumber = 43607.4166666667

这里,我们需要保存时间戳到数据库,所以这里做了判断,且返回String。 Date、Timestamp已有,需要直接返回即可。 //Mysql支持的时间戳限制 static long minTime = Timestamp.valueOf("1970-01-01 09:00:00").getTime(); static long maxTime = Timestamp.valueOf("2038-01-19 11:00:00").getTime(); static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //判断 并转换时间格式 ditNumber = 43607.4166666667 public static String getTime(String ditNumber) { //如果是数字 小于0则 返回 BigDecimal bd = new BigDecimal(ditNumber); int days = bd.intValue();//天数 int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600); //获取时间 Calendar c = Calendar.getInstance(); c.set(1900, 0, 1); c.add(Calendar.DATE, days - 2); int hour = mills / 3600; int minute = (mills - hour * 3600) / 60; int second = mills - hour * 3600 - minute * 60; c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); Date d = c.getTime();//Date return DateUtil.formatDateTime(d); } //校验是否数据含小数点 private static boolean isNotNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]+\\.*[0-9]*"); Matcher isNum = pattern.matcher(str); if(!isNum.matches()){ return false; } return true; }

继上,给大家提供校验日期格式的方法:

static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //传入String,返回boolean public static boolean checkDate(String date) { if (date != null && date.isEmpty()) { try { //禁止SimpleDateFormat的自动计算功能,严格解析日期 dateFormat.setLenient(false); dateFormat.parse(date); } catch (Exception e) { System.out.println("传入日期错误" + date); return false; } return true; } return false; } //传入java.util.Date,返回boolean public static boolean checkDate(Date date) { if (date != null) { try { //禁止SimpleDateFormat的自动计算功能,严格解析日期 dateFormat.setLenient(false); dateFormat.format(date); } catch (Exception e) { System.out.println("传入日期错误" + date); return false; } return true; } return false; }

手动操作导入

public void updateOrderExpress(MultipartFile file) throws Exception { Workbook wb = new XSSFWorkbook(file.getInputStream()); //根据页面index 获取sheet页 Sheet sheet = wb.getSheetAt(0); Row row = null; int rowNum = 0; Iterator rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { rowNum+=1; String errorMes = ""; //获取每一行数据 row = rowIterator.next(); if (rowNum == 1) { continue; } if (ObjectUtil.isEmpty(row)) { continue; } for (int j = 1; j


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3