如何从java中的Excel工作表中获取日期时间值?

时间:2014-11-05 10:55:13

标签: java excel-vba vba excel

尝试使用poi库代码从Excel工作表中读取日期时间值,例如 10/1/2014 8:47:10 AM

row.getCell(3).getDateCellValue(); 

我得到 Wed Oct 01 08:47:10 IST 2014 。但我需要将其转换为 2014-11-20 16:23:16 才能存储到我的数据库。我该如何转换它?

3 个答案:

答案 0 :(得分:0)

您使用的是JPA框架(hibernate)吗? 因为那时它应该只填写日期/时间戳字段并将其发送到数据库。

您还可以查看日期格式。

String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(myTimestamp);

在您的情况下应转换为

String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(myTimestamp);

编辑,对于jsp:

<%@ page import="java.text.SimpleDateFormat" %>    
<% SimpleDateFormat dateFormatWithTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");%>
<INPUT TYPE="text" value="<%=dateFormatWithTime.format(yourExcelSheetDateTime)%>">

答案 1 :(得分:0)

public static String getFormatedDate(final Date date) {
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return myFormat.format(date);
}

使用SimpleDateFormat

答案 2 :(得分:0)

只需转换所有单元格类型为字符串然后读取它

if ((cell == null) || (cell.equals("")) || (cell.getCellType() == cell.CELL_TYPE_BLANK)) {
                            arraylist.add("");
                        } else {
                            cell.setCellType(Cell.CELL_TYPE_STRING);
                            arraylist.add(cell);
                        }