Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.text.ParseException:不可解析的日期:";42321“;(它是关于将日期从excel格式转换为java格式,而不是关于DateFormat.parse。)_Java_Excel - Fatal编程技术网

java.text.ParseException:不可解析的日期:";42321“;(它是关于将日期从excel格式转换为java格式,而不是关于DateFormat.parse。)

java.text.ParseException:不可解析的日期:";42321“;(它是关于将日期从excel格式转换为java格式,而不是关于DateFormat.parse。),java,excel,Java,Excel,我从xlsx文件中获取数据,xlsx文件的数据如下 1 1 13/11/15 00:00:02 3 42.61 xxxx to xxxx 9 2 2 Axle Truck or Bus 2 1 13/11/15 00:00:12 1 27.11 xxxx to xxxx 3 2 3 Wheeler 3 1 13/11/15 00:00:15 3 22.65 xxxx to xxxx

我从xlsx文件中获取数据,xlsx文件的数据如下

1   1   13/11/15    00:00:02    3   42.61   xxxx to xxxx    9   2   2 Axle Truck or Bus
2   1   13/11/15    00:00:12    1   27.11   xxxx to xxxx    3   2   3 Wheeler
3   1   13/11/15    00:00:15    3   22.65   xxxx to xxxx    7   2   LMV 2 Axle Light Motor Vehicle
4   1   13/11/15    00:00:21    2   18.76   xxxx to xxxx    7   2   LMV 2 Axle Light Motor Vehicle
5   1   13/11/15    00:00:21    3   18.76   xxxx to xxxx    9   2   2 Axle Truck or Bus
从这些数据中,我逐行阅读并使用下面的代码插入数据库

但问题是,我无法从xlsx文件中获取第三列的日期,但我还有剩余的时间要打印

下面是我的代码:

processOneSheet("C:/Users/Penchalaiah/Desktop/New folder/"+hs.getAttribute("filename1"));
        System.out.println(hs.getAttribute("filename1"));
        System.out.println("clossing the connnection");
        ps.close();
        con1.close();
        System.out.println("execution completed");
        //request.setAttribute("message","THE XLSX DATA TRANSFERRED SUCCEFULLY");
        // request.getRequestDispatcher("/HomePage.jsp").forward(request, response);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   

    /*try {
        ps.close();
        con1.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/


}



public void processOneSheet(String filename) throws Exception {
    System.out.println("executing Process Method");
    OPCPackage pkg = OPCPackage.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    SharedStringsTable sst = r.getSharedStringsTable();

    XMLReader parser = fetchSheetParser(sst);

    // To look up the Sheet Name / Sheet Order / rID,
    //  you need to process the core Workbook stream.
    // Normally it's of the form rId# or rSheet#
    InputStream sheet2 = r.getSheet("rId2");
    System.out.println("Sheet2");
    InputSource sheetSource = new InputSource(sheet2);
    parser.parse(sheetSource);
    sheet2.close();
}



public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    ContentHandler handler = new SheetHandler(sst);
    parser.setContentHandler(handler);
    return parser;
}

/** 
 * See org.xml.sax.helpers.DefaultHandler javadocs 
 */
private  class SheetHandler extends DefaultHandler {

    private SharedStringsTable sst;
    private String lastContents;
    private boolean nextIsString;

    String TxnNo;
    String SurveyId;
    Date date;
    String Time;
    String Lane;
    String Avspeed;
    String Direction;
    String VehicleCategory;
    String Axle_count;
    String Vehicle;


    int i = 1;

