← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17119: change to use regex in dateUtil.dateIsValid, needed to support more exotic calendars like Nepali, ...

 

------------------------------------------------------------
revno: 17119
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-10-15 16:24:13 +0700
message:
  change to use regex in dateUtil.dateIsValid, needed to support more exotic calendars like Nepali, where jdk DateFormat validation is not enough
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/DateUtilsTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2014-10-08 07:48:52 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2014-10-15 09:24:13 +0000
@@ -28,7 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.apache.commons.validator.routines.DateValidator;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.period.Period;
@@ -385,17 +384,18 @@
         return yearString + "-" + monthString + "-" + dayString;
     }
 
+    private static final String DEFAULT_DATE_REGEX = "\\b\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-2])\\b";
+
     /**
      * This method checks whether the String inDate is a valid date following
      * the format "yyyy-MM-dd".
      *
-     * @param date the string to be checked.
-     * @return true/false depending on whether the string is a date according to
-     * the format "yyyy-MM-dd".
+     * @param dateString the string to be checked.
+     * @return true/false depending on whether the string is a date according to the format "yyyy-MM-dd".
      */
     public static boolean dateIsValid( String dateString )
     {
-        return DateValidator.getInstance().isValid( dateString, DEFAULT_DATE_FORMAT );
+        return dateString.matches( DEFAULT_DATE_REGEX );
     }
 
     /**

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/DateUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/DateUtilsTest.java	2014-10-14 13:30:08 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/DateUtilsTest.java	2014-10-15 09:24:13 +0000
@@ -50,6 +50,14 @@
         assertFalse( dateIsValid( "07-07-2000" ) );
         assertFalse( dateIsValid( "2000-03-40" ) );
         assertFalse( dateIsValid( "20d20-03-01" ) );
+        assertTrue( dateIsValid( "0000-12-32" ) );
+        assertTrue( dateIsValid( "2014-01-01" ) );
+        assertFalse( dateIsValid( "2014-12-33" ) );
+        assertFalse( dateIsValid( "2014-13-32" ) );
+        assertFalse( dateIsValid( "2014-ab-cd" ) );
+        assertFalse( dateIsValid( "201-01-01" ) );
+        assertFalse( dateIsValid( "01-01-01" ) );
+        assertFalse( dateIsValid( "abcd-01-01" ) );
     }
     
     @Test