Get the date of week in current month : Kotlin [cl

2019-09-30 07:30发布

问题:

How to get the date of week in current month?

Example this month is September, so the output will be 2,9,16,23,30

var calendar = Calendar.getInstance()
var dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)

I only able to get 2.

回答1:

You can use the following code for that:

List<LocalDate> mondayDates(YearMonth month) {
    List<LocalDate> firstDaysOfWeeks = new ArrayList<>();
    LocalDate day = month.atDay(1).with(DayOfWeek.MONDAY);
    while (!day.isAfter(month.atEndOfMonth())) {
        firstDaysOfWeeks.add(day);
        day = day.plusWeeks(1);
    }
    return firstDaysOfWeeks;
}