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

CalendaraddQuarter(Calendar calendar, int quarterDelta)
Add quarter to current
int month = calendar.get(Calendar.MONTH);
int quarter = month / 3;
Calendar startMonth = new GregorianCalendar(calendar.get(Calendar.YEAR), quarter * 3, 1);
startMonth.add(Calendar.MONTH, quarterDelta * 3);
return startMonth;
longaddTime(int calendarType, long time, int number)
add Time
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.add(calendarType, number);
return cal.getTimeInMillis();
CalendaralignHours(int interval, Calendar timestamp)
Aligns the time fields to the start of given interval.
int value = timestamp.get(Calendar.HOUR_OF_DAY);
value = value - (value % interval);
timestamp.set(Calendar.HOUR_OF_DAY, value);
return alignHour(timestamp);
CalendaralignSecond(Calendar timestamp)
Aligns the time fields to the start of the second.
timestamp.set(Calendar.MILLISECOND, 0);
return timestamp;
Stringcal2Str(Calendar pCal)

Gets string representation of given Calendar.

StringBuffer str = new StringBuffer();
if (pCal != null) {
    int month = pCal.get(Calendar.MONTH) + 1;
    int date = pCal.get(Calendar.DATE);
    int year = pCal.get(Calendar.YEAR);
    str.append(month);
    str.append("/" + date);
    str.append("/" + year);
...
intcalcAge(Calendar dateOfBirth, Calendar now)
Calculates the age if date of birth is given (for a calendar time stamp)
int age = now.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
int nowM = now.get(Calendar.MONTH);
int dobM = dateOfBirth.get(Calendar.MONTH);
int nowDOM = now.get(Calendar.DAY_OF_MONTH);
int dobDOM = dateOfBirth.get(Calendar.DAY_OF_MONTH);
if ((nowM < dobM) || ((nowM == dobM) && (nowDOM < dobDOM))) {
    age--;
if (age < 0) {
    throw new IllegalArgumentException("Calculated age results in negative value.");
return age;
IntegercalcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)
calc Difference As Days
long tDifference = aTargetDate.getTimeInMillis() - aBaseDate.getTimeInMillis();
return (int) (tDifference / 86400000);
intcalcDifferenceAsYearsPrivate(Calendar aBaseDate, Calendar aTargetDate)
calc Difference As Years Private
int tBaseDateMonth = aBaseDate.get(Calendar.MONTH);
int tTargetDateMonth = aTargetDate.get(Calendar.MONTH);
return aTargetDate.get(Calendar.YEAR) - aBaseDate.get(Calendar.YEAR)
        - (tBaseDateMonth > tTargetDateMonth || (tBaseDateMonth == tTargetDateMonth
                && aBaseDate.get(Calendar.DATE) > aTargetDate.get(Calendar.DATE)) ? 1 : 0);
longcalcTime(Date end, Date start, int calendarField)
calc Time
if (end == null || start == null)
    return 0;
long diff = end.getTime() - start.getTime();
switch (calendarField) {
case Calendar.MILLISECOND:
    return diff;
case Calendar.SECOND:
    return diff / 1000;
...
StringcalcTimezone(Calendar cal)
calc Timezone
Date date = cal.getTime();
TimeZone z = cal.getTimeZone();
int tz = z.getRawOffset();
if (z.inDaylightTime(date)) {
    int tzDst = z.getDSTSavings();
    tz = tz + tzDst;
String sign = "+";
...