Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

java - DataFormatter on cell containing date renders two digits years

I needed to get cell data from Excel files as they look like and bumped to DataFormatter class of Apache PO. This works like a charm except for cells containing date. Below is my code:

while (rowIterator.hasNext())
{
    Row row = rowIterator.next();

    StringBuilder rowDataBuilder = new StringBuilder();
    int iCellCount = row.getLastCellNum();
    for (int i = 0; i < iCellCount; i++)
    {
        Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
        rowDataBuilder.append(dataFormatter.formatCellValue(cell));
        rowDataBuilder.append(" | ");
    }

    _LOG.info("-----> row data: " + rowDataBuilder.toString());
}

For example, a cell contains 5/3/2013, I only get 5/3/13. Would there be any solutions for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For fetching the date in desired format you can use following:

  SimpleDateFormat DtFormat = new SimpleDateFormat("dd/MM/yyyy");
  Date date=Test.getRow(RowNum).getCell(CellNum).getDateCellValue();
  System.out.println(DtFormat.format(date).toString());

And now if the cell value is 05/03/2013, it will give o/p as 05/03/2013. I hope this will resolve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...