Calculating Days Between Two Dates With Java

Calculate days between two Dates in Java 8

If you want logical calendar days, use DAYS.between() method from java.time.temporal.ChronoUnit:

LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);

If you want literal 24 hour days, (a duration), you can use the Duration class instead:

LocalDate today = LocalDate.now()
LocalDate yesterday = today.minusDays(1);
// Duration oneDay = Duration.between(today, yesterday); // throws an exception
Duration.between(today.atStartOfDay(), yesterday.atStartOfDay()).toDays() // another option

For more information, refer to this document.

Java, Calculate the number of days between two dates

Note: this answer was written in 2011. I would recommend using java.time now instead of Joda Time.

Well to start with, you should only deal with them as strings when you have to. Most of the time you should work with them in a data type which actually describes the data you're working with.

I would recommend that you use Joda Time, which is a much better API than Date/Calendar. It sounds like you should use the LocalDate type in this case. You can then use:

int days = Days.daysBetween(date1, date2).getDays();

Calculating days between two dates in JAVA (catch ParseException error)

Don't use Date. Try this.

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String date1 = "11/11/2020";
String date2 = "13/11/2020";

LocalDate d1 = LocalDate.parse(date1,dtf);
LocalDate d2 = LocalDate.parse(date2,dtf);

long ndays = d1.datesUntil(d2).count();
System.out.println(ndays);

Difference in days between two dates in Java?

I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date past = new Date(110, 5, 20); // June 20th, 2010
Date today = new Date(110, 6, 24); // July 24th
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); // => 34

Calculate no of days between two dates in java

Reason is, you are not subtracting two dates with same time format.

Use Calendar class to change the time as 00:00:00 for both date and you will get exact difference in days.

Date createdDate = new Date();
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, 0);
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
createdDate = time.getTime();

More explaination in Jim Garrison' answer

Calculate days between two dates ignoring year

So, the "basic" concept is, you want to take your list of dates and change the year to match this year. The "catch" is, if the resulting date is before today, you should increment the year by one, so that if it's December now, you will catch all the anniversaries which occur in January.

Maybe something like...

LocalDate now = LocalDate.now();
int year = now.getYear();
List<LocalDate> dates = ...;
List<LocalDate> adjusted = new ArrayList<>(10);
for (LocalDate date : dates) {
LocalDate warped = date.withYear(year);
if (warped.isBefore(now)) {
warped = warped.withYear(year + 1);
}
adjusted.add(warped);
}

Then you would simply check to see if the dates fall within your required range...

LocalDate limit = now.plusDays(30);
for (LocalDate date : adjusted) {
if ((date.isAfter(now) || date.isEqual(now)) && (date.isBefore(limit) || date.isEqual(limit))) {
System.out.println("~~ " + date);
}
}

So, with same pseudo, randomly generated date, I can get a result which looks something like...

Input date 2019-04-19
+---------------+---------------+--------------+
| Original Date | Adjusted Date | Within range |
+---------------+---------------+--------------+
| 1996-04-13 | 2020-04-13 | |
| 1986-04-24 | 2019-04-24 | X |
| 1989-04-23 | 2019-04-23 | X |
| 1960-05-11 | 2019-05-11 | X |
| 1986-05-18 | 2019-05-18 | X |
| 1984-04-06 | 2020-04-06 | |
| 1997-05-29 | 2019-05-29 | |
| 2008-03-31 | 2020-03-31 | |
| 2014-04-18 | 2020-04-18 | |
| 1982-04-23 | 2019-04-23 | X |
+---------------+---------------+--------------+

And if we change the anchor date to something like 2019-12-20, it could generate something like...

+---------------+---------------+--------------+
| Original Date | Adjusted Date | Within range |
+---------------+---------------+--------------+
| 2001-12-16 | 2020-12-16 | |
| 2005-12-28 | 2019-12-28 | X |
| 1988-12-31 | 2019-12-31 | X |
| 1989-11-13 | 2020-11-13 | |
| 1976-11-13 | 2020-11-13 | |
| 1991-01-09 | 2020-01-09 | X |
| 1963-11-04 | 2020-11-04 | |
| 2001-11-02 | 2020-11-02 | |
| 1980-01-11 | 2020-01-11 | X |
| 1979-11-17 | 2020-11-17 | |
+---------------+---------------+--------------+

So it's capturing the dates which land in next year.

nb: I randomly generate my test date to be within +/- one month of the anchor date so I would get better test data.



Related Topics



Leave a reply



Submit