Java LocalDate Calculate getLastDayOfTheQuarter(final LocalDate date)

Here you can find the source of getLastDayOfTheQuarter(final LocalDate date)

Description

Returns a LocalDate date representing the last day of the quarter based on a specified date.

License

Open Source License

Parameter

Parameter Description
date the base date to work from

Return

The last day of the quarter specified

Declaration

public static LocalDate getLastDayOfTheQuarter(final LocalDate date) 

Method Source Code

//package com.java2s;
/*/*www.  j av a2s .  co  m*/
 * jGnash, a personal finance application
 * Copyright (C) 2001-2019 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.time.LocalDate;

import java.time.Month;

import java.time.temporal.TemporalAdjusters;

import java.util.Objects;

public class Main {
    private static final int DAYS_PER_WEEK = 7;

    /**
     * Returns a {@code LocalDate} date representing the last day of the quarter based on
     * a specified date.
     *
     * @param date the base date to work from
     * @return The last day of the quarter specified
     */
    public static LocalDate getLastDayOfTheQuarter(final LocalDate date) {
        Objects.requireNonNull(date);

        LocalDate result;

        LocalDate[] bounds = getQuarterBounds(date);

        if (date.compareTo(bounds[2]) < 0) {
            result = bounds[1];
        } else if (date.compareTo(bounds[4]) < 0) {
            result = bounds[3];
        } else if (date.compareTo(bounds[6]) < 0) {
            result = bounds[5];
        } else {
            result = bounds[DAYS_PER_WEEK];
        }

        return result;
    }

    /**
     * Returns an array of quarter bound dates of the year based on a specified
     * date. The order is q1s, q1e, q2s, q2e, q3s, q3e, q4s, q4e.
     *
     * @param date the base date to work from
     * @return The array of quarter bound dates
     */
    private static LocalDate[] getQuarterBounds(final LocalDate date) {
        Objects.requireNonNull(date);

        final LocalDate[] bounds = new LocalDate[8];

        bounds[0] = date.with(TemporalAdjusters.firstDayOfYear());
        bounds[1] = date.withMonth(Month.MARCH.getValue()).with(TemporalAdjusters.lastDayOfMonth());
        bounds[2] = date.withMonth(Month.APRIL.getValue()).with(TemporalAdjusters.firstDayOfMonth());
        bounds[3] = date.withMonth(Month.JUNE.getValue()).with(TemporalAdjusters.lastDayOfMonth());
        bounds[4] = date.withMonth(Month.JULY.getValue()).with(TemporalAdjusters.firstDayOfMonth());
        bounds[5] = date.withMonth(Month.SEPTEMBER.getValue()).with(TemporalAdjusters.lastDayOfMonth());
        bounds[6] = date.withMonth(Month.OCTOBER.getValue()).with(TemporalAdjusters.firstDayOfMonth());
        bounds[DAYS_PER_WEEK] = date.with(TemporalAdjusters.lastDayOfYear());

        return bounds;
    }
}

Related

  1. getDateCriteria(LocalDate startDate, LocalDate endDate)
  2. getDateStart(LocalDate localDate)
  3. getEndDateOfMonth(LocalDate date)
  4. getFirstDayOfTheMonth(final LocalDate date)
  5. getLastDayInMonth(LocalDate localDate)
  6. getLocalDateBydayAndWeek(LocalDate localDate, int weekNumber, int dayNumber)
  7. getLrsLocalDate(String lbdcDate)
  8. getMax(@Nonnull final LocalDate aDate1, @Nonnull final LocalDate aDate2)
  9. getNextNoWeekedDay(LocalDate today)