Java Date to Quarter getQuarterBounds(final Date date)

Here you can find the source of getQuarterBounds(final Date date)

Description

Returns an array of quarter bound dates of the year based on a specified date.

License

Open Source License

Parameter

Parameter Description
date the base date to work from

Return

The array of quarter bound dates

Declaration

private static Date[] getQuarterBounds(final Date date) 

Method Source Code

//package com.java2s;
/*//from w w  w .j  a v a 2 s.c o m
 * jGnash, a personal finance application
 * Copyright (C) 2001-2015 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.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.Objects;

public class Main {
    /**
     * ThreadLocal for a {@code GregorianCalendar}
     */
    private static final ThreadLocal<GregorianCalendar> gregorianCalendarThreadLocal = new ThreadLocal<GregorianCalendar>() {
        @Override
        protected GregorianCalendar initialValue() {
            return new GregorianCalendar();
        }
    };

    /**
     * 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 Date[] getQuarterBounds(final Date date) {
        Objects.requireNonNull(date);

        final GregorianCalendar c = gregorianCalendarThreadLocal.get();

        c.setTime(date);

        final int year = c.get(Calendar.YEAR);

        Date[] bounds = new Date[8];

        c.set(year, Calendar.JANUARY, 1, 0, 0, 0);
        bounds[0] = c.getTime();

        c.set(year, Calendar.MARCH, 31, 0, 0, 0);
        bounds[1] = c.getTime();

        c.set(year, Calendar.APRIL, 1, 0, 0, 0);
        bounds[2] = c.getTime();

        c.set(year, Calendar.JUNE, 30, 0, 0, 0);
        bounds[3] = c.getTime();

        c.set(year, Calendar.JULY, 1, 0, 0, 0);
        bounds[4] = c.getTime();

        c.set(year, Calendar.SEPTEMBER, 30, 0, 0, 0);
        bounds[5] = c.getTime();

        c.set(year, Calendar.OCTOBER, 1, 0, 0, 0);
        bounds[6] = c.getTime();

        c.set(year, Calendar.DECEMBER, 31, 0, 0, 0);
        bounds[7] = c.getTime();

        return bounds;
    }
}

Related

  1. getQuarter(Date date)
  2. getQuarter(Date date)
  3. getQuarter(Date date)
  4. getQuarter(Date date)
  5. getQuarterlyKey(Date d)
  6. getQuarterNum(Date date)
  7. getQuarterNum(Date dt)
  8. getQuarterOfDay(Date day)