Allain Lalonde Allain Lalonde - 3 months ago 15
Java Question

How do I discover the Quarter of a given Date?

Given a java.util.Date object how do I go about finding what Quarter it's in?

Assuming Q1 = Jan Feb Mar, Q2 = Apr, May, Jun, etc.

Answer

You could use

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

Be warned, though that getMonth is deprecated:

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

Comments