Applying the fix and the test patches from LANG-346 - fixes bugs in DateUtils.round() for minutes and seconds. Patch from Dave Meikle

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@564053 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2007-08-09 00:24:59 +00:00
parent 2dfca4a424
commit 050fe8e916
2 changed files with 69 additions and 6 deletions

View File

@ -640,18 +640,18 @@ public class DateUtils {
int millisecs = val.get(Calendar.MILLISECOND);
if (!round || millisecs < 500) {
time = time - millisecs;
if (field == Calendar.SECOND) {
done = true;
}
}
if (field == Calendar.SECOND) {
done = true;
}
// truncate seconds
int seconds = val.get(Calendar.SECOND);
if (!done && (!round || seconds < 30)) {
time = time - (seconds * 1000L);
if (field == Calendar.MINUTE) {
done = true;
}
}
if (field == Calendar.MINUTE) {
done = true;
}
// truncate minutes

View File

@ -692,6 +692,69 @@ public class DateUtilsTest extends TestCase {
dateTimeParser.setTimeZone(defaultZone);
}
/**
* Tests the Changes Made by LANG-346 to the DateUtils.modify() private method invoked
* by DateUtils.round().
*/
public void testRoundLang346() throws Exception
{
TimeZone.setDefault(defaultZone);
dateTimeParser.setTimeZone(defaultZone);
Calendar testCalendar = Calendar.getInstance();
testCalendar.set(2007, 6, 2, 8, 8, 50);
Date date = testCalendar.getTime();
assertEquals("Minute Round Up Failed",
dateTimeParser.parse("July 2, 2007 08:09:00.000"),
DateUtils.round(date, Calendar.MINUTE));
testCalendar.set(2007, 6, 2, 8, 8, 20);
date = testCalendar.getTime();
assertEquals("Minute No Round Failed",
dateTimeParser.parse("July 2, 2007 08:08:00.000"),
DateUtils.round(date, Calendar.MINUTE));
testCalendar.set(2007, 6, 2, 8, 8, 50);
testCalendar.set(Calendar.MILLISECOND, 600);
date = testCalendar.getTime();
assertEquals("Second Round Up with 600 Milli Seconds Failed",
dateTimeParser.parse("July 2, 2007 08:08:51.000"),
DateUtils.round(date, Calendar.SECOND));
testCalendar.set(2007, 6, 2, 8, 8, 50);
testCalendar.set(Calendar.MILLISECOND, 200);
date = testCalendar.getTime();
assertEquals("Second Round Down with 200 Milli Seconds Failed",
dateTimeParser.parse("July 2, 2007 08:08:50.000"),
DateUtils.round(date, Calendar.SECOND));
testCalendar.set(2007, 6, 2, 8, 8, 20);
testCalendar.set(Calendar.MILLISECOND, 600);
date = testCalendar.getTime();
assertEquals("Second Round Up with 200 Milli Seconds Failed",
dateTimeParser.parse("July 2, 2007 08:08:21.000"),
DateUtils.round(date, Calendar.SECOND));
testCalendar.set(2007, 6, 2, 8, 8, 20);
testCalendar.set(Calendar.MILLISECOND, 200);
date = testCalendar.getTime();
assertEquals("Second Round Down with 200 Milli Seconds Failed",
dateTimeParser.parse("July 2, 2007 08:08:20.000"),
DateUtils.round(date, Calendar.SECOND));
testCalendar.set(2007, 6, 2, 8, 8, 50);
date = testCalendar.getTime();
assertEquals("Hour Round Down Failed",
dateTimeParser.parse("July 2, 2007 08:00:00.000"),
DateUtils.round(date, Calendar.HOUR));
testCalendar.set(2007, 6, 2, 8, 31, 50);
date = testCalendar.getTime();
assertEquals("Hour Round Up Failed",
dateTimeParser.parse("July 2, 2007 09:00:00.000"),
DateUtils.round(date, Calendar.HOUR));
}
/**
* Tests various values with the trunc method
*/