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

booleanisAfterWorkTime(Calendar start)
Determines whether or not the date is after the start of the work day
final int hourOfDay = start.get(Calendar.HOUR_OF_DAY);
return hourOfDay >= END_WORKDAY;
booleanisInWorkTime(Calendar start)
Determines whether or not the time of the date is within the work day
return !isBeforeWorkTime(start) && !isAfterWorkTime(start);
booleanisSameInstant(Calendar cal1, Calendar cal2)

Checks if two calendar objects represent the same instant in time.

This method compares the long millisecond time of the two objects.

if (cal1 == null || cal2 == null) {
    throw new IllegalArgumentException("The date must not be null");
return cal1.getTime().getTime() == cal2.getTime().getTime();
booleanisSameLocalTime(Calendar cal1, Calendar cal2)

Checks if two calendar objects represent the same local time.

This method compares the values of the fields of the two objects.

if (cal1 == null || cal2 == null)
    throw new IllegalArgumentException("The date must not be null");
return (cal1.get(Calendar.MILLISECOND) == cal2.get(Calendar.MILLISECOND)
        && cal1.get(Calendar.SECOND) == cal2.get(Calendar.SECOND)
        && cal1.get(Calendar.MINUTE) == cal2.get(Calendar.MINUTE)
        && cal1.get(Calendar.HOUR) == cal2.get(Calendar.HOUR)
        && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
        && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
...
voidmaximizeTime(Calendar cal)
Set the time of a calendar to 23:59:59.999
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
GregorianCalendarmillisToCalendar(long millis)
millis To Calendar
GregorianCalendar result = new GregorianCalendar();
result.setTimeZone(TimeZone.getTimeZone("GMT"));
result.setTimeInMillis((long) (Math.ceil((double) millis / 1000) * 1000));
return result;
voidnormalize(final Calendar cal)
Normalizes a date to be comparable with another date, fixed at 12 hours, 0 seconds, 0 minutes of the day GMT+0.
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.setTimeZone(TimeZone.getTimeZone("GMT+0"));
CalendarnormalizedClone(final Calendar cal)
Clones and normalizes a calendar.
final Calendar cl = (Calendar) cal.clone();
normalize(cl);
return cl;
Datepostpone(final Calendar calendar, final int measureUnit, final int amount)
postpone
switch (measureUnit) {
case MILLISECOND:
    calendar.add(MILLISECOND, amount);
    break;
case SECOND:
    calendar.add(SECOND, amount);
    break;
case MINUTE:
...
voidresetCalendarTime(Calendar calendar)
reset Calendar Time
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int date = calendar.get(Calendar.DATE);
calendar.set(year, month, date, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);