Java Month of Year getLastMonthOfQuater(Date date)

Here you can find the source of getLastMonthOfQuater(Date date)

Description

get Last Month Of Quater

License

Open Source License

Declaration


public static int getLastMonthOfQuater(Date date) 

Method Source Code


//package com.java2s;
import java.text.ParseException;

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

public class Main {

    public static int getLastMonthOfQuater(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);/*from w w  w  .j av  a  2s .c om*/
        Date lastDayOfQuarter = getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
        int quarter = getQuarterOfYear(date);
        calendar.setTime(lastDayOfQuarter);
        // System.out.println(quarter);
        // System.out.println(lastDayOfQuarter);
        int i = calendar.get(Calendar.MONTH) + 1;
        return i;
    }

    public static Date getLastDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getLastDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
    }

    public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 3 - 1;
        } else if (quarter == 2) {
            month = 6 - 1;
        } else if (quarter == 3) {
            month = 9 - 1;
        } else if (quarter == 4) {
            month = 12 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getLastDayOfMonth(year, month);
    }

    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }

    public static Date getLastDayOfMonth(Date date) throws ParseException {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }
}

Related

  1. getLastMonth()
  2. getLastMonthAndCycle()
  3. getLastMonthdate()
  4. getLastMonthDate()
  5. getLastMonthDate(int date)
  6. getLastMonthOfYear()
  7. getMaxDayOfMonth(int year, int month)
  8. getMonth(int week, int year)
  9. getMonthDays(Object year, Object month)