【问题标题】:How to convert the date format from 04-May-2016 to 04/05/2016 using Java Apache POI如何使用 Java Apache POI 将日期格式从 04-May-2016 转换为 04/05/2016
【发布时间】:2016-08-29 12:04:02
【问题描述】:

我读取了一个 .xlsx 文件并检索了一个日期值,它应该被输入到一个文本框字段中。

在 excel 中,单元格值为 04/05/2016

但在从单元格中获取日期值时,该值是 04-May-2016

如何将此 2016 年 5 月 4 日格式转换为 mm/dd/yyy

代码:

public static String readExcel(String filePath,String fileName,String sheetName,int RowNumber,int ColNumber) throws Exception{

    Object result = null;
    try
    {

        sheet=getSheet(filePath, fileName, sheetName);
        row=sheet.getRow(RowNumber);

        if(row != null)
        {
            //System.out.println("Row is not empty");
            cell= row.getCell(ColNumber);

            if(cell!=null)
            {
                switch (cell.getCellType()) {

                case Cell.CELL_TYPE_NUMERIC:// numeric value in excel
                    if(DateUtil.isCellDateFormatted(cell)){
                        //DateUtil.truncate(new Date(), java.util.Calendar.DAY_OF_MONTH);

                        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                        result = formatter.format(cell);
                        System.out.println("Today : " + result);    
                    }
                    else{
                        result = new BigDecimal(cell.getNumericCellValue()).toPlainString();
                    }
                    break;

                case Cell.CELL_TYPE_STRING: // string value in excel
                    result = cell.getStringCellValue();
                    break;

                case Cell.CELL_TYPE_BOOLEAN: // boolean value in excel
                    result = cell.getBooleanCellValue();
                    break;

                case Cell.CELL_TYPE_BLANK: // blank value in excel
                    result = cell.getStringCellValue();
                    break;

                case Cell.CELL_TYPE_ERROR: // Error value in excel
                    result = cell.getErrorCellValue()+"";
                    break;


                }
            }
            else
            {
                return null;
            }
        }
        else
        {
            //System.out.println("Row is empty");
            return null;
        }

        inputStream.close(); 
    }

    catch (Exception ex){
        ex.printStackTrace();
        }

    return result.toString();

}

控制台:

java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(Unknown Source)
    at java.text.Format.format(Unknown Source)
    at utility.ExcelUtility.readExcel(ExcelUtility.java:116)

引导我伸出援手。

【问题讨论】:

  • 你为什么不用SimpleDateFormat
  • 是的,我只使用 SimpleDateFormat 并且我得到了异常,因为无法转换格式。

标签: java excel date apache-poi


【解决方案1】:

要获取日期,您可以这样做

Date myDate = cell.getDateCellValue()

然后像您的示例一样使用 SimpleDateFormat :

if(DateUtil.isCellDateFormatted(cell)){
    Date myDate = cell.getDateCellValue();
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String result = formatter.format(myDate);
    System.out.println("Today : " + result);    
}

【讨论】:

  • 为什么你不在result = formatter.format(myDate); 中使用myDate?相反,您使用的是cell,这会导致cell.toString() 导致@Ashok kumar 已经出现的相同错误。
  • 我的错误,我的观点更多的是获取日期而不是执行 dateFormat。我正在纠正它。
  • @Akah 是的,我理解我的错误。我更改了 myDate。它工作正常。
【解决方案2】:

您应该将日期作为文件区域中的字符串。我的读者喜欢这样。

switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    cellValue = String.valueOf((long) cell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                }

然后你用 SimpleDateFormat 解析字符串。

我的格式化程序;

public final static DateFormat BIRTHDAY_FORMATTER = new SimpleDateFormat("myPatterns");

和日期;

Date date = BIRTHDAY_FORMATTER.parse(myCellValue);

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多