【问题标题】:Reading date from excel file for different locations从excel文件中读取不同位置的日期
【发布时间】:2015-04-10 05:58:59
【问题描述】:

我正在使用 Apache POI 读取 Java 中的 Excel 文件。

有些列是日期格式的,格式是 MM/DD/YYYY

当我在印度读取文件时,它正确读取了日期。

例如04/07/2015 读作 07-Apr-2015 例如2014 年 11 月 30 日读作 2015 年 11 月 30 日

这很好。

但是,当相同的代码在美国运行时,它的行为很奇怪。

例如04/07/2015 读作 04-Jul-2015 例如2014 年 11 月 30 日未能说 30 月无效。

当我在美国和印度的 MS Excel 中打开 excel 文件时,它们的可见性是相同的。即在 excel 中它显示为 MM/DD/YYYY。但是在美国通过代码读取错误。

我的代码很简单:

if (DateUtil.isCellDateFormatted(cell))
    data.append(cell);

POI 会根据位置进行某些操作。有人可以建议如何处理这种情况。

【问题讨论】:

标签: java apache date apache-poi locale


【解决方案1】:

试试这个(cell1.getDateCellValue() 工作正常):

//get your cell value

Cell cell1 = sheet.getRow(0).getCell(0); 
System.out.println(cell1.getDateCellValue());

尝试使用 SimpleDateFormat 类:

//using Apache POI get the date
SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY");
Date date = sdf.parse("your date");
String formattedDate=sdf.format(date);
System.out.println(formattedDate);

另一种获取时区的方法:

SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多