List of utility methods to do Calendar Calculate
boolean | isAfterWorkTime(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; |
boolean | isInWorkTime(Calendar start) Determines whether or not the time of the date is within the work day return !isBeforeWorkTime(start) && !isAfterWorkTime(start);
|
boolean | isSameInstant(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(); |
boolean | isSameLocalTime(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) ... |
void | maximizeTime(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); |
GregorianCalendar | millisToCalendar(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; |
void | normalize(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"));
|
Calendar | normalizedClone(final Calendar cal) Clones and normalizes a calendar. final Calendar cl = (Calendar) cal.clone(); normalize(cl); return cl; |
Date | postpone(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: ... |
void | resetCalendarTime(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); |