Example usage for java.time.temporal TemporalAdjusters firstDayOfNextMonth

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

Introduction

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

Prototype

public static TemporalAdjuster firstDayOfNextMonth() 

Source Link

Document

Returns the "first day of next month" adjuster, which returns a new date set to the first day of the next month.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 25); // 2014-02-25

    // first day of next month (2014-03-01)
    LocalDate firstDayOfNextMonth = date.with(TemporalAdjusters.firstDayOfNextMonth());

    System.out.println(firstDayOfNextMonth);
}

From source file:Main.java

public static void main(String[] args) {
    TemporalQuery<Integer> daysBeforeChristmas = new TemporalQuery<Integer>() {

        public int daysTilChristmas(int acc, Temporal temporal) {
            int month = temporal.get(ChronoField.MONTH_OF_YEAR);
            int day = temporal.get(ChronoField.DAY_OF_MONTH);
            int max = temporal.with(TemporalAdjusters.lastDayOfMonth()).get(ChronoField.DAY_OF_MONTH);
            if (month == 12 && day <= 25)
                return acc + (25 - day);
            return daysTilChristmas(acc + (max - day + 1),
                    temporal.with(TemporalAdjusters.firstDayOfNextMonth()));
        }//ww  w. ja  v  a  2s.  c o m

        @Override
        public Integer queryFrom(TemporalAccessor temporal) {
            if (!(temporal instanceof Temporal))
                throw new RuntimeException("Temporal accessor must be of type Temporal");
            return daysTilChristmas(0, (Temporal) temporal);
        }
    };

    System.out.println(LocalDate.of(2013, 12, 26).query(daysBeforeChristmas)); // 364
    System.out.println(LocalDate.of(2013, 12, 23).query(daysBeforeChristmas)); // 2
    System.out.println(LocalDate.of(2013, 12, 25).query(daysBeforeChristmas)); // 0
    System.out.println(ZonedDateTime.of(2013, 12, 1, 11, 0, 13, 938282, ZoneId.of("America/Los_Angeles"))
            .query(daysBeforeChristmas)); // 24
}