get Current Quarterly Month Label - Java java.util

Java examples for java.util:Month

Description

get Current Quarterly Month Label

Demo Code


//package com.java2s;

import java.util.Calendar;
import java.util.Date;

import java.util.Locale;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getCurrentQuarterlyMonthLabel());
    }// w  w w  .j av a  2 s  .  c  om

    public static String getCurrentQuarterlyMonthLabel() {
        return getCurrentQuarterlyMonthLabel(new Date());
    }

    public static String getCurrentQuarterlyMonthLabel(Date date) {
        if (date == null) {
            return "";
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH);
            if (month > 0 && month <= 3) {
                calendar.set(Calendar.MONDAY, Calendar.JANUARY);
                String name = calendar.getDisplayName(Calendar.MONTH,
                        Calendar.LONG, Locale.getDefault());
                return name.substring(0, 1).toUpperCase()
                        + name.substring(1);
            } else if (month > 3 && month <= 6) {
                calendar.set(Calendar.MONDAY, Calendar.APRIL);
                String name = calendar.getDisplayName(Calendar.MONTH,
                        Calendar.LONG, Locale.getDefault());
                return name.substring(0, 1).toUpperCase()
                        + name.substring(1);
            } else if (month > 6 && month <= 9) {
                calendar.set(Calendar.MONDAY, Calendar.JULY);
                String name = calendar.getDisplayName(Calendar.MONTH,
                        Calendar.LONG, Locale.getDefault());
                return name.substring(0, 1).toUpperCase()
                        + name.substring(1);
            } else if (month > 9 && month <= 12) {
                calendar.set(Calendar.MONDAY, Calendar.OCTOBER);
                String name = calendar.getDisplayName(Calendar.MONTH,
                        Calendar.LONG, Locale.getDefault());
                return name.substring(0, 1).toUpperCase()
                        + name.substring(1);
            }
        }
        return null;
    }
}

Related Tutorials