Class DateFormatConverter


  • public final class DateFormatConverter
    extends Object
    Convert java DateFormat patterns into Excel custom number formats. For example, to format a date in excel using the "dd MMMM, yyyy" pattern and Japanese locale, use the following code:
    
          // returns "[$-0411]dd MMMM, yyyy;@" where the [$-0411] prefix tells Excel to use the Japanese locale
          String excelFormatPattern = DateFormatConverter.convert(Locale.JAPANESE, "dd MMMM, yyyy");
    
          CellStyle cellStyle = workbook.createCellStyle();
    
          DataFormat poiFormat = workbook.createDataFormat();
          cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
          cell.setCellValue(new Date());
          cell.setCellStyle(cellStyle);  // formats date as '2012年3月17日'
      
    TODO Generalise this for all Excel format strings
    • Method Detail

      • getPrefixForLocale

        public static String getPrefixForLocale​(Locale locale)
      • getJavaDatePattern

        public static String getJavaDatePattern​(int style,
                                                Locale locale)
      • getJavaTimePattern

        public static String getJavaTimePattern​(int style,
                                                Locale locale)
      • getJavaDateTimePattern

        public static String getJavaDateTimePattern​(int style,
                                                    Locale locale)