I have get the date from excel sheet using POI using the below code,
if (DateUtil.isCellDateFormatted(currentCell))
{
Date date = currentCell.getDateCellValue();
cellValue = date.getDate() + "/" + (date.getMonth() + 1) + "/" + (1900 + date.getYear());
}
By default the date format of excel sheet is MM/dd/yy. But if I make the month value is greater than 12 (i.e >= 13), the format is changed to dd/MM/yy.
By the above code,If I give date as 10/11/2010, it gives month as 10 and date as 11. If I give it like 15/10/2010, I got month as 10 and date as 15.
But I need to maintain the one format that is dd/MM/yyyy. How can I achieve this?
解决方案
Use SimpleDateFormat to format the date because "currentCell.getDateCellValue()) returns a Date.
if (DateUtil.isCellDateFormatted(currentCell))
{
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
cellValue = = sdf.format(currentCell.getDateCellValue());
} catch (ParseException e) {
e.printStackTrace();
}
}
This should prevent the automatic conversion that is taking place.