    private SheetHandler(SharedStringsTable sst) {
        this.sst = sst;

    }

    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        // c => cell
        if(name.equals("c")) {
            // Print the cell reference
            //System.out.print(attributes.getValue("r") + " - ");
            // Figure out if the value is an index in the SST
            String cellType = attributes.getValue("t");
            if(cellType != null && cellType.equals("s")) {
                nextIsString = true;
            } else {
                nextIsString = false;
            }
        }
        // Clear contents cache
        lastContents = "";
    }

    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // Process the last contents as required.
        // Do now, as characters() may be called more than once
        if(nextIsString) {
            int idx = Integer.parseInt(lastContents);
            lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
            nextIsString = false;
        }
        // v => contents of a cell
        // Output after we've seen the string contents
        if(name.equals("v")) {

            if(i == 1){
                TxnNo = lastContents;
            }
            if(i == 2){
                SurveyId = lastContents;
            }
            if(i == 3){
                try {
 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    date = new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH).parse(lastContents);
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                    System.out.println("The Date is: "+date);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 4){
                Time = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 5){
                Lane = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 6){
                Avspeed = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 7){
                Direction = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 8){
                VehicleCategory = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 9){
                Axle_count = lastContents;
                //System.out.print(lastContents+"("+i+")");
            }
            if(i == 10){
                Vehicle = lastContents;
                //System.out.print(lastContents+"("+i+")");

                //System.out.print(lastContents+"("+i+")");
                insertInToDb(TxnNo, SurveyId, date, Time, Lane, Avspeed,Direction, VehicleCategory, Axle_count, Vehicle);
                i = 0;
            }


            i++;


        }

    }


    public void characters(char[] ch, int start, int length)
            throws SAXException {
        lastContents += new String(ch, start, length);
    }
}
    int gcc = 0;
    public void insertInToDb(String TxnNo,String SurveyId,Date date, String Time,String Lane,String Avspeed,String Direction, String VehicleCategory, String Axle_count, String Vehicle){

        try {

            ps.setString(1, TxnNo);
            ps.setString(2, SurveyId);
            ps.setDate(3, (java.sql.Date) date);
            ps.setString(4, Time);
            ps.setString(5, Lane);
            ps.setString(6, Avspeed);
            ps.setString(7, Direction);
            ps.setString(8, VehicleCategory);
            ps.setString(9, Axle_count);
            ps.setString(10, Vehicle);
            ps.setString(11, (String)hs.getAttribute("zoneId1"));
            ps.setString(12, (String)hs.getAttribute("location1"));
            ps.executeUpdate();
我在/@@@@@@@@@@@@@之间遇到异常情况
//@@@@@@@@@@@ 上面显示的代码的数量
当我打印字符串的日期列时,我得到42321这个值


如何转换为该列值的日期并插入数据库?

Excel将日期存储为1900年1月0日起的天数。因此,您需要手动创建实际日期,而不仅仅是解析

@Test
public void excel() {
    Calendar cl = Calendar.getInstance();
    cl.set(Calendar.YEAR, 1900);
    cl.set(Calendar.MONTH, Calendar.JANUARY);
    cl.set(Calendar.DAY_OF_MONTH, 0);
    cl.add(Calendar.DAY_OF_MONTH, 42321 - 1);
    System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(cl.getTime()));

    double excel = 2.31481481481481E-005;
    cl.set(Calendar.SECOND, (int) Math.round(excel * 86400));
    cl.set(Calendar.MINUTE, (int) Math.round(excel * 86400 / 60));
    cl.set(Calendar.HOUR_OF_DAY, (int) Math.round(excel * 86400 / 60 / 60));
    System.out.println(new SimpleDateFormat("HH:mm:ss").format(cl.getTime()));
}
输出

13/11/2015
00:00:02
编辑

注意
-1
靠近
43231
,因为Excel中存在缺陷,所以我们需要跳过不存在的1900年2月29日

编辑


增加了时间计算。

dd/M/yy是否有效?dd/M/yy是否有效通过使用这个,我得到了同样的例外。顺便说一句,如果(i==X)改变为
开关,它会更有效,更可读。这是什么?什么能做到这一点?顺便说一句,如果(i==X)改为switch,我不认为这个问题是重复的——它是关于将日期从excel格式转换为java格式,而不是关于DateFormat.parse。我还得到了2015年11月14日。我只需要15年11月13日不需要2015@honey1使用“dd/MM/yy”作为格式字符串,使用dd/MM/yy我将获得2015年11月14日。但是,如何利用时间呢?我将xlsx中的时间作为字符串,当我打印字符串时,它给出2.31481E-005。如何获得像xlsx一样的时间非常感谢@Mikhail Kuchma,但我需要打印和xlsx一样的时间