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

CalendargetCalendarAtMidnight(Date d)
Set the given date to a Calendar at midnight.
Calendar cal = date2Calendar(d);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
CalendargetCalendarBefore(int i)
get Calendar Before
return getCalendarBefore(Calendar.getInstance(), i);
CalendargetCalendarByName(String timeZone)
get Calendar By Name
timeZone = timeZone.trim();
TimeZone zone = TimeZone.getTimeZone(timeZone);
String id = zone.getID();
if (id.compareTo(timeZone) != 0) {
    throw new IllegalArgumentException("Bad timezone name: '" + timeZone + "'");
return Calendar.getInstance(zone);
CalendargetCalendarBySpecific(int year, int month, int date, int hour)
get Calendar By Specific
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, date);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
...
CalendargetCalendarByValue(String timeZone)
get Calendar By Value
timeZone = timeZone.trim();
char ch = timeZone.charAt(0);
if (ch == '+' || ch == '-') {
    timeZone = "GMT" + timeZone;
} else if (Character.getType(ch) == Character.DECIMAL_DIGIT_NUMBER) {
    timeZone = "GMT+" + timeZone;
String prefix = timeZone.substring(0, 4);
...
CalendargetCalendarDate()
Retrieve Calendar date.
final Calendar currentDate = Calendar.getInstance();
currentDate.set(Calendar.SECOND, 0);
return currentDate;
StringgetCalendarDate(Calendar calendar)
get Calendar Date
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (month.length() < 2)
    month = "0" + month;
String date = String.valueOf(calendar.get(Calendar.DATE));
if (date.length() < 2)
    date = "0" + date;
String hours = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
...
longgetCalendarDateFromToday(final int numDays)
Flips through the Calendar from the current date over the given number of days to fetch the required date in milli seconds.
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_MONTH, numDays);
return cal.getTimeInMillis();
StringgetCalendarDayString(Calendar date)
returns the month (mm) as a string from a calendar date
return String.valueOf(date.get(Calendar.DAY_OF_MONTH));
intgetCalendarField(char c)
get Calendar Field
switch (c) {
case 'y':
    return Calendar.YEAR;
case 'M':
    return Calendar.MONTH;
case 'D':
    return Calendar.DAY_OF_YEAR;
case 'd':
...