简体   繁体   中英

How can I create a void function that lists next 13 Friday on 13th

Guys, i want to create a list of upcoming 13 Fridays on 13th how can I do this?

I try this for one Year:

  public static void getFridayThirteen() {

    for (int i = 1; i <= 365; i++) {
        if (Calendar.FRIDAY == 13) {
            fridayThirteen = i++;
            System.out.println("Test" + fridayThirteen);
        }
    }

but nothing appear in the output.

One way you can do this is:

LocalDate ld = LocalDate.now(); // or the LocalDate.now(ZoneId) overload
int count = 0;
// first set the date to the next Friday first...
ld = ld.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
// this will find 10 such dates
while (count < 10) {
    if (isFriday13(ld)) { // implementation shown below
        count++;
        System.out.println(ld);
    }
    ld = ld.plusDays(7); // this set ld to be the next Friday
}

isFriday13 is declare as:

private static boolean isFriday13(LocalDate ld) {
    return ld.getDayOfMonth() == 13 && ld.getDayOfWeek() == DayOfWeek.FRIDAY;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM