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

StringconvertToTimeStpam(Calendar cal)
Convert date to short time string
return convertToTimeStamp(cal, true);
CalendarconvertToTimeZone(Calendar time, TimeZone timeZone)
convert To Time Zone
Calendar foreignTime = new GregorianCalendar(timeZone);
foreignTime.setTimeInMillis(time.getTimeInMillis());
return foreignTime;
CalendarcopyLocalTime(final Calendar from, final Calendar to)
Copy the local time from one calendar to another.
final boolean sameClass = from.getClass() == to.getClass();
final boolean createGregSource = !sameClass && to.getClass() == GregorianCalendar.class;
final boolean createGregDest = !sameClass && from.getClass() == GregorianCalendar.class;
if (!sameClass && !createGregSource && !createGregDest)
    throw new IllegalArgumentException("Calendars mismatch " + from.getClass() + " != " + to.getClass());
final Calendar source = createGregSource ? new GregorianCalendar(from.getTimeZone()) : from;
if (createGregSource) {
    source.setTime(from.getTime());
...
CalendarcopyLocalTime(final Calendar from, final Calendar to)
Copy the local time from one calendar to another.
to.clear();
for (final int field : new int[] { Calendar.YEAR, Calendar.DAY_OF_YEAR, Calendar.HOUR_OF_DAY,
        Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND }) {
    to.set(field, from.get(field));
return to;
CalendarcurrentTimeAsCalendar()
current Time As Calendar
return Calendar.getInstance();
doubledateTimeToFMDateTime(Calendar dateTime)
date Time To FM Date Time
int y = dateTime.get(Calendar.YEAR);
int m = dateTime.get(Calendar.MONTH);
int d = dateTime.get(Calendar.DAY_OF_MONTH);
int h = dateTime.get(Calendar.HOUR_OF_DAY);
int n = dateTime.get(Calendar.MINUTE);
int s = dateTime.get(Calendar.SECOND);
int datePart = ((y - 1700) * 10000) + ((m + 1) * 100) + d;
int timePart = (h * 10000) + (n * 100) + s;
...
int[]elapsedTime(Calendar before, Calendar after)
Calculate the total of elapsed time from years up to seconds between the two given calendars.
int[] elapsedTime = new int[6];
Calendar clone = (Calendar) before.clone(); 
elapsedTime[0] = elapsedYears(clone, after);
addYears(clone, elapsedTime[0]);
elapsedTime[1] = elapsedMonths(clone, after);
addMonths(clone, elapsedTime[1]);
elapsedTime[2] = elapsedDays(clone, after);
addDays(clone, elapsedTime[2]);
...
CalendarequalizeTime(final Calendar toEqualize, final Calendar source)
sets the time of the day of the first argument, so that it is equal to the time of the day of the second argument.
if (source == null) {
    return null;
final int hourOfDay = source.get(Calendar.HOUR_OF_DAY);
final int minute = source.get(Calendar.MINUTE);
final int second = source.get(Calendar.SECOND);
final int milliSecond = source.get(Calendar.MILLISECOND);
final Calendar result = (Calendar) toEqualize.clone();
...
booleanequalsIgnoreTimeZone(final Calendar c1, final Calendar c2)
equals Ignore Time Zone
if (c1 == null && c2 == null) {
    return true;
} else if (c1 == null || c2 == null) {
    return false;
return c1.getTimeInMillis() == c2.getTimeInMillis();
CalendargetCalendarWithoutTimeZone(Date date)
Returns a Calendar with no time zone
if (date == null) {
    return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.clear(Calendar.ZONE_OFFSET);
return cal;