如何将Exceldate转换为Java的Timestamp?

我正在接收有人在Excel中写的CSV文件,我需要parsing这些文件到我的数据库。

然而,当我试图parsingdate到一个时间戳,它告诉我的格式是不正确的,并期望YYYY-MM-DD hh:mm:ss

有两种可能的解决办法,我不知道哪一个是可行的。
1.在导出csv之前,先将date转换为YYYY-MM-DD hh:mm:ss 2.以某种方式转换Java中的Exceldate格式。

我试图改变在Excel中的格式,但我没有看到任何格式化选项导致YYYY-MM-DD hh:mm:ss我得到最接近的是YY-MMM-DD或MM / DD / YY

哪一个是更好的溶剂,1或2,我该怎么做?

编辑相关的Java代码片段:

public static List<AffiliateEntry> convert(ArrayList<String[]> collection) { List<AffiliateEntry> entryList = new ArrayList<AffiliateEntry>(); HashMap<String, Integer> columnIndex = new HashMap(); for (int col_index = 0; col_index < collection.get(0).length; col_index++) { columnIndex.put(collection.get(0)[col_index].trim(), col_index); } for (int i = 1; i < collection.size(); i++) { String[] row = collection.get(i); AffiliateEntry convertedEntry = new AffiliateEntry(); int dateIndx = columnIndex.get("Date") != null ? columnIndex.get("Date") : 0; Timestamp date = (row[dateIndx] != null && !row[dateIndx].isEmpty()) ? Timestamp.valueOf(row[dateIndx]) : new Timestamp(System.currentTimeMillis()); convertedEntry.setReport_date(date); ....//more things to convert } } 

最简单的方法是修改原始数据集的格式。

您可以轻松创build自定义格式并将其应用于Excel中所需的所有单元格。 只需select单元格,右键单击>格式>自定义,然后添加新格式: yyyy-mm-dd hh:mm:ss

资料来源: 微软

我会select2。

您可以使用SimpleDateFormat类来parsing格式不同的date格式,例如:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date dateStr = formatter.parse(strDate);