Java Utililty Methods Calendar Create

List of utility methods to do Calendar Create

Description

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

Method

IntegergetCalendarField(Date date, int calFieldId)
Shortcut function that returns the given Calendar field for the date
if (date != null) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(calFieldId);
return null;
StringgetCalendarFieldName(int field)
get Calendar Field Name
switch (field) {
case Calendar.DAY_OF_MONTH:
    return "DAY_OF_MONTH";
case Calendar.DAY_OF_WEEK:
    return "DAY_OF_WEEK";
case Calendar.DAY_OF_YEAR:
    return "DAY_OF_YEAR";
case Calendar.HOUR:
...
CalendargetCalendarFormYear(int year)
get Calendar of given year
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.YEAR, year);
return cal;
GregorianCalendargetCalendarForNextRun(GregorianCalendar startTime, int targetDay, int targetHour)
Given a start time, computes the next time when the wallclock will reach a certain hour of the day, on a certain day of the week Eg: From today, when is the next Saturday, 12PM ?
long startTimeMs = startTime.getTimeInMillis();
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(startTimeMs);
cal.set(Calendar.HOUR_OF_DAY, targetHour);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if (cal.get(Calendar.DAY_OF_WEEK) != targetDay || cal.getTimeInMillis() < startTimeMs) {
...
CalendargetCalendarForSchedule(Calendar cal)
get Calendar For Schedule
return cal;
CalendargetCalendarForTime(final long timeInMilliseconds)
Function to return a calendar object holding the UTC representation of the current time.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTimeInMillis(timeInMilliseconds);
return calendar;
CalendargetCalendarFromDates(Date date, Date hourMinuteMillisseconds)
Merge two Date variables to compose a single Calendar object.
Calendar ret = Calendar.getInstance();
ret.setTime(date);
Calendar hour = Calendar.getInstance();
hour.setTime(hourMinuteMillisseconds);
ret.set(Calendar.HOUR_OF_DAY, hour.get(Calendar.HOUR_OF_DAY));
ret.set(Calendar.MINUTE, hour.get(Calendar.MINUTE));
ret.set(Calendar.SECOND, hour.get(Calendar.SECOND));
ret.set(Calendar.MILLISECOND, hour.get(Calendar.MILLISECOND));
...
CalendargetCalendarFromSystemTimeInMillis(long date)
get Calendar From System Time In Millis
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(date);
return calendar;
CalendargetCalendarGMT()
get Calendar GMT
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT"));
return c;
CalendargetCalendarInstance()
Get a new calendar instance from UTC timezone
return Calendar.getInstance(UTC_TIMEZONE);