← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19328: Applied patch from TW: updates DateUtils.parseDate to use thread safe date parsing

 

------------------------------------------------------------
revno: 19328
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-06-10 08:03:19 +0700
message:
  Applied patch from TW: updates DateUtils.parseDate to use thread safe date parsing
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.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	2015-04-24 16:07:03 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2015-06-10 01:03:19 +0000
@@ -44,8 +44,7 @@
 import org.joda.time.DateTime;
 import org.joda.time.Days;
 import org.joda.time.Months;
-import org.joda.time.format.PeriodFormatter;
-import org.joda.time.format.PeriodFormatterBuilder;
+import org.joda.time.format.*;
 
 /**
  * @author Lars Helge Overland
@@ -53,18 +52,21 @@
  */
 public class DateUtils
 {
-    public static final SimpleDateFormat[] SUPPORTED_DATE_FORMATS = new SimpleDateFormat[]{
-        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ),
-        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" ),
-        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss" ),
-        new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm" ),
-        new SimpleDateFormat( "yyyy-MM-dd'T'HH" ),
-        new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" ),
-        new SimpleDateFormat( "yyyy-MM-dd" ),
-        new SimpleDateFormat( "yyyy-MM" ),
-        new SimpleDateFormat( "yyyy" )
+    private static final DateTimeParser[] SUPPORTED_DATE_FORMAT_PARSERS = {
+            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ssZ").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
+            DateTimeFormat.forPattern("yyyy-MM").getParser(),
+            DateTimeFormat.forPattern("yyyy").getParser()
     };
 
+    private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
+            .append( null, SUPPORTED_DATE_FORMAT_PARSERS ).toFormatter();
+
     private static final String SEP = ", ";
     
     public static final PeriodFormatter DAY_SECOND_FORMAT = new PeriodFormatterBuilder()
@@ -617,17 +619,6 @@
             return null;
         }
 
-        for ( SimpleDateFormat format : SUPPORTED_DATE_FORMATS )
-        {
-            try
-            {
-                return format.parse( dateString );
-            }
-            catch ( ParseException ignored )
-            {
-            }
-        }
-
-        return null;
+        return DATE_TIME_FORMATTER.parseDateTime( dateString ).toDate();
     }
 }