Calculate days between two dates ignoring year

allanshivji :

I have lists of marriage dates in "LocalDate" type and I want to find the list of people whose anniversary is in next 30 days. All of the marriage dates are from past years like 1980, 1990, 2000...

I have tried to use ChronoUnit.DAYS.between() function but it only shows the number of days if the date is of todays and future day.

String str = "2019-04-24";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(str, formatter);
LocalDate today = LocalDate.now();          
long dd = ChronoUnit.DAYS.between(today, date);

The answer that I'm expecting is that if the marriage date is like 1990-04-29, then it should show me if this date is within the next 30 days or not. Meaning, if the anniversary of the above date is within next 30 days or not.

MadProgrammer :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134163&siteId=1