Example usage for java.time.temporal TemporalAdjusters firstInMonth

List of usage examples for java.time.temporal TemporalAdjusters firstInMonth

Introduction

In this page you can find the example usage for java.time.temporal TemporalAdjusters firstInMonth.

Prototype

public static TemporalAdjuster firstInMonth(DayOfWeek dayOfWeek) 

Source Link

Document

Returns the first in month adjuster, which returns a new date in the same month with the first matching day-of-week.

Usage

From source file:Main.java

public static void main(String[] args) {
    Month month = Month.valueOf("March".toUpperCase());

    System.out.printf("For the month of %s:%n", month);
    LocalDate date = Year.now().atMonth(month).atDay(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    Month mi = date.getMonth();/*w w  w. j  ava 2s .co  m*/
    while (mi == month) {
        System.out.printf("%s%n", date);
        date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        mi = date.getMonth();
    }
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, Month.JULY, 16);
    System.out.println(date);//  w w  w .jav  a2 s.c  om

    LocalDate firstDayOfJuly = date.with(TemporalAdjusters.firstDayOfMonth()); // 2014-07-01
    System.out.println(firstDayOfJuly);

    LocalDate dateOfFirstMonday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2014-07-07
    System.out.println(dateOfFirstMonday);

}

From source file:ListMondays.java

public static void main(String[] args) {
    Month month = null;//ww  w.j a v a2s.c om

    if (args.length < 1) {
        System.out.printf("Usage: ListMondays <month>%n");
        throw new IllegalArgumentException();
    }

    try {
        month = Month.valueOf(args[0].toUpperCase());
    } catch (IllegalArgumentException exc) {
        System.out.printf("%s is not a valid month.%n", args[0]);
        throw exc; // Rethrow the exception.
    }

    System.out.printf("For the month of %s:%n", month);
    LocalDate date = Year.now().atMonth(month).atDay(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    Month mi = date.getMonth();
    while (mi == month) {
        System.out.printf("%s%n", date);
        date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        mi = date.getMonth();
    }
}

From source file:Main.java

@Override
public Temporal adjustInto(Temporal input) {
    LocalDate date = LocalDate.from(input);
    LocalDate nextMonth = date.plusMonths(1);
    LocalDate firstTuesdayInNextMonth = nextMonth.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY));
    return input.with(firstTuesdayInNextMonth);
}