Returns the current quarter of the given date - Java java.time

Java examples for java.time:Quarter

Description

Returns the current quarter of the given date

Demo Code


//package com.java2s;

import java.time.LocalDate;

import java.time.Month;

import java.time.temporal.ChronoField;

public class Main {
    /**/*ww w.  ja  va 2 s .co m*/
     * Returns the current quarter of the given date
     * 
     * @return int (0 .. 3)
     * @param cal
     *            Given date, cannot be null
     */
    public static int getQuarter(LocalDate cal) {
        int month = cal.get(ChronoField.MONTH_OF_YEAR);
        switch (Month.of(month)) {
        case JANUARY:
        case FEBRUARY:
        case MARCH:
        default:
            return 0;
        case APRIL:
        case MAY:
        case JUNE:
            return 1;
        case JULY:
        case AUGUST:
        case SEPTEMBER:
            return 2;
        case OCTOBER:
        case NOVEMBER:
        case DECEMBER:
            return 3;
        }
    }
}

Related Tutorials