List of utility methods to do Calendar Calculate
Calendar | addQuarter(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; |
long | addTime(int calendarType, long time, int number) add Time Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.add(calendarType, number);
return cal.getTimeInMillis();
|
Calendar | alignHours(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); |
Calendar | alignSecond(Calendar timestamp) Aligns the time fields to the start of the second. timestamp.set(Calendar.MILLISECOND, 0);
return timestamp;
|
String | cal2Str(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); ... |
int | calcAge(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; |
Integer | calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate) calc Difference As Days long tDifference = aTargetDate.getTimeInMillis() - aBaseDate.getTimeInMillis(); return (int) (tDifference / 86400000); |
int | calcDifferenceAsYearsPrivate(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); |
long | calcTime(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; ... |
String | calcTimezone(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 = "+"; ... |