Java Data Type How to - Get the current quarter of the given date


We would like to know how to get the current quarter of the given date.


// w ww .  j  a  va 2 s .  c om
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;

public class Main {
  public static void main(String[] argv) {

   * 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:
      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;


The code above generates the following result.