当我尝试解析时间时,无法在索引 0 处解析错误文本“时间”

问题描述 投票:0回答:1

我正在尝试从 java 中的 xlsx 表中检索日期单元格,并绑定以“MM/dd/yyyy”格式格式化数据

Cell celldate=row.getCell(2);
    String value=formatter.formatCellValue(celldate);
    CellType type=celldate.getCellType();
    String dat=value;
    if(dat!="") {

        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm a");
        // Parsing the date
        LocalDateTime date = LocalDateTime.parse(dat,inputFormat);
        // Format for output
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
        // Printing the date
    System.out.println(date.format(outputFormat));

但出现错误文本“时间”无法在索引 0 处解析

xlsx表

当我写入与表中写入的相同数据时,它正在工作

    Cell cellname=row.getCell(7);
    String stname=cellname.getStringCellValue();
    Cell celldate=row.getCell(2);
    String value=formatter.formatCellValue(celldate);
    CellType type=celldate.getCellType();
    String dat="12-09-2023 10:08 am";
    if(dat!="") {

        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm a");
        // Parsing the date
        LocalDateTime date = LocalDateTime.parse(dat,inputFormat);
        // Format for output
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
        // Printing the date
    System.out.println(date.format(outputFormat));
java date apache-poi java-time
1个回答
0
投票

通过添加此行解决了我的错误

Iterator <Row>rowiterator =sheet.iterator();
    Row headerRow= rowiterator.next();
© www.soinside.com 2019 - 2024. All rights reserved.