Ich lese eine .xlsx-Datei und einen Datumswert abrufen und es sollte in ein Textfeld Feld eingegeben werden.Wie konvertiert man das Datumsformat vom 04. Mai 2016 bis 04/05/2016 mit Java Apache POI
In Excel ist der Zellenwert 04/05/2016.
Aber beim Abrufen des Datumswerts aus der Zelle wird der Wert 04-May-2016.
Wie konvertiere ich dieses 04-Mai-2016 Format in mm/dd/yyy.
Code:
public static String readExcel(String filePath,String fileName,String sheetName,int RowNumber,int ColNumber) throws Exception{
Object result = null;
try
{
sheet=getSheet(filePath, fileName, sheetName);
row=sheet.getRow(RowNumber);
if(row != null)
{
//System.out.println("Row is not empty");
cell= row.getCell(ColNumber);
if(cell!=null)
{
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// numeric value in excel
if(DateUtil.isCellDateFormatted(cell)){
//DateUtil.truncate(new Date(), java.util.Calendar.DAY_OF_MONTH);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
result = formatter.format(cell);
System.out.println("Today : " + result);
}
else{
result = new BigDecimal(cell.getNumericCellValue()).toPlainString();
}
break;
case Cell.CELL_TYPE_STRING: // string value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: // boolean value in excel
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK: // blank value in excel
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: // Error value in excel
result = cell.getErrorCellValue()+"";
break;
}
}
else
{
return null;
}
}
else
{
//System.out.println("Row is empty");
return null;
}
inputStream.close();
}
catch (Exception ex){
ex.printStackTrace();
}
return result.toString();
}
Console:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at utility.ExcelUtility.readExcel(ExcelUtility.java:116)
mich Führer zu erreichen.
warum verwenden Sie nicht 'SimpleDateFormat'? –
Ja, ich benutze nur das ** SimpleDateFormat ** und bekomme die Ausnahme, da das Format nicht konvertiert werden kann. –