如何在Java 8中使用每个月的LocalDate来获取每个月可用的星期五数量?

huangapple go评论97阅读模式
英文:

how to get how many Friday available in each month using each month of LocalDate in java 8?

问题

我想要每个月可用的总星期五的数量。如何获得?

  1. 代码
private static int getFridayCount(DayOfWeek dayOfWeek, LocalDate startDate) {
    return // 
}
英文:

I want count of total Friday available in each month. How to get that?

1.code

private static int getFridayCount(DayOfWeek dayOfWeek, LocalDate startDate) {
    return // 
}

答案1

得分: 4

可能不是最快的方法,但我觉得这个非常简单易懂:

public static int numberOfDaysOfWeekInMonth(DayOfWeek dow, YearMonth yearMonth) {
    LocalDate startOfMonth = yearMonth.atDay(1);
    LocalDate first = startOfMonth.with(TemporalAdjusters.firstInMonth(dow));
    LocalDate last = startOfMonth.with(TemporalAdjusters.lastInMonth(dow));
    return (last.getDayOfMonth() - first.getDayOfMonth()) / 7 + 1;
}

示例用法:

System.out.println(
    numberOfDaysOfWeekInMonth(
        DayOfWeek.FRIDAY, YearMonth.of(2020, 9)
    )
); // 输出 4
英文:

Probably not the fastest, but I find this really simple to understand:

public static int numberOfDaysOfWeekInMonth(DayOfWeek dow, YearMonth yearMonth) {
    LocalDate startOfMonth = yearMonth.atDay(1);
    LocalDate first = startOfMonth.with(TemporalAdjusters.firstInMonth(dow));
    LocalDate last = startOfMonth.with(TemporalAdjusters.lastInMonth(dow));
    return (last.getDayOfMonth() - first.getDayOfMonth()) / 7 + 1;
}

Example usage:

System.out.println(
    numberOfDaysOfWeekInMonth(
        DayOfWeek.FRIDAY, YearMonth.of(2020, 9)
    )
); // outputs 4

答案2

得分: 1

你可以从startDate到月底计算每个dayOfWeek的次数,如下所示:

import java.time.*;

public class Main {
    public static void main(String[] args) {
        DayOfWeek dow = DayOfWeek.FRIDAY;
        LocalDate startDate = LocalDate.of(2020, 9 ,25);
        System.out.println("Amount: " + getCountOfDayInMonth(dow, startDate));
    }

    private static int getCountOfDayInMonth(DayOfWeek dow, LocalDate startDate) {
        LocalDate date = startDate;
        int count = 0;
        while (date.getMonth() == startDate.getMonth()) {
            if (date.getDayOfWeek() == dow) {
                count++;
            }
            date = date.plusDays(1);
        }
        return count;
    }
}

其中date是从startDate开始,持续到当前月份的LocalDate

英文:

You may count every dayOfWeek from startDate to end of month like

import java.time.*;

public class Main {
    public static void main(String[] args) {
        DayOfWeek dow = DayOfWeek.FRIDAY;
        LocalDate startDate = LocalDate.of(2020, 9 ,25);
        System.out.println("Amount: " + getCountOfDayInMonth(dow, startDate));
    }

    private static int getCountOfDayInMonth(DayOfWeek dow, LocalDate startDate) {
        LocalDate date = startDate;
        int count = 0;
        while (date.getMonth() == startDate.getMonth()) {
            if (date.getDayOfWeek() == dow) {
                count++;
            }
            date = date.plusDays(1);
        }
        return count;
    }
}

Where date is a LocalDate beginning at startDate and going through the current Month.

huangapple
  • 本文由 发表于 2020年9月25日 14:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64059276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定