Author: djones Date: Thu Mar 13 21:40:26 2014 New Revision: 1577332 URL: http://svn.apache.org/r1577332 Log: LANG-987: DateUtils.getFragmentInDays(Date, Calendar.MONTH) returns wrong days, reported by Jay Xu.
Modified: commons/proper/lang/trunk/src/changes/changes.xml commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DateUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java Modified: commons/proper/lang/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1577332&r1=1577331&r2=1577332&view=diff ============================================================================== --- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original) +++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Thu Mar 13 21:40:26 2014 @@ -22,6 +22,7 @@ <body> <release version="3.4" date="TBA" description="TBA"> + <action issue="LANG-987" type="fix" dev="djones">DateUtils.getFragmentInDays(Date, Calendar.MONTH) returns wrong days</action> <action issue="LANG-983" type="fix" dev="sebb">DurationFormatUtils does not describe format string fully</action> <action issue="LANG-981" type="fix" dev="sebb">DurationFormatUtils#lexx does not detect unmatched quote char</action> <action issue="LANG-984" type="fix" dev="sebb">DurationFormatUtils does not handle large durations correctly</action> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DateUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DateUtils.java?rev=1577332&r1=1577331&r2=1577332&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DateUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DateUtils.java Thu Mar 13 21:40:26 2014 @@ -1690,13 +1690,15 @@ public class DateUtils { final long millisPerUnit = getMillisPerUnit(unit); long result = 0; + int offset = (unit == Calendar.DAY_OF_YEAR) ? 0 : 1; + // Fragments bigger than a day require a breakdown to days switch (fragment) { case Calendar.YEAR: - result += ((calendar.get(Calendar.DAY_OF_YEAR) -1) * MILLIS_PER_DAY) / millisPerUnit; + result += ((calendar.get(Calendar.DAY_OF_YEAR) - offset) * MILLIS_PER_DAY) / millisPerUnit; break; case Calendar.MONTH: - result += ((calendar.get(Calendar.DAY_OF_MONTH) -1) * MILLIS_PER_DAY) / millisPerUnit; + result += ((calendar.get(Calendar.DAY_OF_MONTH) - offset) * MILLIS_PER_DAY) / millisPerUnit; break; default: break; Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java?rev=1577332&r1=1577331&r2=1577332&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DateUtilsFragmentTest.java Thu Mar 13 21:40:26 2014 @@ -561,4 +561,32 @@ testResult); / DateUtils.MILLIS_PER_HOUR, testResult); } + + @Test + public void testDaysOfMonthWithCalendar() throws Exception { + final long testResult = DateUtils.getFragmentInDays(aCalendar, Calendar.MONTH); + assertEquals(days, testResult); + } + + @Test + public void testDaysOfMonthWithDate() throws Exception { + final long testResult = DateUtils.getFragmentInDays(aDate, Calendar.MONTH); + final Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals(cal.get(Calendar.DAY_OF_MONTH), testResult); + } + + @Test + public void testDaysOfYearWithCalendar() throws Exception { + final long testResult = DateUtils.getFragmentInDays(aCalendar, Calendar.YEAR); + assertEquals(aCalendar.get(Calendar.DAY_OF_YEAR), testResult); + } + + @Test + public void testDaysOfYearWithDate() throws Exception { + final long testResult = DateUtils.getFragmentInDays(aDate, Calendar.YEAR); + final Calendar cal = Calendar.getInstance(); + cal.setTime(aDate); + assertEquals(cal.get(Calendar.DAY_OF_YEAR), testResult); + } }