JXL讀取Excel日期時間不準確

JXL讀取Excel日期時間多出了8個小時。

                    Cell c = rs.getCell(j, i);
                    if (c.getType() == CellType.DATE) {//手動填寫模板文件時爲 date 類型,其他情況有可能不是date類型
                        DateCell dc = (DateCell) c;
                        Date date = dc.getDate();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String sDate = sdf.format(date);
                        nextLine[j] = sDate;
                    }
                    else {
                        String d = rs.getCell(j, i).getContents().trim();
                        nextLine[j] = d;
                    }

解決辦法:獲取的日期時間需要調整時區。參見:http://www.andykhan.com/jexcelapi/tutorial.html#dates

                    Cell c = rs.getCell(j, i);
                    if (c.getType() == CellType.DATE) {//手動填寫模板文件時爲 date 類型,其他情況有可能不是date類型
                        DateCell dc = (DateCell) c;
                        Date date = dc.getDate();
                        TimeZone zone = TimeZone.getTimeZone("GMT");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        sdf.setTimeZone(zone);
                        String sDate = sdf.format(date);
                        nextLine[j] = sDate;
                    }
                    else {
                        String d = rs.getCell(j, i).getContents().trim();
                        nextLine[j] = d;
                    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章