]> source.dussan.org Git - poi.git/commitdiff
Bugzilla >52928 - DateFormatConverter: an utility to convert instances of java.text...
authorYegor Kozlov <yegor@apache.org>
Sat, 17 Mar 2012 12:16:45 +0000 (12:16 +0000)
committerYegor Kozlov <yegor@apache.org>
Sat, 17 Mar 2012 12:16:45 +0000 (12:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1301923 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/ss/util/DateFormatConverter.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java [new file with mode: 0644]

index abf8b7e4dae01b85545228f357601bf32cfb8cd3..ef6a7f8a2376cd788490c215897e5f60cda1d73d 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="add">52928 - DateFormatConverter: an utility to convert instances of java.text.DateFormat to Excel format patterns</action>
            <action dev="poi-developers" type="fix">52895 - show SSTIndex instead of XFIndex in LabelSSTRecord.toString()</action>
            <action dev="poi-developers" type="fix">52835 - Tolerate missing Count and UniqueCount attributes when parsing shared strings table in XSSF eventusermodel</action>
            <action dev="poi-developers" type="add">52818 - Added implementation for RANK()</action>
diff --git a/src/java/org/apache/poi/ss/util/DateFormatConverter.java b/src/java/org/apache/poi/ss/util/DateFormatConverter.java
new file mode 100644 (file)
index 0000000..b11f7a5
--- /dev/null
@@ -0,0 +1,426 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.util;\r
+\r
+import java.text.DateFormat;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Locale;\r
+import java.util.Map;\r
+\r
+/**\r
+ *  Convert java DateFormat patterns into Excel custom number formats.\r
+ *  For example, to format a date in excel using the "dd MMMM, yyyy" pattern and Japanese\r
+ *  locale, use the following code:\r
+ *\r
+ *  <pre><code>\r
+ *      // returns "[$-0411]dd MMMM, yyyy;@" where the [$-0411] prefix tells Excel to use the Japanese locale\r
+ *      String excelFormatPattern = DateFormatConverter.convert(Locale.JAPANESE, "dd MMMM, yyyy");\r
+ *\r
+ *      CellStyle cellStyle = workbook.createCellStyle();\r
+ *\r
+ *      DataFormat poiFormat = workbook.createDataFormat();\r
+ *      cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));\r
+ *      cell.setCellValue(new Date());\r
+ *      cell.setCellStyle(cellStyle);  // formats date as '2012\u5e743\u670817\u65e5'\r
+ *\r
+ *  </code></pre>\r
+ *\r
+ *\r
+ */\r
+public class DateFormatConverter  {\r
+       \r
+       public static class DateFormatTokenizer {\r
+               String format;\r
+               int pos;\r
+               \r
+               public DateFormatTokenizer(String format) {\r
+                       this.format = format;\r
+               }\r
+               \r
+               public String getNextToken() {\r
+                       if( pos >= format.length() ) {\r
+                               return null;\r
+                       }\r
+                       int subStart = pos;\r
+                       char curChar = format.charAt(pos);\r
+                       ++pos;\r
+                       if( curChar == '\'' ) {\r
+                               while( ( pos < format.length() ) && ( ( curChar = format.charAt(pos) ) != '\'' ) ) {\r
+                                       ++pos;\r
+                               }\r
+                               if( pos < format.length() ) {\r
+                                       ++pos;\r
+                               }\r
+                       } else {\r
+                               char activeChar = curChar;\r
+                               while( ( pos < format.length() ) && ( ( curChar = format.charAt(pos) ) == activeChar ) ) {\r
+                                       ++pos;\r
+                               }\r
+                       }\r
+                       return format.substring(subStart,pos);\r
+               }\r
+               \r
+               public static String[] tokenize( String format ) {\r
+                       List<String> result = new ArrayList<String>();\r
+                       \r
+                       DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);\r
+                       String token;\r
+                       while( ( token = tokenizer.getNextToken() ) != null ) {\r
+                               result.add(token);\r
+                       }\r
+                       \r
+                       return result.toArray(new String[0]);\r
+               }\r
+               \r
+               public String toString() {\r
+                       StringBuilder result = new StringBuilder();\r
+                       \r
+                       DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);\r
+                       String token;\r
+                       while( ( token = tokenizer.getNextToken() ) != null ) {\r
+                               if( result.length() > 0 ) {\r
+                                       result.append( ", " );\r
+                               }\r
+                               result.append("[").append(token).append("]");\r
+                       }\r
+                       \r
+                       return result.toString();\r
+               }               \r
+       }               \r
+       \r
+       private static Map<String,String> tokenConversions = prepareTokenConversions();\r
+       private static Map<String,String> localePrefixes = prepareLocalePrefixes();\r
+       \r
+       private static Map<String,String> prepareTokenConversions() {\r
+               Map<String,String> result = new HashMap<String,String>();\r
+               \r
+               result.put( "EEEE", "dddd" );\r
+               result.put( "EEE", "ddd" );\r
+               result.put( "EE", "ddd" );\r
+               result.put( "E", "d" );\r
+               result.put( "Z", "" );\r
+               result.put( "z", "" );\r
+               result.put( "a", "am/pm" );\r
+               result.put( "A", "AM/PM" );\r
+               result.put( "K", "H" );\r
+               result.put( "KK", "HH" );\r
+               result.put( "k", "h" );\r
+               result.put( "kk", "hh" );\r
+               result.put( "S", "0" );\r
+               result.put( "SS", "00" );\r
+               result.put( "SSS", "000" );\r
+               \r
+               return result;\r
+       }\r
+       \r
+       private static Map<String,String> prepareLocalePrefixes() {\r
+               Map<String,String> result = new HashMap<String,String>();\r
+               \r
+               result.put( "af", "[$-0436]" );\r
+               result.put( "am", "[$-45E]" );\r
+               result.put( "ar_ae", "[$-3801]" );\r
+               result.put( "ar_bh", "[$-3C01]" );\r
+               result.put( "ar_dz", "[$-1401]" );\r
+               result.put( "ar_eg", "[$-C01]" );\r
+               result.put( "ar_iq", "[$-0801]" );\r
+               result.put( "ar_jo", "[$-2C01]" );\r
+               result.put( "ar_kw", "[$-3401]" );\r
+               result.put( "ar_lb", "[$-3001]" );\r
+               result.put( "ar_ly", "[$-1001]" );\r
+               result.put( "ar_ma", "[$-1801]" );\r
+               result.put( "ar_om", "[$-2001]" );\r
+               result.put( "ar_qa", "[$-4001]" );\r
+               result.put( "ar_sa", "[$-0401]" );\r
+               result.put( "ar_sy", "[$-2801]" );\r
+               result.put( "ar_tn", "[$-1C01]" );\r
+               result.put( "ar_ye", "[$-2401]" );\r
+               result.put( "as", "[$-44D]" );\r
+               result.put( "az_az", "[$-82C]" );\r
+               result.put( "az_az", "[$-42C]" );\r
+               result.put( "be", "[$-0423]" );\r
+               result.put( "bg", "[$-0402]" );\r
+               result.put( "bn", "[$-0845]" );\r
+               result.put( "bn", "[$-0445]" );\r
+               result.put( "bo", "[$-0451]" );\r
+               result.put( "bs", "[$-141A]" );\r
+               result.put( "ca", "[$-0403]" );\r
+               result.put( "cs", "[$-0405]" );\r
+               result.put( "cy", "[$-0452]" );\r
+               result.put( "da", "[$-0406]" );\r
+               result.put( "de_at", "[$-C07]" );\r
+               result.put( "de_ch", "[$-0807]" );\r
+               result.put( "de_de", "[$-0407]" );\r
+               result.put( "de_li", "[$-1407]" );\r
+               result.put( "de_lu", "[$-1007]" );\r
+               result.put( "dv", "[$-0465]" );\r
+               result.put( "el", "[$-0408]" );\r
+               result.put( "en_au", "[$-C09]" );\r
+               result.put( "en_bz", "[$-2809]" );\r
+               result.put( "en_ca", "[$-1009]" );\r
+               result.put( "en_cb", "[$-2409]" );\r
+               result.put( "en_gb", "[$-0809]" );\r
+               result.put( "en_ie", "[$-1809]" );\r
+               result.put( "en_in", "[$-4009]" );\r
+               result.put( "en_jm", "[$-2009]" );\r
+               result.put( "en_nz", "[$-1409]" );\r
+               result.put( "en_ph", "[$-3409]" );\r
+               result.put( "en_tt", "[$-2C09]" );\r
+               result.put( "en_us", "[$-0409]" );\r
+               result.put( "en_za", "[$-1C09]" );\r
+               result.put( "es_ar", "[$-2C0A]" );\r
+               result.put( "es_bo", "[$-400A]" );\r
+               result.put( "es_cl", "[$-340A]" );\r
+               result.put( "es_co", "[$-240A]" );\r
+               result.put( "es_cr", "[$-140A]" );\r
+               result.put( "es_do", "[$-1C0A]" );\r
+               result.put( "es_ec", "[$-300A]" );\r
+               result.put( "es_es", "[$-40A]" );\r
+               result.put( "es_gt", "[$-100A]" );\r
+               result.put( "es_hn", "[$-480A]" );\r
+               result.put( "es_mx", "[$-80A]" );\r
+               result.put( "es_ni", "[$-4C0A]" );\r
+               result.put( "es_pa", "[$-180A]" );\r
+               result.put( "es_pe", "[$-280A]" );\r
+               result.put( "es_pr", "[$-500A]" );\r
+               result.put( "es_py", "[$-3C0A]" );\r
+               result.put( "es_sv", "[$-440A]" );\r
+               result.put( "es_uy", "[$-380A]" );\r
+               result.put( "es_ve", "[$-200A]" );\r
+               result.put( "et", "[$-0425]" );\r
+               result.put( "eu", "[$-42D]" );\r
+               result.put( "fa", "[$-0429]" );\r
+               result.put( "fi", "[$-40B]" );\r
+               result.put( "fo", "[$-0438]" );\r
+               result.put( "fr_be", "[$-80C]" );\r
+               result.put( "fr_ca", "[$-C0C]" );\r
+               result.put( "fr_ch", "[$-100C]" );\r
+               result.put( "fr_fr", "[$-40C]" );\r
+               result.put( "fr_lu", "[$-140C]" );\r
+               result.put( "gd", "[$-43C]" );\r
+               result.put( "gd_ie", "[$-83C]" );\r
+               result.put( "gn", "[$-0474]" );\r
+               result.put( "gu", "[$-0447]" );\r
+               result.put( "he", "[$-40D]" );\r
+               result.put( "hi", "[$-0439]" );\r
+               result.put( "hr", "[$-41A]" );\r
+               result.put( "hu", "[$-40E]" );\r
+               result.put( "hy", "[$-42B]" );\r
+               result.put( "id", "[$-0421]" );\r
+               result.put( "is", "[$-40F]" );\r
+               result.put( "it_ch", "[$-0810]" );\r
+               result.put( "it_it", "[$-0410]" );\r
+               result.put( "ja", "[$-0411]" );\r
+               result.put( "kk", "[$-43F]" );\r
+               result.put( "km", "[$-0453]" );\r
+               result.put( "kn", "[$-44B]" );\r
+               result.put( "ko", "[$-0412]" );\r
+               result.put( "ks", "[$-0460]" );\r
+               result.put( "la", "[$-0476]" );\r
+               result.put( "lo", "[$-0454]" );\r
+               result.put( "lt", "[$-0427]" );\r
+               result.put( "lv", "[$-0426]" );\r
+               result.put( "mi", "[$-0481]" );\r
+               result.put( "mk", "[$-42F]" );\r
+               result.put( "ml", "[$-44C]" );\r
+               result.put( "mn", "[$-0850]" );\r
+               result.put( "mn", "[$-0450]" );\r
+               result.put( "mr", "[$-44E]" );\r
+               result.put( "ms_bn", "[$-83E]" );\r
+               result.put( "ms_my", "[$-43E]" );\r
+               result.put( "mt", "[$-43A]" );\r
+               result.put( "my", "[$-0455]" );\r
+               result.put( "ne", "[$-0461]" );\r
+               result.put( "nl_be", "[$-0813]" );\r
+               result.put( "nl_nl", "[$-0413]" );\r
+               result.put( "no_no", "[$-0814]" );\r
+               result.put( "or", "[$-0448]" );\r
+               result.put( "pa", "[$-0446]" );\r
+               result.put( "pl", "[$-0415]" );\r
+               result.put( "pt_br", "[$-0416]" );\r
+               result.put( "pt_pt", "[$-0816]" );\r
+               result.put( "rm", "[$-0417]" );\r
+               result.put( "ro", "[$-0418]" );\r
+               result.put( "ro_mo", "[$-0818]" );\r
+               result.put( "ru", "[$-0419]" );\r
+               result.put( "ru_mo", "[$-0819]" );\r
+               result.put( "sa", "[$-44F]" );\r
+               result.put( "sb", "[$-42E]" );\r
+               result.put( "sd", "[$-0459]" );\r
+               result.put( "si", "[$-45B]" );\r
+               result.put( "sk", "[$-41B]" );\r
+               result.put( "sl", "[$-0424]" );\r
+               result.put( "so", "[$-0477]" );\r
+               result.put( "sq", "[$-41C]" );\r
+               result.put( "sr_sp", "[$-C1A]" );\r
+               result.put( "sr_sp", "[$-81A]" );\r
+               result.put( "sv_fi", "[$-81D]" );\r
+               result.put( "sv_se", "[$-41D]" );\r
+               result.put( "sw", "[$-0441]" );\r
+               result.put( "ta", "[$-0449]" );\r
+               result.put( "te", "[$-44A]" );\r
+               result.put( "tg", "[$-0428]" );\r
+               result.put( "th", "[$-41E]" );\r
+               result.put( "tk", "[$-0442]" );\r
+               result.put( "tn", "[$-0432]" );\r
+               result.put( "tr", "[$-41F]" );\r
+               result.put( "ts", "[$-0431]" );\r
+               result.put( "tt", "[$-0444]" );\r
+               result.put( "uk", "[$-0422]" );\r
+               result.put( "ur", "[$-0420]" );\r
+               result.put( "UTF_8", "[$-0000]" );\r
+               result.put( "uz_uz", "[$-0843]" );\r
+               result.put( "uz_uz", "[$-0443]" );\r
+               result.put( "vi", "[$-42A]" );\r
+               result.put( "xh", "[$-0434]" );\r
+               result.put( "yi", "[$-43D]" );\r
+               result.put( "zh_cn", "[$-0804]" );\r
+               result.put( "zh_hk", "[$-C04]" );\r
+               result.put( "zh_mo", "[$-1404]" );\r
+               result.put( "zh_sg", "[$-1004]" );\r
+               result.put( "zh_tw", "[$-0404]" );\r
+               result.put( "zu", "[$-0435]" );\r
+\r
+               result.put( "ar", "[$-0401]" );\r
+               result.put( "bn", "[$-0845]" );\r
+               result.put( "de", "[$-0407]" );\r
+               result.put( "en", "[$-0409]" );\r
+               result.put( "es", "[$-40A]" );\r
+               result.put( "fr", "[$-40C]" );\r
+               result.put( "it", "[$-0410]" );\r
+               result.put( "ms", "[$-43E]" );\r
+               result.put( "nl", "[$-0413]" );\r
+               result.put( "nn", "[$-0814]" );\r
+               result.put( "no", "[$-0414]" );\r
+               result.put( "pt", "[$-0816]" );\r
+               result.put( "sr", "[$-C1A]" );\r
+               result.put( "sv", "[$-41D]" );\r
+               result.put( "uz", "[$-0843]" );\r
+               result.put( "zh", "[$-0804]" );\r
+               \r
+               result.put( "ga", "[$-43C]" );\r
+               result.put( "ga_ie", "[$-83C]" );\r
+               result.put( "in", "[$-0421]" );\r
+               result.put( "iw", "[$-40D]" );\r
+               \r
+               return result;\r
+       }\r
+       \r
+       public static String getPrefixForLocale( Locale locale ) {\r
+               String localeString = locale.toString().toLowerCase();\r
+               String result = localePrefixes.get( localeString );\r
+               if( result == null ) {\r
+                       result = localePrefixes.get( localeString.substring( 0, 2 ) );\r
+                       if( result ==  null ) {\r
+                               Locale parentLocale = new Locale(localeString.substring( 0, 2 ));\r
+                               System.out.println( "Unable to find prefix for " + locale + "(" + locale.getDisplayName() + ") or " \r
+                                               + localeString.substring( 0, 2 ) + "(" + parentLocale.getDisplayName() + ")" );\r
+                               return "";\r
+                       }\r
+               }\r
+               return result;\r
+       }\r
+\r
+    public static String convert( Locale locale, DateFormat df ) {\r
+        String ptrn = ((SimpleDateFormat)df).toPattern();\r
+        return convert(locale, ptrn);\r
+    }\r
+\r
+    public static String convert( Locale locale, String format ) {\r
+               StringBuilder result = new StringBuilder();\r
+               \r
+               result.append(getPrefixForLocale(locale));\r
+               DateFormatTokenizer tokenizer = new DateFormatTokenizer(format);\r
+               String token;\r
+               while( ( token = tokenizer.getNextToken() ) != null ) {\r
+                       if( token.startsWith("'") ) {\r
+                               result.append( token.replaceAll("'", "\"") );\r
+                       } else if( ! Character.isLetter( token.charAt( 0 ) ) ) {\r
+                               result.append( token );\r
+                       } else {\r
+                               // It's a code, translate it if necessary\r
+                               String mappedToken = tokenConversions.get(token);\r
+                               result.append( mappedToken == null ? token : mappedToken );\r
+                       }\r
+               }\r
+        result.append(";@");\r
+               return result.toString().trim();\r
+       }\r
+       \r
+       public static String getJavaDatePattern(int style, Locale locale) {\r
+       DateFormat df = DateFormat.getDateInstance(style, locale);\r
+       if( df instanceof SimpleDateFormat ) {\r
+               return ((SimpleDateFormat)df).toPattern();\r
+       } else {\r
+               switch( style ) {\r
+               case DateFormat.SHORT:\r
+                       return "d/MM/yy";\r
+               case DateFormat.MEDIUM:\r
+                       return "MMM d, yyyy";\r
+               case DateFormat.LONG:\r
+                       return "MMMM d, yyyy";\r
+               case DateFormat.FULL:\r
+                       return "dddd, MMMM d, yyyy";\r
+               default:\r
+                       return "MMM d, yyyy";\r
+               }\r
+       }\r
+       }\r
+       \r
+       public static String getJavaTimePattern(int style, Locale locale) {\r
+       DateFormat df = DateFormat.getTimeInstance(style, locale);\r
+       if( df instanceof SimpleDateFormat ) {\r
+               return ((SimpleDateFormat)df).toPattern();\r
+       } else {\r
+               switch( style ) {\r
+               case DateFormat.SHORT:\r
+                       return "h:mm a";\r
+               case DateFormat.MEDIUM:\r
+                       return "h:mm:ss a";\r
+               case DateFormat.LONG:\r
+                       return "h:mm:ss a";\r
+               case DateFormat.FULL:\r
+                       return "h:mm:ss a";\r
+               default:\r
+                       return "h:mm:ss a";\r
+               }\r
+       }\r
+       }\r
+       \r
+       public static String getJavaDateTimePattern(int style, Locale locale) {\r
+       DateFormat df = DateFormat.getDateTimeInstance(style, style, locale);\r
+       if( df instanceof SimpleDateFormat ) {\r
+               return ((SimpleDateFormat)df).toPattern();\r
+       } else {\r
+               switch( style ) {\r
+               case DateFormat.SHORT:\r
+                       return "M/d/yy h:mm a";\r
+               case DateFormat.MEDIUM:\r
+                       return "MMM d, yyyy h:mm:ss a";\r
+               case DateFormat.LONG:\r
+                       return "MMMM d, yyyy h:mm:ss a";\r
+               case DateFormat.FULL:\r
+                       return "dddd, MMMM d, yyyy h:mm:ss a";\r
+               default:\r
+                       return "MMM d, yyyy h:mm:ss a";\r
+               }\r
+       }\r
+       }\r
+               \r
+}\r
diff --git a/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java b/src/testcases/org/apache/poi/ss/util/TestDateFormatConverter.java
new file mode 100644 (file)
index 0000000..3342a91
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.ss.util;
+
+import junit.framework.TestCase;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.util.TempFile;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+public final class TestDateFormatConverter extends TestCase {
+    private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception {
+
+        Workbook workbook = new HSSFWorkbook();
+        String sheetName;
+        if( dates ) {
+            if( times ) {
+                sheetName = "DateTimes";
+            } else {
+                sheetName = "Dates";
+            }
+        } else {
+            sheetName = "Times";
+        }
+        Sheet sheet = workbook.createSheet(sheetName);
+        Row header = sheet.createRow(0);
+        header.createCell(0).setCellValue("locale");
+        header.createCell(1).setCellValue("DisplayName");
+        header.createCell(2).setCellValue("Excel " + styleName);
+        header.createCell(3).setCellValue("java.text.DateFormat");
+        header.createCell(4).setCellValue("Equals");
+        header.createCell(5).setCellValue("Java pattern");
+        header.createCell(6).setCellValue("Excel pattern");
+
+        int rowNum = 1;
+        for( Locale locale : DateFormat.getAvailableLocales() ) {
+            Row row = sheet.createRow(rowNum++);
+
+            row.createCell(0).setCellValue(locale.toString());
+            row.createCell(1).setCellValue(locale.getDisplayName());
+
+            DateFormat dateFormat;
+            if( dates ) {
+                if( times ) {
+                    dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
+                } else {
+                    dateFormat = DateFormat.getDateInstance(style, locale);
+                }
+            } else {
+                dateFormat = DateFormat.getTimeInstance(style, locale);
+            }
+
+            Cell cell = row.createCell(2);
+
+            cell.setCellValue(date);
+            CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
+
+            String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
+            String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
+
+            DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
+            cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
+            row.createCell(3).setCellValue(dateFormat.format(date));
+
+            cell.setCellStyle(cellStyle);
+
+            // the formula returns TRUE is the formatted date in column C equals to the string in column D
+            row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum);
+            row.createCell(5).setCellValue(javaDateFormatPattern);
+            row.createCell(6).setCellValue(excelFormatPattern);
+        }
+
+        File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx");
+        FileOutputStream outputStream = new FileOutputStream(outputFile);
+        try {
+            workbook.write(outputStream);
+        } finally {
+            outputStream.close();
+        }
+        System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel");
+
+    }
+
+    public void testJavaDateFormatsInExcel() throws Exception {
+
+        Date date = new Date();
+
+        outputLocaleDataFormats(date, true, false, DateFormat.DEFAULT, "Default" );
+        outputLocaleDataFormats(date, true, false, DateFormat.SHORT, "Short" );
+        outputLocaleDataFormats(date, true, false, DateFormat.MEDIUM, "Medium" );
+        outputLocaleDataFormats(date, true, false, DateFormat.LONG, "Long" );
+        outputLocaleDataFormats(date, true, false, DateFormat.FULL, "Full" );
+
+        outputLocaleDataFormats(date, true, true, DateFormat.DEFAULT, "Default" );
+        outputLocaleDataFormats(date, true, true, DateFormat.SHORT, "Short" );
+        outputLocaleDataFormats(date, true, true, DateFormat.MEDIUM, "Medium" );
+        outputLocaleDataFormats(date, true, true, DateFormat.LONG, "Long" );
+        outputLocaleDataFormats(date, true, true, DateFormat.FULL, "Full" );
+
+        outputLocaleDataFormats(date, false, true, DateFormat.DEFAULT, "Default" );
+        outputLocaleDataFormats(date, false, true, DateFormat.SHORT, "Short" );
+        outputLocaleDataFormats(date, false, true, DateFormat.MEDIUM, "Medium" );
+        outputLocaleDataFormats(date, false, true, DateFormat.LONG, "Long" );
+        outputLocaleDataFormats(date, false, true, DateFormat.FULL, "Full" );
+    }
+
+}