From: Nick Burch Date: Sun, 17 Jun 2007 15:27:23 +0000 (+0000) Subject: Add a new method on HSSFDateUtil of isADateFormat, which will cope with both internal... X-Git-Tag: REL_3_0_1_RC3~20 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e40fac5f9bf5490afd1d51888fb1f16e16cd6613;p=poi.git 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) git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@548044 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java index 3a1a8f09be..a12f12623f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java @@ -148,10 +148,46 @@ public class HSSFDateUtil 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; diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java index 01539f0939..ddf20fd774 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java @@ -182,6 +182,56 @@ public class TestHSSFDateUtil 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