I am trying to calculate the difference between two LocalDate objects and the result seems to be off, but not every time.
I am using the Period construct. The below code shows one example which returns the expected result (I got that here) and another one which gives me the "wrong" result. I put that in quotes because I am not sure if that truly is wrong, or if the expected value is wrong. Note however, that if I use the online calculator from calculator.net, that gives me the result I expect.
public void manualTestPeriodBetween() {
//works fine - expected result obtained
LocalDate start = LocalDate.of(2014, 2, 14);
LocalDate end = LocalDate.of(2017, 8, 1);
Period result = Period.between(start, end);
Period expected = Period.of(3, 5, 18);
checkPeriods(expected, result);
//does not work as expected
start = LocalDate.of(2017, 5, 19);
end = LocalDate.of(2019, 7, 13);
result = Period.between(start, end);
expected = Period.of(2, 1, 25);
checkPeriods(expected, result);
}
private void checkPeriods(Period expected, Period result) {
System.out.println("expected Period = " + expected + ", resulting Period = " + result);
if (result.equals(expected)) {
System.out.println("SUCCESS - result Period matches expected");
} else {
System.out.println("FAIL - result Period not matched");
}
}
Output:
expected Period = P3Y5M18D, resulting Period = P3Y5M18D
SUCCESS - result Period matches expected
expected Period = P2Y1M25D, resulting Period = P2Y1M24D
FAIL - result Period not matched
Can someone help me figure out whether I am missing something or the expected result is wrong (both in my code and the online date calculator)? or maybe something else I am not even considering.
Here is a screenshot of the results obtained from the online date calculator:
The online date calculator you have provided may have been implemented incorrectly. From my favorite time resource, timeanddate
From and including: Friday, May 19, 2017
To, but not including Saturday, July 13, 2019
Result: 785 days
It is 785 days from the start date to the end date, but not including the end date.
Or 2 years, 1 month, 24 days excluding the end date.
Or 25 month, 24 days excluding the end date.
This is identical to the response provided by thecalculatorsite.com
I don't have the implementation for your online calculator, but the discrepancy appears to occur because May is a 31-day month.
It looks to be calculating the day-difference before the month difference, using some mathematical assumption for month length.
The Java method uses epochday to calculate the distances between two days in case of underflow, rather than adding an arbitrary value to offset a month.
if (totalMonths > 0 && days < 0) {
totalMonths--;
LocalDate calcDate = this.plusMonths(totalMonths);
days = (int) (end.toEpochDay() - calcDate.toEpochDay()); // safe
} else if (totalMonths < 0 && days > 0) {
totalMonths++;
days -= end.lengthOfMonth();
I mean, it should be 24 days depending on how you do the calculations.
If you add a month to get to june 19th, and june has 30 days- then it's only 24 days between the two dates.
https://www.wolframalpha.com/input/?i=days+between+5%2F19%2F2017+and+7%2F13%2F2019
The website you are checking it against is wrong, I can think of several ways that could happen depending on how they are implementing their date difference functions.
Related
This question already has answers here:
What would be a good implementation to get all Monday and Thursday dates Between a given date range (DateX and DateY) in JAVA
(3 answers)
Closed 5 years ago.
Can someone suggest the logic to find out the no. of Mondays between two dates in Java?
Instead of looping through all the days, is there any other approach to count the no. of occurrences of Mondays between two dates in java
There’s more than one way to go. Here’s a suggestion:
public static long noOfMondaysBetween(LocalDate first, LocalDate last) {
if (last.isBefore(first)) {
throw new IllegalArgumentException("first " + first + " was after last " + last);
}
// find first Monday in interval
LocalDate firstMonday = first.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
// similarly find last Monday
LocalDate lastMonday = last.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
// count
long number = ChronoUnit.WEEKS.between(firstMonday, lastMonday);
// add one to count both first Monday and last Monday in
return number + 1;
}
For example, noOfMondaysBetween(LocalDate.of(2017, Month.JUNE, 15), LocalDate.of(2017, Month.JUNE, 15)) returns 0. It may be a little subtle that the code takes this case into account: First Monday is June 19 and last is June 12. Count of weeks between the two Mondays is -1, so when I add 1, the result is 0, which is correct. To count the Mondays in June:
System.out.println(noOfMondaysBetween(LocalDate.of(2017, Month.MAY, 31), LocalDate.of(2017, Month.JULY, 1)));
Result:
4
If you intended to include the first date in the count (if it is a Monday), use nextOrSame(DayOfWeek.MONDAY) instead of next(DayOfWeek.MONDAY). Similarly to include the second date use previousOrSame(DayOfWeek.MONDAY).
I'm not a Java coder but I'm a coder. Here's how I'd solve this:
Count the days between the two dates (aka DATESPAN). I'm sure Java has a function for that.
Get the 'Day of Week' (AS A NUMBER, assuming that Monday = 1 )of both dates. I'm sure Java has a function for this too. We need to know if either is a Monday.
If DATESPAN < 7 Use this logic:
Answer = End Date Number > DATESPAN ? 0 : 1
IF DATESPAN >=7 CONTINUE TO GET ANSWER:
Divide the DATESPAN by 7.
If there is a remainder from the division, use the floor value of the quotient for the answer.
If there is NO remainder, check the start date and end date. If either are a Monday the quotient is the answer, If not the quotient - 1 is the answer
NOTE THIS IS NOT A DUPLICATE OF EITHER OF THE FOLLOWING
Calculating the difference between two Java date instances
calculate months between two dates in java [duplicate]
I have two dates:
Start date: "2016-08-31"
End date: "2016-11-30"
Its 91 days duration between the above two dates, I expected my code to return 3 months duration, but the below methods only returned 2 months. Does anyone have a better suggestion? Or do you guys think this is a bug in Java 8? 91 days the duration only return 2 months.
Thank you very much for the help.
Method 1:
Period diff = Period.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30"));
Method 2:
long daysBetween = ChronoUnit.MONTHS.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30"));
Method 3:
I tried to use Joda library instead of Java 8 APIs, it works. it loos will return 3, It looks like Java duration months calculation also used days value. But in my case, i cannot use the Joda at my project. So still looking for other solutions.
LocalDate dateBefore= LocalDate.parse("2016-08-31");
LocalDate dateAfter = LocalDate.parse("2016-11-30");
int months = Months.monthsBetween(dateBefore, dateAfter).getMonths();
System.out.println(months);
Since you don't care about the days in your case. You only want the number of month between two dates, use the documentation of the period to adapt the dates, it used the days as explain by Jacob. Simply set the days of both instance to the same value (the first day of the month)
Period diff = Period.between(
LocalDate.parse("2016-08-31").withDayOfMonth(1),
LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(diff); //P3M
Same with the other solution :
long monthsBetween = ChronoUnit.MONTHS.between(
LocalDate.parse("2016-08-31").withDayOfMonth(1),
LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(monthsBetween); //3
Edit from #Olivier Grégoire comment:
Instead of using a LocalDate and set the day to the first of the month, we can use YearMonth that doesn't use the unit of days.
long monthsBetween = ChronoUnit.MONTHS.between(
YearMonth.from(LocalDate.parse("2016-08-31")),
YearMonth.from(LocalDate.parse("2016-11-30"))
)
System.out.println(monthsBetween); //3
Since Java8:
ChronoUnit.MONTHS.between(startDate, endDate);
//Backward compatible with older Java
public static int monthsBetween(Date d1, Date d2){
if(d2==null || d1==null){
return -1;//Error
}
Calendar m_calendar=Calendar.getInstance();
m_calendar.setTime(d1);
int nMonth1=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
m_calendar.setTime(d2);
int nMonth2=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
return java.lang.Math.abs(nMonth2-nMonth1);
}
The documentation of Period#between states the following:
The start date is included, but the end date is not.
Furthermore:
A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
Your end day-of-month 30 is not greater than or equal to your start day-of-month 31, so a third month is not considered.
Note the parameter names:
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
To return 3 months, you can increment the endDateExclusive by a single day.
In case you want stick to java.time.Period API
As per java.time.Period documentation
Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
where
#param startDateInclusive the start date, inclusive, not null
#param endDateExclusive the end date, exclusive, not null
So it is better to adjust your implementation to make your end date inclusive and get your desired result
Period diff = Period.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30").plusDays(1));
System.out.println("Months : " + diff.getMonths());
//Output -> Months : 3
You have to be careful, never use LocalDateTime to calculate months between two dates the result is weird and incorrect, always use LocalDate !
here's is some code to prove the above:
package stack.time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class TestMonthsDateTime {
public static void main(String[] args) {
/**------------------Date Time----------------------------*/
LocalDateTime t1 = LocalDateTime.now();
LocalDateTime t2 = LocalDateTime.now().minusMonths(3);
long dateTimeDiff = ChronoUnit.MONTHS.between(t2, t1);
System.out.println("diff dateTime : " + dateTimeDiff); // diff dateTime : 2
/**-------------------------Date----------------------------*/
LocalDate t3 = LocalDate.now();
LocalDate t4 = LocalDate.now().minusMonths(3);
long dateDiff = ChronoUnit.MONTHS.between(t4, t3);
System.out.println("diff date : " + dateDiff); // diff date : 3
}
}
My 2%
This example checks to see if the second date is the end of that month. If it is the end of that month and if the first date of month is greater than the second month date it will know it will need to add 1
LocalDate date1 = LocalDate.parse("2016-08-31");
LocalDate date2 = LocalDate.parse("2016-11-30");
long monthsBetween = ChronoUnit.MONTHS.between(
date1,
date2);
if (date1.isBefore(date2)
&& date2.getDayOfMonth() == date2.lengthOfMonth()
&& date1.getDayOfMonth() > date2.getDayOfMonth()) {
monthsBetween += 1;
}
After the short investigation, still not totally fix my question, But I used a dirty solution to avoid return the incorrect duration. At least, we can get the reasonable duration months.
private static long durationMonths(LocalDate dateBefore, LocalDate dateAfter) {
System.out.println(dateBefore+" "+dateAfter);
if (dateBefore.getDayOfMonth() > 28) {
dateBefore = dateBefore.minusDays(5);
} else if (dateAfter.getDayOfMonth() > 28) {
dateAfter = dateAfter.minusDays(5);
}
return ChronoUnit.MONTHS.between(dateBefore, dateAfter);
}
The Java API response is mathematically accurate according to the calendar. But you need a similar mechanism, such as rounding decimals, to get the number of months between dates that matches the human perception of the approximate number of months between two dates.
Period period = Period.between(LocalDate.parse("2016-08-31"), LocalDate.parse("2016-11-30"));
long months = period.toTotalMonths();
if (period.getDays() >= 15) {
months++;
}
This question already has answers here:
What would be a good implementation to get all Monday and Thursday dates Between a given date range (DateX and DateY) in JAVA
(3 answers)
Closed 5 years ago.
Can someone suggest the logic to find out the no. of Mondays between two dates in Java?
Instead of looping through all the days, is there any other approach to count the no. of occurrences of Mondays between two dates in java
There’s more than one way to go. Here’s a suggestion:
public static long noOfMondaysBetween(LocalDate first, LocalDate last) {
if (last.isBefore(first)) {
throw new IllegalArgumentException("first " + first + " was after last " + last);
}
// find first Monday in interval
LocalDate firstMonday = first.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
// similarly find last Monday
LocalDate lastMonday = last.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
// count
long number = ChronoUnit.WEEKS.between(firstMonday, lastMonday);
// add one to count both first Monday and last Monday in
return number + 1;
}
For example, noOfMondaysBetween(LocalDate.of(2017, Month.JUNE, 15), LocalDate.of(2017, Month.JUNE, 15)) returns 0. It may be a little subtle that the code takes this case into account: First Monday is June 19 and last is June 12. Count of weeks between the two Mondays is -1, so when I add 1, the result is 0, which is correct. To count the Mondays in June:
System.out.println(noOfMondaysBetween(LocalDate.of(2017, Month.MAY, 31), LocalDate.of(2017, Month.JULY, 1)));
Result:
4
If you intended to include the first date in the count (if it is a Monday), use nextOrSame(DayOfWeek.MONDAY) instead of next(DayOfWeek.MONDAY). Similarly to include the second date use previousOrSame(DayOfWeek.MONDAY).
I'm not a Java coder but I'm a coder. Here's how I'd solve this:
Count the days between the two dates (aka DATESPAN). I'm sure Java has a function for that.
Get the 'Day of Week' (AS A NUMBER, assuming that Monday = 1 )of both dates. I'm sure Java has a function for this too. We need to know if either is a Monday.
If DATESPAN < 7 Use this logic:
Answer = End Date Number > DATESPAN ? 0 : 1
IF DATESPAN >=7 CONTINUE TO GET ANSWER:
Divide the DATESPAN by 7.
If there is a remainder from the division, use the floor value of the quotient for the answer.
If there is NO remainder, check the start date and end date. If either are a Monday the quotient is the answer, If not the quotient - 1 is the answer
For the following Period calculation:
Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 9, 2))
the result is:
P1M1D
This is equivalent to 31 days + 1 day = 32 days.
For this Period:
Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 10, 2))
the result is:
P2M1D
This is equivalent to: 31 days (in August) + 30 days (in September) + 1 (in October) = 62 days
Is there a method in the java.time package which will give the number of days in a Period? I can't find one. Not sure if I have overlooked anything or if it is just plain not there.
From the documentation:
To define an amount of time with date-based values (years, months,
days), use the Period class. The Period class provides various get
methods, such as getMonths, getDays, and getYears.To present the amount >of time measured in a single unit of time, such as days, you can use the
ChronoUnit.between method.
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months, and " + p.getDays() +
" days old. (" + p2 + " days total)");
The code produces output similar to the following:
You are 53 years, 4 months, and 29 days old. (19508 days total)
There is no way to do what you ask. The reason is that it is not possible from a Period to deduce the actual number of calendar days in the period. A Period is not tied to specific dates, once constructed in the way you show, it loses track of the actual calendar dates.
For example your first period represents a period of 1 month and 1 day. But the period does not care which month. It is simply a concept of "a month and a day".
If you need the number of days between two dates you should use ChronoUnit.DAYS.between as Saket Mittal writes.
There's a specific object depending at the amount of time you'd like to deal with.
This page here is very useful explaining which is best for your scenario.
The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds
LocalDate localDateStartDate = LocalDate.of(2016, 06, 10);
LocalDate localDateEndDate = LocalDate.of(2016,06,23);
long days = ChronoUnit.DAYS.between(localDateStartDate, localDateEndDate);
I have a Java problem where I need to check if an item has expired. This is supposed to check if the item is at least x (x is an integer and can be set to any integer value) months old.
Just to reclarify Supposing I have a pack of eggs, I want to check if it has been 1 months since I added them (dateAdded).
I wrote a simple comparison but it doesn't seem to give the correct response. Here is the code.
public Boolean isEndOfLine() {
Calendar today = Calendar.getInstance();
if(today.compareTo(dateAdded) >= END_OF_LINE) {
return true;
} else {
return false;
}
}
The value of end of line is an integer 12 i.e 12 months.
I do not hold javadoc in my head, but along the lines of:
dateAdded.add(Calendar.Month, END_OF_LINE).compareTo(today) > 0
Here's some similar example code, but using the Joda-Time 2.3 library.
FYI:
A Joda-Time DateTime instance knows its own time zone.
The minusMonths method is smart, handles Daylight Saving Time and other issues. You may want to read its source code to verify its logic follows your business rules as to what "x number of months ago" means.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
// Better to specify a time zone explicitly rather than rely on default.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html (not quite up-to-date, read page for details)
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
int countMonths = 2;
DateTime now = new DateTime( timeZone );
// If you want to include the entire day, get first moment of the day by calling "withTimeAtStartOfDay".
DateTime someMonthsAgo = now.minusMonths( countMonths ).withTimeAtStartOfDay();
DateTime dateAdded = new DateTime( 2013, 5, 6, 7, 8, 9, timeZone ); // Arbitrary values for example.
// If 'dateAdded' happened prior to our target date-time 'someMonthsAgo', the pack of eggs is expired.
Boolean isEndOfLine = dateAdded.isBefore( someMonthsAgo );
Dump to console…
System.out.println( "now: " + now );
System.out.println( "someMonthsAgo: " + someMonthsAgo );
System.out.println( "dateAdded: " + dateAdded );
System.out.println( "isEndOfLine: " + isEndOfLine );
When run…
now: 2014-01-08T21:36:11.179+01:00
someMonthsAgo: 2013-11-08T00:00:00.000+01:00
dateAdded: 2013-05-06T07:08:09.000+02:00
isEndOfLine: true
as mentioned in the Calendar docs
You should not rely on the number returned by compareTo - you just know that if it is greater than 0 that the original date is greater.
So create a new date (x months in the passed) and compare to that one.
The method returns 0 if the time represented by the argument is equal to the time represented by this Calendar object; or a value less than 0 if the time of this Calendar is before the time represented by the argument; or a value greater than 0 if the time of this Calendar is after the time represented.
import java.util.*;
public class CalendarDemo {
public static void main(String[] args) {
// create two calendar at the different dates
Calendar cal1 = new GregorianCalendar(2015, 8, 15);
Calendar cal2 = new GregorianCalendar(2008, 1, 02);
// compare the time values represented by two calendar objects.
int i = cal1.compareTo(cal2);
// return positive value if equals else return negative value
System.out.println("The result is :"+i);
// compare again but with the two calendars swapped
int j = cal2.compareTo(cal);
// return positive value if equals else return negative value
System.out.println("The result is :" + j);
}
}
Here is the working solution. Tested with JUNIT to confirm results.
public Boolean isEndOfLine() {
Calendar today = Calendar.getInstance();
today.add(Calendar.MONTH, -END_OF_LINE);
return today.compareTo(dateAdded) >= 0;
}
I subtracted the END_OF_LINE from today using the add method. Notice the minus on line 3. I then compared to see if it is greater than 0. Thanks for all your suggestions.