commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r653494 - in /commons/sandbox/me/trunk: src/org/apache/commons/me/util/DateHelper.java test-src/org/apache/commons/me/util/TestDateHelper.java
Date Mon, 05 May 2008 14:40:14 GMT
Author: nick
Date: Mon May  5 07:40:13 2008
New Revision: 653494

URL: http://svn.apache.org/viewvc?rev=653494&view=rev
Log:
New date utils method for getting the number of days covered between two date

Modified:
    commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java
    commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java

Modified: commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java?rev=653494&r1=653493&r2=653494&view=diff
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java (original)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/util/DateHelper.java Mon May  5 07:40:13
2008
@@ -45,6 +45,23 @@
 		c.setTime(date);
 		return c;
 	}
+
+	/**
+	 * Returns a copy of the given calendar, with the date 
+	 *  rolled backwards to midnight.
+	 * Even if you give us a calendar at midnight, you'll still
+	 *  get a copy back
+	 */
+	public static Calendar rollBackToMidnight(Calendar orig) {
+		Calendar c = Calendar.getInstance();
+		c.setTimeZone(orig.getTimeZone());
+		c.setTime(orig.getTime());
+		c.set(Calendar.HOUR_OF_DAY, 0);
+		c.set(Calendar.MINUTE, 0);
+		c.set(Calendar.SECOND, 0);
+		c.set(Calendar.MILLISECOND, 0);
+		return c;
+	}
 	
 	public static Calendar fromYMD(int[] ymd) {
 		if(ymd.length != 3) {
@@ -128,6 +145,38 @@
 		
 		return dt.toString();
 	}
+
+	/**
+	 * Returns the number of whole days, inclusive, between
+	 *  two dates.
+	 * Between a date and itself you get 1, between 2007-02-02
+	 *  and 2007-02-04 you get 3 etc.
+	 */
+	public static int daysCovered(int[] startYMD, int[] endYMD) {
+		return daysCovered(fromYMD(startYMD), fromYMD(endYMD));
+	}
+	/**
+	 * Returns the number of whole days, inclusive, between
+	 *  two dates.
+	 * Between a date and itself you get 1, between 2007-02-02
+	 *  and 2007-02-04 you get 3 etc.
+	 */
+	public static int daysCovered(Calendar start, Calendar end) {
+		// Roll both back to midnight, so it's a known point
+		long midnightStart = rollBackToMidnight(start).getTime().getTime();
+		long midnightEnd = rollBackToMidnight(end).getTime().getTime();
+
+		// Figure out the number of days, rounding down
+		int days = (int)(
+			(midnightEnd - midnightStart) / 24.0 / 60 / 60 / 1000);
+		
+		// Now add 1
+		if(days >= 0) {
+			return days + 1;
+		} else {
+			return days - 1;
+		}
+	}
 	
 	/**
 	 * Gets the date specified, in ISO date format

Modified: commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java?rev=653494&r1=653493&r2=653494&view=diff
==============================================================================
--- commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java (original)
+++ commons/sandbox/me/trunk/test-src/org/apache/commons/me/util/TestDateHelper.java Mon May
 5 07:40:13 2008
@@ -143,6 +143,50 @@
 		assertEquals(ymd[1], alt_ymd[1]);
 		assertEquals(ymd[2], alt_ymd[2]);
 	}
+
+	public void testDaysCovered() throws Exception {
+		Calendar c = Calendar.getInstance();
+		c.setTimeZone(TimeZone.getTimeZone("UTC"));
+
+		// 2009-02-13 23:31:30
+		c.setTimeInMillis(1234567890*1000l);
+		int[] y20090213 = DateHelper.toYMD(c);
+		// 2012-04-04 19:31:30
+		c.setTimeInMillis(1333567890*1000l);
+		int[] y20120404 = DateHelper.toYMD(c);
+		// 2012-04-16 09:18:10
+		c.setTimeInMillis(1334567890*1000l);
+		int[] y20120416 = DateHelper.toYMD(c);
+		
+		// Date and itself gives 1
+		assertEquals(1, DateHelper.daysCovered(c, c));
+		assertEquals(1, DateHelper.daysCovered(y20120416, y20120416));
+
+		// Same year, both not at midnight
+		assertEquals(13, DateHelper.daysCovered(y20120404, y20120416));
+		assertEquals(13, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20120404), c));
+		// Same year, both now at midnight
+		assertEquals(13, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20120404), DateHelper.fromYMD(y20120416)));
+
+		// Different years
+		assertEquals(365+365+366+16+31+4, DateHelper.daysCovered(y20090213, y20120404));
+		assertEquals(365+365+366+16+31+16, DateHelper.daysCovered(y20090213, y20120416));
+		assertEquals(365+365+366+16+31+4, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20090213), DateHelper.fromYMD(y20120404)));
+		assertEquals(365+365+366+16+31+16, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20090213), DateHelper.fromYMD(y20120416)));
+		assertEquals(365+365+366+16+31+16, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20090213), c));
+
+		// Backwards
+		assertEquals(-13, DateHelper.daysCovered(
+			DateHelper.fromYMD(y20120416), DateHelper.fromYMD(y20120404)));
+
+		// Check we haven't broken the calendars
+		assertEquals(1334567890*1000l, c.getTime().getTime());
+	}
 	
 	public void testFormatTime() throws Exception {
 		Calendar c = DateHelper.getCalendar();



Mime
View raw message