Java Day Between getDiffBetweenQuarter(Date latestDate, Date current)

Here you can find the source of getDiffBetweenQuarter(Date latestDate, Date current)

Description

get Diff Between Quarter

License

Apache License

Declaration

public static int getDiffBetweenQuarter(Date latestDate, 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 int getDiffBetweenQuarter(Date latestDate, Date current) {
        Calendar calCurrent = Calendar.getInstance();
        calCurrent.setTime(current);/*w  w w  .  ja va2s.  c o  m*/

        Calendar calLatest = Calendar.getInstance();
        calLatest.setTime(latestDate);

        int latestQuarterNum = getQuarterNum(latestDate);
        int currentQuarterNum = getQuarterNum(current);

        if (calCurrent.get(Calendar.YEAR) - calLatest.get(Calendar.YEAR) > 0) {
            int diff = calCurrent.get(Calendar.YEAR) - calLatest.get(Calendar.YEAR);
            return (diff - 1) * 4 + (currentQuarterNum - 1) + (4 - latestQuarterNum + 1);
        } else {
            return currentQuarterNum - latestQuarterNum;
        }
    }

    private static int getQuarterNum(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int currentMonth = cal.get(Calendar.MONTH) + 1;
        switch (currentMonth) {
        case 1:
        case 2:
        case 3:
            return 1;
        case 4:
        case 5:
        case 6:
            return 2;
        case 7:
        case 8:
        case 9:
            return 3;
        case 10:
        case 11:
        case 12:
            return 4;
        }

        return 0;
    }
}

Related

  1. getDaysBetween(Date startDate, Date endDate)
  2. getDaysBetween(Date startDate, Date endDate)
  3. getDaysBetweenDate(Date begin, Date end)
  4. getDaysDiff(Date startDate, Date endDate)
  5. getDaysDifference(final Date begin, final Date end)
  6. getDiffDays(Date date1, Date date2)
  7. getDiffDays(String startDate, String endDate)
  8. getDifferDays(Date d1, Date d2)
  9. getDifferDays(String date1, String date2)