Example usage for java.time.temporal TemporalAdjusters lastDayOfMonth

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

Introduction

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

Prototype

public static TemporalAdjuster lastDayOfMonth() 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) {
    DayOfWeek d = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek();
    System.out.println(d);/*from w  w  w  .  jav  a 2 s.  com*/
}

From source file:Main.java

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

    // last day of February 2014 (2014-02-28)
    LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(lastDayOfMonth);
}

From source file:Main.java

public static void main(String[] argv) {
    List<DayOfWeek> list = new ArrayList<>();

    for (Month month : Month.values()) {
        DayOfWeek day = LocalDate.now().withYear(2010).with(month).with(TemporalAdjusters.lastDayOfMonth())
                .getDayOfWeek();//from   w  w w .  ja va2  s.  c o m

        list.add(day);
    }

    System.out.println(list);
}

From source file:Main.java

public static void main(String[] argv) {
    List<DayOfWeek> list = new ArrayList<>();

    list = (List<DayOfWeek>) Stream.of(Month.values()).map(month -> LocalDate.now().withYear(2010).with(month)
            .with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek()).collect(Collectors.toList());

    System.out.println(list);/*w w  w.j  a va2  s.  co m*/
}

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()));
        }/*w  ww  .  ja  v  a2s  .  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
}

From source file:Main.java

public static void main(String[] args) {
    TemporalAdjuster temporalAdjuster = (Temporal t) -> t.plus(Period.ofDays(10));

    System.out.println(temporalAdjuster);

    TemporalAdjuster fourMinutesFromNow = temporal -> temporal.plus(4, ChronoUnit.MINUTES);

    LocalTime localTime1 = LocalTime.of(12, 0, 0);
    System.out.println(localTime1.with(temporal -> temporal.plus(4, ChronoUnit.MINUTES)));

    System.out.println(Instant.now().with(temporalAdjuster));

    LocalDate localDate1 = LocalDate.of(2013, 12, 13);
    System.out.println(localDate1.with(TemporalAdjusters.lastDayOfMonth()));

}

From source file:Main.java

/**
 * The adjustInto method accepts a Temporal instance
 * and returns an adjusted LocalDate. If the passed in
 * parameter is not a LocalDate, then a DateTimeException is thrown.
 *///  ww w. j a va 2  s.c  om
public Temporal adjustInto(Temporal input) {
    LocalDate date = LocalDate.from(input);
    int day;
    if (date.getDayOfMonth() < 15) {
        day = 15;
    } else {
        day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
    }
    date = date.withDayOfMonth(day);
    if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
        date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
    }

    return input.with(date);
}

From source file:com.github.drbookings.LocalDates.java

public static LocalDate getLastDayOfMonth(final LocalDate date) {
    return date.with(TemporalAdjusters.lastDayOfMonth());
}

From source file:jgnash.ui.report.compiled.MonthlyAccountBalanceChart.java

private TimeSeriesCollection createTimeSeriesCollection(final Account account) {
    List<LocalDate> dates = Collections.emptyList();

    if (subAccountCheckBox.isSelected()) {
        // Getting the dates to calculate
        final LocalDate start = startDateField.getLocalDate().with(TemporalAdjusters.firstDayOfMonth());
        final LocalDate stop = endDateField.getLocalDate().with(TemporalAdjusters.lastDayOfMonth());

        dates = DateUtils.getLastDayOfTheMonths(start, stop);
        TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"),
                rb.getString("Column.Balance"));

        // For every month, calculate the total amount
        for (LocalDate date : dates) {
            final LocalDate d = date.with(TemporalAdjusters.lastDayOfMonth());
            final LocalDate s = date.with(TemporalAdjusters.firstDayOfMonth());

            // Get the total amount for the account and every sub accounts for the specified date
            // and include it in the chart
            t.add(new Month(DateUtils.asDate(date)), calculateTotal(s, d, account, account.getCurrencyNode()));
        }//from w w  w .java2  s  . c om

        return new TimeSeriesCollection(t);
    }

    int count = account.getTransactionCount();

    if (count > 0) {
        LocalDate start = account.getTransactionAt(0).getLocalDate();
        LocalDate stop = account.getTransactionAt(count - 1).getLocalDate();
        dates = DateUtils.getLastDayOfTheMonths(start, stop);
    }

    TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"),
            rb.getString("Column.Balance"));

    AccountType type = account.getAccountType();

    for (LocalDate localDate : dates) {
        // get balance for the whole month
        LocalDate d = localDate.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate s = localDate.with(TemporalAdjusters.firstDayOfMonth());

        BigDecimal balance = AccountBalanceDisplayManager.convertToSelectedBalanceMode(type,
                account.getBalance(s, d));
        t.add(new Month(DateUtils.asDate(localDate)), balance);
    }

    return new TimeSeriesCollection(t);
}

