Java Day of getFirstDayOfQuarter(Date date)

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

Description

get First Day Of Quarter

License

Open Source License

Declaration

public static Date getFirstDayOfQuarter(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 getFirstDayOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//from ww  w .  j a  va  2  s  .  c o  m
        return getFirstDayOfQuarter(calendar.get(Calendar.YEAR), getQuarterOfYear(date));
    }

    public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 1 - 1;
        } else if (quarter == 2) {
            month = 4 - 1;
        } else if (quarter == 3) {
            month = 7 - 1;
        } else if (quarter == 4) {
            month = 10 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getFirstDayOfMonth(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 getFirstDayOfMonth(Date date) throws ParseException {

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

    public static Date getFirstDayOfMonth(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);
        return calendar.getTime();
    }
}

Related

  1. getEndDayofMouth(String date, String inPattern, String outPattern)
  2. getFirstDay(boolean isNeedHH24MISS)
  3. getFirstDay(boolean withTime)
  4. getFirstDay(Date date)
  5. getFirstDayInThisQuarter(String nowDate)
  6. getFirstOfDay(Date date)
  7. getFirstTimeInDay(Date day)
  8. getFormattedNowDatePlusDays(int days, String dateFormat)
  9. getFullAge(Date birthday)