Java: The First Monday of the Month

The following snippets calculates the next first Monday of the month:


import java.time.DayOfWeek;
import java.time.LocalDate;
import static java.time.temporal.TemporalAdjusters.*;

public class FirstMondayOfTheMonth {

    public static void main(String[] args) {
        var todayInAMonth = LocalDate.now().plusMonths(1);
        var firstMondayOfTheMonth = todayInAMonth.
                with(firstDayOfMonth()).
                with(next(DayOfWeek.MONDAY));
        System.out.printf("See you at next airhacks.tv: %s 8pm CET (UTC +1)",firstMondayOfTheMonth);
    }
}

Because its Java, you don't even have to compile it (Java 11 / JEP-330).

Just run: java FirstMondayOfTheMonth.java and you will get: "See you at next airhacks.tv: 2020-11-02 8pm CET (UTC +1)"

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License