java to excel date formatting

samligo picture samligo · Jun 16, 2012 · Viewed 15k times · Source

The problem is when I'm running my application and have a grid (with strings and date columns) and save it as an excel file.

When I save it for the first time everything is correctly formatted, but when I try to save the same exact grid again a second time, the date formatting is gone (it's just a float value that when i right click and format to a dateTime object works). When I restart my app it will work again for the first time, then lose formatting again

the code looks like this:

Calendar calendar = Calendar.getInstance();

calendar.setTime((Date)data);
            Date gmtDate = new Date(((Date) data).getTime() + (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)));

writableCell = new jxl.write.DateTime(sheetColumn, sheetRow, gmtDate, jxl.write.DateTime.GMT);

cellFormat = new jxl.write.WritableCellFormat (new jxl.write.DateFormat("m/d/yyyy h:mm");

writableCell.setCellFormat(cellFormat);

sheet.addCell(writableCell);

I kept break-pointing and everything is as it should be (it always knew it was a dateTime type before going in to the sheet), so I don't think it's from the code.

Has anyone else run into this issue?

Answer

duffymo picture duffymo · Jun 16, 2012

Try this:

cellFormat = new jxl.write.WritableCellFormat (new jxl.write.DateFormat("MM/dd/yyyy hh:mm");