gpt4 book ai didi

java - 使用 POI 从 Excel 工作表获取日期

转载 作者:行者123 更新时间:2023-12-01 06:30:04 32 4
gpt4 key购买 nike

我使用 POI 使用以下代码从 Excel 工作表中获取日期,

if (DateUtil.isCellDateFormatted(currentCell))
{
Date date = currentCell.getDateCellValue();
cellValue = date.getDate() + "/" + (date.getMonth() + 1) + "/" + (1900 + date.getYear());
}

默认情况下,Excel 工作表的日期格式为 MM/dd/yy。但如果我使月份值大于 12(即 >= 13),则格式将更改为 dd/MM/yy。

通过上面的代码,如果我给出的日期为 10/11/2010,则月份为 10,日期为 11。如果我给出的日期为 15/10/2010,则月份为 10,日期为 15。

但我需要维护一种格式,即 dd/MM/yyyy。我怎样才能实现这个目标?

最佳答案

使用 SimpleDateFormat 格式化日期,因为“currentCell.getDateCellValue()) 返回一个日期。

if (DateUtil.isCellDateFormatted(currentCell))
{
try {

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
cellValue = = sdf.format(currentCell.getDateCellValue());

} catch (ParseException e) {
e.printStackTrace();
}
}

这应该会阻止正在发生的自动转换。

关于java - 使用 POI 从 Excel 工作表获取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5509017/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com