Java Utililty Methods Calendar Time

List of utility methods to do Calendar Time

Description

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

Method

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)
...
booleanisSameTime(final Calendar aCal1, final Calendar aCal2)
is Same Time
boolean retVal = aCal1.get(Calendar.HOUR_OF_DAY) == aCal2.get(Calendar.HOUR_OF_DAY);
retVal = retVal && (aCal1.get(Calendar.MINUTE) == aCal2.get(Calendar.MINUTE));
retVal = retVal && (aCal1.get(Calendar.SECOND) == aCal2.get(Calendar.SECOND));
return retVal;
booleanisSleepTime(Calendar calendar, String sleepTimeStart, String sleepTimeEnd)
is Sleep Time
int sleepStart = Integer.parseInt(sleepTimeStart);
int sleepEnd = Integer.parseInt(sleepTimeEnd);
int hourofday = calendar.get(Calendar.HOUR_OF_DAY);
return hourofday >= sleepStart || hourofday <= sleepEnd;
booleanisTime0(final Calendar calendar)
is Time
if (calendar.get(Calendar.HOUR_OF_DAY) == 0 && calendar.get(Calendar.MINUTE) == 0
        && calendar.get(Calendar.SECOND) == 0 && calendar.get(Calendar.MILLISECOND) == 0) {
    return true;
return false;
booleanisWorkTime(Calendar calendar, String workTimeStart, String workTimeEnd)
is Work Time
int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
int hourofday = calendar.get(Calendar.HOUR_OF_DAY);
int workStart = Integer.parseInt(workTimeStart);
int workEnd = Integer.parseInt(workTimeEnd);
return dayofweek > Calendar.SUNDAY && dayofweek < Calendar.SATURDAY && hourofday >= workStart
        && hourofday <= workEnd;
voidmaximizeTimeFields(final Calendar calendar)
maximize Time Fields
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
Calendarmerge(Calendar date, Calendar time)
Merge two calendars, one with the day date component and the other with a time component.
Calendar resCal = Calendar.getInstance();
if (date == null && time == null)
    return resCal;
if (date != null)
    resCal = (Calendar) date.clone();
resCal.clear();
if (null == time) {
    resCal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH));
...
CalendarmergeDateAndTime(final Calendar date, final Calendar time)
merge Date And Time
time.set(Calendar.YEAR, date.get(Calendar.YEAR));
time.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH));
time.set(Calendar.MONTH, date.get(Calendar.MONTH));
return time;
CalendarmergeDateTime(Calendar date, Calendar time)
merge Date Time
Calendar clendar = null;
if (date != null && time != null) {
    clendar = Calendar.getInstance();
    clendar.set(Calendar.YEAR, date.get(Calendar.YEAR));
    clendar.set(Calendar.MONTH, date.get(Calendar.MONTH));
    clendar.set(Calendar.DAY_OF_MONTH, date.get(Calendar.DAY_OF_MONTH));
    clendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    clendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
...
CalendarnewCalendar(final long time)
new Calendar
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return calendar;