Как вывести все результаты цикла в одной строке

Хочу сразу уточнить, что это задача в обучении. Условие:
Создайте в начале метода две переменные класса LocalDate — birthday и today. После этого напишите цикл, в котором добавляйте один год к birthday и сравнивайте получившуюся дату с сегодняшней, используя метод isAfter или isBefore. Таким образом у вас будет проверяться не только год, но и день, что позволит точно выводить данные о прошедших днях рождениях. Для переноса текста на новую строку используйте символ переноса строки, который возвращается методом.

System.lineSeparator():
String text = text + "Строка с датой" + System.lineSeparator();

public class Birthdays {

Я на сколько мог написал цикл, и могу вывести в консоль все значения по одному c помощь SOUT, но такое решение не проходит проверку, т.к. я понимаю все значения необходимо вывести через return с помощью отредактированной строки. P.S. коллекции и пр. вещи еще не проходил ...

public static void main(String[] args) {

    int day = 1;
    int month = 01;
    int year = 1990;

    System.out.println(collectBirthdays(year, month, day));

}

public static String collectBirthdays(int year, int month, int day) {

    LocalDate birthDay = LocalDate.of(year, month, day);
    LocalDate today = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy - EEE", Locale.US);
    int a = 0;

    while (birthDay.isBefore(today)) {
        //System.out.println(i + " - " + formatter.format(birthDay));
        a += 1;
        birthDay = birthDay.plusYears(1);
        if (birthDay.isAfter(today)) {
            break;
        }
    }

    return "";

Ответы (2 шт):

Автор решения: Дмитрий

Все почти так, как вы и сделали. надо лишь немного подправить:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Birthdays {
    
    private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy - EEE", Locale.US);

    public static void main(String[] args) {
        System.out.println(collectBirthdays(1, 1, 1990));
    }

    public static String collectBirthdays(int day, int month, int year) {        
        StringBuilder sb = new StringBuilder();
        LocalDate birthDay = LocalDate.of(year, month, day);
        LocalDate today = LocalDate.now();
        int i = 0;
        while (birthDay.isBefore(today)) {
            String text = FORMATTER.format(birthDay);
            sb.append(i++).append(" - ").append(text).append(System.lineSeparator());
            birthDay = birthDay.plusYears(1);
        }

        return sb.toString();
    }

}
→ Ссылка
Автор решения: Kiben

Думаю, догадаться далее, что вставить в return, уже сможете

LocalDate birthday = LocalDate.of(year, month, day);
    LocalDate today = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyy - E", Locale.ENGLISH);

    int i = 0;
    String str = "";

    while (birthday.isBefore(today) || today.isEqual(birthday)) {
        str += i + " - " + birthday.format(formatter) + System.lineSeparator();
        birthday = birthday.plusYears(1);
        i++;
    }
→ Ссылка