How do I discover the Quarter of a given Date?

前端 未结 14 1114
小鲜肉
小鲜肉 2020-12-24 05:37

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.

14条回答
  •  既然无缘
    2020-12-24 06:21

    You are going to have to write your own code because the term "Quarter" is different for each business. Can't you just do something like:

    Calendar c = /* get from somewhere */
    int month = c.get(Calendar.MONTH);
    
    return (month >= Calendar.JANUARY && month <= Calendar.MARCH)     ? "Q1" :
           (month >= Calendar.APRIL && month <= Calendar.JUNE)        ? "Q2" :
           (month >= Calendar.JULY && month <= Calendar.SEPTEMBER)    ? "Q3" :
                                                                        "Q4";
    

提交回复
热议问题