Java Date Before getQuarterStartDateBeforeCurrent(int num, Date current)

Here you can find the source of getQuarterStartDateBeforeCurrent(int num, Date current)

Description

get Quarter Start Date Before Current

License

Apache License

Declaration

public static long getQuarterStartDateBeforeCurrent(int num, Date current) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.Date;

public class Main {

    public static long getQuarterStartDateBeforeCurrent(int num, Date current) {
        Calendar calCurrent = Calendar.getInstance();
        calCurrent.setTime(current);/*from   ww w  . j  a  v  a 2 s.c  om*/

        int currentMonth = calCurrent.get(Calendar.MONTH) + 1;

        Calendar latestQuarter = Calendar.getInstance();

        switch (currentMonth) {
        // delete by lijun begin
        // case 1 :
        // case 2 :
        // case 3 : {latestQuarter.set(Calendar.YEAR,
        // calCurrent.get(Calendar.YEAR) - 1);
        // latestQuarter.set(Calendar.MONTH, Calendar.OCTOBER);
        // break;}
        // case 4 :
        // case 5 :
        // case 6 : {latestQuarter.set(Calendar.YEAR,
        // calCurrent.get(Calendar.YEAR));
        // latestQuarter.set(Calendar.MONTH, Calendar.JANUARY);
        // break;}
        // case 7 :
        // case 8 :
        // case 9 : {latestQuarter.set(Calendar.YEAR,
        // calCurrent.get(Calendar.YEAR));
        // latestQuarter.set(Calendar.MONTH, Calendar.APRIL);
        // break;}
        // case 10 :
        // case 11 :
        // case 12 : {latestQuarter.set(Calendar.YEAR,
        // calCurrent.get(Calendar.YEAR));
        // latestQuarter.set(Calendar.MONTH, Calendar.JULY);
        // break;}
        // delete by lijun end

        // add by lijun begin
        case 1:
        case 2:
        case 3: {
            latestQuarter.set(Calendar.YEAR, calCurrent.get(Calendar.YEAR));
            latestQuarter.set(Calendar.MONTH, Calendar.JANUARY);
            break;
        }
        case 4:
        case 5:
        case 6: {
            latestQuarter.set(Calendar.YEAR, calCurrent.get(Calendar.YEAR));
            latestQuarter.set(Calendar.MONTH, Calendar.APRIL);
            break;
        }
        case 7:
        case 8:
        case 9: {
            latestQuarter.set(Calendar.YEAR, calCurrent.get(Calendar.YEAR));
            latestQuarter.set(Calendar.MONTH, Calendar.JULY);
            break;
        }
        case 10:
        case 11:
        case 12: {
            latestQuarter.set(Calendar.YEAR, calCurrent.get(Calendar.YEAR));
            latestQuarter.set(Calendar.MONTH, Calendar.OCTOBER);
            break;
        }
        // add by lijun end
        }

        latestQuarter.set(Calendar.DAY_OF_MONTH, 1);
        latestQuarter.set(Calendar.HOUR_OF_DAY, 0);
        latestQuarter.set(Calendar.MINUTE, 0);
        latestQuarter.set(Calendar.SECOND, 0);
        latestQuarter.set(Calendar.MILLISECOND, 0);

        // delete by lijun begin
        // Calendar calQuarter = Calendar.getInstance();
        // calQuarter.set(Calendar.YEAR, latestQuarter.get(Calendar.YEAR) - (num
        // - 1)/4);
        //
        // if ((latestQuarter.get(Calendar.MONTH) + 1 - (num - 1)%4*3) > 0) {
        // calQuarter.set(Calendar.MONTH, latestQuarter.get(Calendar.MONTH) -
        // (num - 1)%4*3);
        // } else {
        // calQuarter.set(Calendar.MONTH, Calendar.JANUARY);
        // }
        //
        // calQuarter.set(Calendar.DAY_OF_MONTH, 1);
        // calQuarter.set(Calendar.HOUR_OF_DAY, 0);
        // calQuarter.set(Calendar.MINUTE, 0);
        // calQuarter.set(Calendar.SECOND, 0);
        //
        //
        // return calQuarter.getTimeInMillis();
        // delete by lijun end

        // add by lijun begin
        int subtractMonthNum = num * 3;

        latestQuarter.add(Calendar.MONTH, -subtractMonthNum);

        return latestQuarter.getTimeInMillis();
        // add by lijun end
    }
}

Related

  1. getMonthBefore(Date currentDate, int numberMonthBefore)
  2. getMonthStartDateBeforeCurrent(int monthNum, Date current)
  3. getOneHourBefore(Date date)
  4. getPassedMonthBetweenDate(Date before, Date after)
  5. getPreviousDate(String before)
  6. getSomeDaysBeforeAfter(Date date, int days)
  7. isAtLeastOneDayBefore(Date daybefore, Date dayafter)
  8. isBefore(Date date)
  9. isBefore(Date date1, Date date2)