user1292124
user1292124

Reputation: 55

How to get customized quarter of the year in Java?

The requirement is:

Completed in Q1 2012 – Jan 1st to and including April 1st. (for Q1-2012 only)

Completed in Q2 2012 – April 2nd to and including July 1st

Completed in Q3 2012 – July 2nd to and including Oct 1st

Completed in Q4 2012 – Oct 2nd to Jan 1st (next year)

So far: Calendar cal Calendar.getInstance(Locale.US);

cal.setTime(date);

int month = cal.get(Calendar.MONTH);

int quarter = (month / 3) + 1;

But how do I get to include the April 1st in the first quarter and so on? That's where I turn blank.

I'm a bit at lost on how to do this and I'm stuck at it for a while now. Any help will be greatly appreciated. Thanks.

Upvotes: 1

Views: 1482

Answers (2)

alessandro
alessandro

Reputation: 19

You can try this

  Public String getQuarter(month){
       return "Q-" + ((int) (month-1)/3+1)
    }

Upvotes: 0

maialithar
maialithar

Reputation: 3123

the simplest idea would be to check if cal.get(Calendar.DAY_OF_MONTH) equals 1 and cal.get(Calendar.MONTH) equals 4, 7 or 10

Upvotes: 3

Related Questions