Java Utililty Methods Calendar Calculate

List of utility methods to do Calendar Calculate

Description

The list of methods to do Calendar Calculate are organized into topic(s).

Method

StringcalculateDuration(Calendar para_c1, Calendar para_c2)
calculate Duration
long seconds = (para_c2.getTimeInMillis() - para_c1.getTimeInMillis()) / 1000;
return String.format("%02d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60));
intcalculateHourDiff(Calendar cal1, Calendar cal2)
calculate Hour Diff
int mil2 = cal2.get(Calendar.MILLISECOND);
int mil1 = cal1.get(Calendar.MILLISECOND);
int sec2 = cal2.get(Calendar.SECOND);
int sec1 = cal1.get(Calendar.SECOND);
int min2 = cal2.get(Calendar.MINUTE);
int min1 = cal1.get(Calendar.MINUTE);
cal2.set(Calendar.MINUTE, 0);
cal1.set(Calendar.MINUTE, 0);
...
intcalculateMinimalDaysInFirstWeek(Calendar calendar)
calculate Minimal Days In First Week
Calendar temp = (Calendar) calendar.clone();
temp.set(Calendar.DAY_OF_YEAR, 1);
int firstDayOfJan = temp.get(Calendar.DAY_OF_WEEK);
return 8 - firstDayOfJan;
intcalculateMonthsInBetween(Calendar startPoint, Calendar endPoint)
returns number of months from date of startPoint till date of endPoint
 1. 
int numMonths = 0;
Calendar tempCalendar = (Calendar) startPoint.clone();
int compare = startPoint.getTime().compareTo(endPoint.getTime());
if (compare <= 0) {
    while (!isSameMonthOfYear(tempCalendar, endPoint)) {
        incrementOneMonth(tempCalendar);
        numMonths++;
} else {
    while (!isSameMonthOfYear(tempCalendar, endPoint)) {
        decrementOneMonth(tempCalendar);
        numMonths--;
return numMonths;
longcalculateRangeInDays(Calendar start, Calendar end)
calculate Range In Days
long diff = end.getTimeInMillis() - start.getTimeInMillis();
return diff / (1000 * 60 * 60 * 24);
intcalculateWeeksInBetween(Calendar startPoint, Calendar endPoint)
returns number of weeks from date of startPoint till date of endPoint
 1. 
int numWeeks = 0;
Calendar tempCalendar = (Calendar) startPoint.clone();
int compare = startPoint.getTime().compareTo(endPoint.getTime());
if (compare <= 0) {
    while (!isSameWeekOfYear(tempCalendar, endPoint)) {
        incrementOneWeek(tempCalendar);
        numWeeks++;
} else {
    while (!isSameWeekOfYear(tempCalendar, endPoint)) {
        decrementOneWeek(tempCalendar);
        numWeeks--;
return numWeeks;
CalendarcalDateOnly(Calendar iniCal)
This method set the hour of day, minute, second and millisecond value of the calendar give in argument to 0
Calendar cal = iniCal;
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
IterablecalendarIterator(final int iterateType, final Calendar startCal, final Calendar endCal)
calendar Iterator
return new Iterable<Calendar>() {
    final Calendar workCal = (Calendar) startCal.clone();
    public Iterator<Calendar> iterator() {
        return new Iterator<Calendar>() {
            public boolean hasNext() {
                return !workCal.after(endCal);
            public Calendar next() {
...
CalendarcloneCalendar(Calendar in)
{ method
GregorianCalendar then = new GregorianCalendar(in.get(Calendar.YEAR), in.get(Calendar.MONTH),
        in.get(Calendar.DATE), in.get(Calendar.HOUR_OF_DAY), in.get(Calendar.MINUTE),
        in.get(Calendar.SECOND));
return (then);
CalendarcopyCalendar(final Calendar calendar, final boolean stripTime)
copy Calendar
final Calendar c = Calendar.getInstance();
c.setTime(calendar.getTime());
if (stripTime) {
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
return c;