Fix: 1 day displayed additionally on 31th of month

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1549 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-05-31 20:36:43 +00:00
parent 26e81106b3
commit 978650618d
2 changed files with 7 additions and 2 deletions

View file

@ -97,15 +97,17 @@ public class Util
}
private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
{
{
int diff = 0;
long savedDate = fromDate.getTimeInMillis();
while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate)))
{
savedDate = fromDate.getTimeInMillis();
fromDate.add(type, future ? 1 : -1);
diff++;
}
diff--;
fromDate.add(type, future ? -1 : 1);
fromDate.setTimeInMillis(savedDate);
return diff;
}

View file

@ -93,6 +93,9 @@ public class UtilTest extends TestCase
a = new GregorianCalendar(2010, 9, 17, 23, 45, 45);
b = new GregorianCalendar(2015, 3, 7, 10, 0, 0);
assertEquals(" 4 years 5 months 20 days 10 hours 14 minutes 15 seconds", Util.formatDateDiff(a, b));
a = new GregorianCalendar(2011, 4, 31, 10, 0, 0);
b = new GregorianCalendar(2011, 4, 31, 10, 5, 0);
assertEquals(" 5 minutes", Util.formatDateDiff(a, b));
}
public void testFDDpast() {