LANG-987: DateUtils.getFragmentInDays(Date, Calendar.MONTH) returns wrong days, reported by Jay Xu.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1577332 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Duncan Jones 2014-03-13 21:40:26 +00:00
parent 66a37174a0
commit 80bd3fdb42
3 changed files with 33 additions and 2 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.4" date="TBA" description="TBA"> <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-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-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> <action issue="LANG-984" type="fix" dev="sebb">DurationFormatUtils does not handle large durations correctly</action>

View File

@ -1690,13 +1690,15 @@ public class DateUtils {
final long millisPerUnit = getMillisPerUnit(unit); final long millisPerUnit = getMillisPerUnit(unit);
long result = 0; long result = 0;
int offset = (unit == Calendar.DAY_OF_YEAR) ? 0 : 1;
// Fragments bigger than a day require a breakdown to days // Fragments bigger than a day require a breakdown to days
switch (fragment) { switch (fragment) {
case Calendar.YEAR: 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; break;
case Calendar.MONTH: 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; break;
default: default:
break; break;

View File

@ -561,4 +561,32 @@ testResult);
/ DateUtils.MILLIS_PER_HOUR, / DateUtils.MILLIS_PER_HOUR,
testResult); 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);
}
} }