How do I discover the Quarter of a given Date?

Since Java 8, the quarter is accessible as a field using classes in the java.time package.

import java.time.LocalDate;
import java.time.temporal.IsoFields;

LocalDate myLocal = LocalDate.now();
quarter = myLocal.get(IsoFields.QUARTER_OF_YEAR);

In older versions of Java, you could use:

import java.util.Date;

Date myDate = new Date();
int quarter = (myDate.getMonth() / 3) + 1;

Be warned, though that getMonth was deprecated early on:

As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

Instead you could use a Calendar object like this:

import java.util.Calendar;
import java.util.GregorianCalendar;

Calendar myCal = new GregorianCalendar();
int quarter = (myCal.get(Calendar.MONTH) / 3) + 1;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)