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
This commit is contained in:
Nick Burch 2007-06-17 15:27:23 +00:00
parent bdc0883b6f
commit e40fac5f9b
2 changed files with 88 additions and 2 deletions

View File

@ -150,8 +150,44 @@ public class HSSFDateUtil
}
/**
* given a format ID this will check whether the format represents
* an internal date format or not.
* 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 excel date format or not.
* @see isDateFormat(int,String)
*/
public static boolean isInternalDateFormat(int format) {
boolean retval =false;

View File

@ -183,6 +183,56 @@ public class TestHSSFDateUtil
}
}
/**
* 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
.println("Testing org.apache.poi.hssf.usermodel.TestHSSFDateUtil");