zoukankan      html  css  js  c++  java
  • 把excel导入的自定义时间改成yyyyMMdd

    public static String changeCellToString(XSSFCell cell){
    String result = "";
    // Object value = null;
    DecimalFormat df = new DecimalFormat("#");
    if (null != cell) {
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:// 数字类型
    if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
    SimpleDateFormat sdf = null;
    if (cell.getCellStyle().getDataFormat() == HSSFDataFormat
    .getBuiltinFormat("h:mm")) {
    sdf = new SimpleDateFormat("HH:mm");
    } else {// 日期
    sdf = new SimpleDateFormat("yyyyMMdd");
    }
    Date date = cell.getDateCellValue();
    result = sdf.format(date);
    } else if (cell.getCellStyle().getDataFormat() == 58) {
    // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    double value = cell.getNumericCellValue();
    Date date = org.apache.poi.ss.usermodel.DateUtil
    .getJavaDate(value);
    result = sdf.format(date);
    } else {
    double value = cell.getNumericCellValue();
    CellStyle style = cell.getCellStyle();
    DecimalFormat format = new DecimalFormat();
    String temp = style.getDataFormatString();
    // 单元格设置成常规
    if (temp.equals("General")) {
    format.applyPattern("#");
    }
    result = format.format(value);
    }
    break;
    case HSSFCell.CELL_TYPE_STRING:// String类型
    result = cell.getRichStringCellValue().toString();
    break;
    case HSSFCell.CELL_TYPE_BLANK:
    result = "";
    default:
    result = "";
    break;
    }
    }
    return result;
    }
  • 相关阅读:
    FJUT3260
    Codeforces Round #387 (Div. 2)
    poj 1375
    试题 历届试题 蚂蚁感冒(模拟)
    丢手绢(尺取)
    「金」点石成金(dfs)
    小A买彩票(dp)
    不平行的直线
    最少交换次数
    第k小数(桶排序)
  • 原文地址:https://www.cnblogs.com/xlj227/p/6101454.html
Copyright © 2011-2022 走看看