Get every year for a period in java

I am trying to get a LocalDate instance for every year in the period. For example, for this:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(2011, Month.DECEMBER, 19);
Period period = Period.between(birthday, today);

I want to 2012-12-19, 2013-12-19, 2014-12-19, 2015-12-19. Given the methods Period, this is not possible. Is there any way around this? Can another method be used?

+4
source share
2 answers

You can try this using Java 8;

    LocalDate start = LocalDate.of(2011, Month.DECEMBER, 19);
    LocalDate end = LocalDate.now();
    while (!start.isAfter(end)) {
        System.out.println(start);
        start = start.plusYears(1);
    }
}
+2
source

. "" . Soorapadman , (19 ), , - 29 . , 28-, 29 , .

, . 31 28 ( 29), 28 28 . , , , .

:

public List<LocalDate> datesBetween(LocalDate start, LocalDate end, Period period);
  List<LocalDate> list = new ArrayList<>();
  int multiplier = 1;
  LocalDate current = start;
  while (!current.isAfter(end)) {
    current = start.plus(period.multipliedBy(multiplier);
    list.add(current);
    multiplier++;
  }
  return list;
}

, . . .

+4

Source: https://habr.com/ru/post/1620850/


All Articles