Java Date to Quarter getQuarter(Date date)

Here you can find the source of getQuarter(Date date)

Description

Get quarter to the date.

License

Open Source License

Parameter

Parameter Description
date a parameter

Declaration

public static int getQuarter(Date date) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**// ww w  .j  a v a 2  s  . com
     * Get quarter to the date.
     * 
     * @param date
     * @return
     */
    public static int getQuarter(Date date) {
        int month = getMonth(date);
        int quarter = -1;
        switch (month) {
        case 1:
        case 2:
        case 3:
            quarter = 1;
            break;
        case 4:
        case 5:
        case 6:
            quarter = 2;
            break;
        case 7:
        case 8:
        case 9:
            quarter = 3;
            break;
        case 10:
        case 11:
        case 12:
            quarter = 4;
            break;
        default:
            break;
        }
        return quarter;
    }

    /**
     * Get month to the date.
     * 
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
}

Related

  1. getQuarter(Date date)
  2. getQuarter(Date date)
  3. getQuarter(Date date)
  4. getQuarterBounds(final Date date)
  5. getQuarterlyKey(Date d)
  6. getQuarterNum(Date date)
  7. getQuarterNum(Date dt)