← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17512: DateUtils, max support method

 

------------------------------------------------------------
revno: 17512
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-11-17 19:13:25 +0100
message:
  DateUtils, max support method
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-15 09:24:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/DateUtils.java	2014-11-17 18:13:25 +0000
@@ -116,6 +116,23 @@
 
         return date != null ? format.format( date ) : null;
     }
+    
+    /**
+     * Returns the latest of the two given dates.
+     * 
+     * @param date1 the first date.
+     * @param date2 the second date.
+     * @return the latest of the two given dates.
+     */
+    public static Date max( Date date1, Date date2 )
+    {
+        if ( date1 == null )
+        {
+            return date2 != null ? date2 : null;
+        }
+        
+        return date2 != null ? ( date1.before( date2 ) ? date1 : date2 ) : date1;        
+    }
 
     /**
      * Formats a Date to the format YYYY-MM-DD.

=== 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-15 09:24:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/DateUtilsTest.java	2014-11-17 18:13:25 +0000
@@ -31,8 +31,11 @@
 import static org.hisp.dhis.system.util.DateUtils.dateIsValid;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Date;
+
 import org.joda.time.DateTime;
 import org.junit.Test;
 
@@ -65,4 +68,20 @@
     {
         assertEquals( 6, DateUtils.daysBetween( new DateTime( 2014, 3, 1, 0, 0 ).toDate(), new DateTime( 2014, 3, 7, 0, 0 ).toDate() ) );
     }
+    
+    @Test
+    public void testMax()
+    {
+        Date date1 = new DateTime( 2014, 5, 15, 3, 3 ).toDate();
+        Date date2 = new DateTime( 2014, 5, 18, 1, 1 ).toDate();
+        Date date3 = null;
+        Date date4 = null;
+        
+        assertEquals( date1, DateUtils.max( date1, date2 ) );
+        assertEquals( date1, DateUtils.max( date2, date1 ) );
+        assertEquals( date1, DateUtils.max( date1, date3 ) );
+        assertEquals( date1, DateUtils.max( date3, date1 ) );
+        
+        assertNull( DateUtils.max( date3, date4 ) );
+    }
 }