我如何创建一个 void 函数,在 13 日星期五列出下一个 13

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

伙计们,我想在 13 日创建一个即将到来的 13 个星期五的列表 我该怎么做?

我试了一年:

  public static void getFridayThirteen() {

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

但输出中没有任何内容。

您可以这样做的一种方法是:

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 声明为:

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