Here you can find the source of getDiffBetweenQuarter(Date latestDate, Date current)
public static int getDiffBetweenQuarter(Date latestDate, Date current)
//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; } }