From 978650618d5b021bc1f382eb6e24207521567369 Mon Sep 17 00:00:00 2001 From: snowleo Date: Tue, 31 May 2011 20:36:43 +0000 Subject: [PATCH] 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 --- Essentials/src/com/earth2me/essentials/Util.java | 6 ++++-- Essentials/test/com/earth2me/essentials/UtilTest.java | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 81447d8e9..faed72ae6 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -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; } diff --git a/Essentials/test/com/earth2me/essentials/UtilTest.java b/Essentials/test/com/earth2me/essentials/UtilTest.java index 712d15cfe..4e7949448 100644 --- a/Essentials/test/com/earth2me/essentials/UtilTest.java +++ b/Essentials/test/com/earth2me/essentials/UtilTest.java @@ -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() {