From source file:jgnash.ui.report.compiled.MonthlyAccountBalanceChartCompare.java

private TimeSeriesCollection createTimeSeriesCollection(final Account account, final Account a2) {
    //always use this method
    //if (subAccountCheckBox.isApproved()) {
    // Getting the dates to calculate
    LocalDate start = startDateField.getLocalDate().with(TemporalAdjusters.firstDayOfMonth());
    LocalDate stop = endDateField.getLocalDate().with(TemporalAdjusters.lastDayOfMonth());

    List<LocalDate> list = DateUtils.getLastDayOfTheMonths(start, stop);

    TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"),
            rb.getString("Column.Balance"));
    TimeSeries t2 = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"),
            rb.getString("Column.Balance"));

    // For every month, calculate the total amount
    for (final LocalDate localDate : list) {
        final LocalDate d = localDate.with(TemporalAdjusters.lastDayOfMonth());
        final LocalDate s = localDate.with(TemporalAdjusters.firstDayOfMonth());

        // Get the total amount for the account and every sub accounts
        // for the specified date
        //BigDecimal bd_TotalAmount = calculateTotal(s, d, account, account.getCurrencyNode());
        BigDecimal bd_TotalAmount = calculateTotal(s, d, account, subAccountCheckBox.isSelected(),
                account.getCurrencyNode());

        // Include it in the graph
        t.add(new Month(DateUtils.asDate(localDate)), totalModulus(bd_TotalAmount, account.getAccountType()));
        if (jcb_compare.isSelected()) {
            bd_TotalAmount = calculateTotal(s, d, a2, subAccountCheckBox.isSelected(),
                    account.getCurrencyNode());
            t2.add(new Month(DateUtils.asDate(localDate)), totalModulus(bd_TotalAmount, a2.getAccountType()));
        }/*from w  w  w  .j  av a  2 s . c  om*/
    }

    TimeSeriesCollection tsc = new TimeSeriesCollection();
    tsc.addSeries(t);
    if (jcb_compare.isSelected()) {
        tsc.addSeries(t2);
    }

    return tsc;
    /*
        return new TimeSeriesCollection(t);
    }
            
    int count = account.getTransactionCount();
            
    if (count > 0) {
        Date start = account.getTransactionAt(0).getDate();
        Date stop = account.getTransactionAt(count - 1).getDate();
        list = DateUtils.getLastDayOfTheMonths(start, stop);
    }
            
    TimeSeries t = new TimeSeries(rb.getString("Column.Month"), rb.getString("Column.Month"), rb.getString("Column.Balance"));
            
    AccountType type = account.getAccountType();
            
    for (Date aList : list) {
        // get balance for the whole month
        Date d = DateUtils.getLastDayOfTheMonth(aList);
        Date s = DateUtils.getFirstDayOfTheMonth(aList);
            
        BigDecimal balance = AccountBalanceDisplayManager.convertToSelectedBalanceMode(type, account.getBalance(s, d));
        t.add(new Month(aList), balance);
    }
            
    return new TimeSeriesCollection(t);
    */
}