You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by ni...@apache.org on 2007/06/17 17:27:24 UTC

svn commit: r548044 - in /jakarta/poi/trunk/src: java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java

Author: nick
Date: Sun Jun 17 08:27:23 2007
New Revision: 548044

URL: http://svn.apache.org/viewvc?view=rev&rev=548044
Log:
Add a new method on HSSFDateUtil of isADateFormat, which will cope with both internal excel date formats, and custom date formats the are for dates (plus test)

Modified:
    jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java
    jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java

Modified: jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java?view=diff&rev=548044&r1=548043&r2=548044
==============================================================================
--- jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java (original)
+++ jakarta/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java Sun Jun 17 08:27:23 2007
@@ -148,10 +148,46 @@
             return null;
         }
     }
+    
+    /**
+     * Given a format ID and its format String, will check to see if the
+     *  format represents a date format or not.
+     * Firstly, it will check to see if the format ID corresponds to an
+     *  internal excel date format (eg most US date formats) 
+     * If not, it will check to see if the format string only contains
+     *  date formatting characters (ymd-/), which covers most
+     *  non US date formats.
+     *  
+     * @param formatIndex The index of the format, eg from ExtendedFormatRecord.getFormatIndex
+     * @param formatString The format string
+     */
+    public static boolean isADateFormat(int formatIndex, String formatString) {
+    	// First up, is this an internal date format?
+    	if(isInternalDateFormat(formatIndex)) {
+    		return true;
+    	}
+    	
+    	// If we didn't get a real string, it can't be
+    	if(formatString == null || formatString.length() == 0) {
+    		return false;
+    	}
+    	
+    	// Translate \- into just -, before matching
+    	String fs = formatString.replace("\\-","-"); 
+    	
+    	// Otherwise, check it's only made up of:
+    	//  y m d - /
+    	if(fs.matches("^[ymd\\-/]+$")) {
+    		return true;
+    	}
+    	
+    	return false;
+    }
 
     /**
-     * given a format ID this will check whether the format represents
-     * an internal date format or not. 
+     * Given a format ID this will check whether the format represents
+     *  an internal excel date format or not.
+     * @see isDateFormat(int,String) 
      */
     public static boolean isInternalDateFormat(int format) {
       boolean retval =false;

Modified: jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java
URL: http://svn.apache.org/viewvc/jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java?view=diff&rev=548044&r1=548043&r2=548044
==============================================================================
--- jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java (original)
+++ jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java Sun Jun 17 08:27:23 2007
@@ -182,6 +182,56 @@
                     HSSFDateUtil.getExcelDate(javaDate), oneMinute);
         }
     }
+    
+    /**
+     * Tests that we correctly detect date formats as such
+     */
+    public void testIdentifyDateFormats() {
+    	// First up, try with a few built in date formats
+    	short[] builtins = new short[] { 0x0e, 0x0f, 0x10, 0x16, 0x2d, 0x2e };
+    	for(int i=0; i<builtins.length; i++) {
+    		String formatStr = HSSFDataFormat.getBuiltinFormat(builtins[i]);
+    		assertTrue( HSSFDateUtil.isInternalDateFormat(builtins[i]) );
+    		assertTrue( HSSFDateUtil.isADateFormat(builtins[i],formatStr) );
+    	}
+    	
+    	// Now try a few built-in non date formats
+    	builtins = new short[] { 0x01, 0x02, 0x17, 0x1f, 0x30 };
+    	for(int i=0; i<builtins.length; i++) {
+    		String formatStr = HSSFDataFormat.getBuiltinFormat(builtins[i]);
+    		assertFalse( HSSFDateUtil.isInternalDateFormat(builtins[i]) );
+    		assertFalse( HSSFDateUtil.isADateFormat(builtins[i],formatStr) );
+    	}
+    	
+    	// Now for some non-internal ones
+    	// These come after the real ones
+    	int numBuiltins = HSSFDataFormat.getNumberOfBuiltinBuiltinFormats();
+    	assertTrue(numBuiltins < 60);
+    	short formatId = 60;
+    	assertFalse( HSSFDateUtil.isInternalDateFormat(formatId) );
+    	
+    	// Valid ones first
+    	String[] formats = new String[] {
+    			"yyyy-mm-dd", "yyyy/mm/dd", "yy/mm/dd", "yy/mmm/dd",
+    			"dd/mm/yy", "dd/mm/yyyy", "dd/mmm/yy",
+    			"dd-mm-yy", "dd-mm-yyyy",
+    			"dd\\-mm\\-yy", // Sometimes escaped
+    	};
+    	for(int i=0; i<formats.length; i++) {
+    		assertTrue( HSSFDateUtil.isADateFormat(formatId, formats[i]) );
+    	}
+    	
+    	// Then invalid ones
+    	formats = new String[] {
+    			"yyyy:mm:dd", 
+    			"0.0", "0.000",
+    			"0%", "0.0%",
+    			"", null
+    	};
+    	for(int i=0; i<formats.length; i++) {
+    		assertFalse( HSSFDateUtil.isADateFormat(formatId, formats[i]) );
+    	}
+    }
 
     public static void main(String [] args) {
         System.out



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org