You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Shirish Amin <am...@us.ibm.com> on 2006/06/01 00:43:06 UTC

Problem exporting date in POI

I am working on a project that exports data from a database to an Excel 
file.  I am having a problem exporting the dates.

The dates are stored as Java long value in the database.  I am creating a 
Date object from the long value.  When this Date object is written to a 
cell that is formatted with the HSSFDataFormat(0x16) ("m/d/yy h:mm"), the 
output date/time in Excel file is four hours ahead.  It appears that the 
HSSFCell.setCellValue(Date date) API is automatically converting the time 
to the GMT time.  Don't know if I am doing something wrong.

Is there a way to stop this automatic conversion?  If not, does anyone 
have a suggestion to work around this problem?

Here is the code snippet:

        HSSFCellStyle _dateStyle = createCellStyleForDate(wb, false);
        private HSSFCellStyle createCellStyleForDate(HSSFWorkbook 
workbook, boolean wrapText)
        {
                HSSFCellStyle dateStyle =  workbook.createCellStyle();

 dateStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                dateStyle.setWrapText(wrapText);
                dateStyle.setDataFormat((short) 
HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); // custom format

            return dateStyle;
        }

        HSSFRow row = sheet.createRow(1);
        HSSFCell startTimeCell  = row.createCell(5);
        startTimeCell.setCellStyle(_dateStyle);

        Date startDate = new Date(objective.getStartTime());    // 
getStartTime() returns a long value
        startTimeCell.setCellValue(startDate);

I will appreciate any help on this problem.

Shirish
amin@us.ibm.com