Java Day getLastDayOfQuarter(Date date)

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

Description

get Last Day Of Quarter

License

Open Source License

Declaration

public static Date getLastDayOfQuarter(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 Date getLastDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);// w  w w  . java2s.  c  om
        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. getLastDay()
  2. getLastDay(Date date)
  3. getLastDay(Date date)
  4. getLastDay(Date date)
  5. getLastDay(String nowdate, String inFormat, String outFormat)
  6. getLastNDays(int aInN)
  7. getLastTimeInDay(Date day)