2016-05-04 6 views
1

.xlsxファイルを読み込んで日付値を取得し、テキストボックスフィールドに入力する必要があります。Javaを使用して2016年5月4日から04/05/2016に日付形式を変換する方法Apache POI

Inは、セル値が04/05/2016であることを示しています。

しかし、セルから日付の値を取得するとき、値は04-May-2016となります。

04-May-2016形式をmm/dd/yyyに変換するにはどうすればよいですか。

コード:

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(); 

} 

コンソール:

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) 

手を差し伸べるために私をガイド。

+2

なぜ「SimpleDateFormat」を使用しないのですか? –

+0

はい、** SimpleDateFormat **のみを使用しています。形式を変換できないため、例外が発生します。 –

答えて

3

日付を取得するには、あなたが

Date myDate = cell.getDateCellValue() 

を行い、その後、あなたの例のようなのSimpleDateFormatを使用することができます。

if(DateUtil.isCellDateFormatted(cell)){ 
    Date myDate = cell.getDateCellValue(); 
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
    String result = formatter.format(myDate); 
    System.out.println("Today : " + result);  
} 
+0

なぜ 'result = formatter.format(myDate);'で 'myDate'を使用していないのですか?代わりに 'cell'を使って' cell.toString() 'を実行すると、@Ashok kumarはすでに同じエラーになります。 –

+0

私の誤りは、dateFormatを実行するよりも、日付を取得する方法が増えていたことです。私はそれを修正しています。 – Akah

+0

@Akah Ok、修正してください。私はそれを使用します。 –

0

あなたはファイル領域内の文字列として日付を取る必要があります。私の読者はこれを好きです。

switch (cell.getCellType()) { 
       case Cell.CELL_TYPE_NUMERIC: 
        cellValue = String.valueOf((long) cell.getNumericCellValue()); 
        break; 
       case Cell.CELL_TYPE_STRING: 
        cellValue = cell.getStringCellValue(); 
        break; 
       } 

次に、SimpleDateFormatで文字列を解析します。

私のフォーマッタ。

public final static DateFormat BIRTHDAY_FORMATTER = new SimpleDateFormat("myPatterns"); 

とdate;

Date date = BIRTHDAY_FORMATTER.parse(myCellValue); 
関連する問題