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

CalendarnewCalendarInstance(TimeZone timeZone)
new Calendar Instance
Calendar calendar = Calendar.getInstance(timeZone);
return calendar;
longnormalizeLocalTime(final Calendar cal)
Normalize cal so that any Calendar with the same local time have the same result.
return cal.getTimeInMillis() + cal.getTimeZone().getOffset(cal.getTimeInMillis());
longparseTime(Calendar calendar, String time_string)
parse Time
String[] parts = time_string.split("[/ :]");
int day = Integer.parseInt(parts[0]);
int month = stringToMonth(parts[1]);
int year = Integer.parseInt(parts[2]);
int hours = Integer.parseInt(parts[3]);
int minutes = Integer.parseInt(parts[4]);
int seconds = Integer.parseInt(parts[5]);
calendar.set(year, month, day, hours, minutes, seconds);
...
longparseTime(String time, Calendar calendar)
Parses a time string in the format [Tt]HH:MM:SS(.[0-9]+)?
if (time.length() < MIN_TIME_STRING_LENGTH) {
    throw new NumberFormatException();
long timeZoneOffset = 0;
char first = time.charAt(0);
if (time.length() < MIN_TIME_STRING_LENGTH || (first != 'T' && first != 't')) {
    throw new NumberFormatException();
time = time.substring(1);
int value = Integer.parseInt(time.substring(0, 2));
if (value > 23) {
    throw new NumberFormatException();
calendar.set(Calendar.HOUR_OF_DAY, value);
if (time.charAt(2) == ':') {
    time = time.substring(3);
} else {
    time = time.substring(2);
value = Integer.parseInt(time.substring(0, 2));
if (value > 59) {
    throw new NumberFormatException();
calendar.set(Calendar.MINUTE, value);
if (time.charAt(2) == ':') {
    time = time.substring(3);
} else {
    time = time.substring(2);
value = Integer.parseInt(time.substring(0, 2));
if (value > 59) {
    throw new NumberFormatException();
calendar.set(Calendar.SECOND, value);
time = time.substring(2);
calendar.set(Calendar.MILLISECOND, 0);
if (time.length() > 0) {
    try {
        int currentStringPos = 0;
        if (time.charAt(currentStringPos) == '.') {
            ++currentStringPos;
            if (!Character.isDigit(time.charAt(currentStringPos))) {
                throw new NumberFormatException();
            int multiplier = 100;
            int milliseconds = Character.digit(time.charAt(currentStringPos), 10) * multiplier;
            ++currentStringPos;
            while (currentStringPos < time.length() && Character.isDigit(time.charAt(currentStringPos))) {
                multiplier /= 10;
                milliseconds += Character.digit(time.charAt(currentStringPos), 10) * multiplier;
                currentStringPos++;
            calendar.set(Calendar.MILLISECOND, milliseconds);
        if (currentStringPos < time.length()) {
            String timezone = time.substring(currentStringPos);
            if ((timezone.charAt(0) == 'Z' || timezone.charAt(0) == 'z') && timezone.length() == 1) {
            } else if (timezone.length() == NUMERIC_TIME_ZONE_LENGTH
                    && (timezone.charAt(0) == '+' || timezone.charAt(0) == '-')
                    && timezone.charAt(3) == ':') {
                int hours = Integer.parseInt(timezone.substring(1, 3));
                int minutes = Integer.parseInt(timezone.substring(4, 6));
                timeZoneOffset = ((hours * 60) + minutes) * 60000;
                if (timezone.charAt(0) == '+') {
                    timeZoneOffset = -timeZoneOffset;
            } else {
                throw new NumberFormatException();
    } catch (IndexOutOfBoundsException e) {
        throw new NumberFormatException();
return timeZoneOffset;
voidremoveTime(Calendar c)
remove Time
if (c == null)
    return;
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
CalendarremoveTime(Calendar cal)
remove Time
return new GregorianCalendar(cal.get(cal.YEAR), cal.get(cal.MONTH), cal.get(cal.DAY_OF_MONTH));
CalendarremoveTime(Calendar calendar)
remove Time
Calendar returnCalendar = (Calendar) calendar.clone();
returnCalendar.set(Calendar.HOUR_OF_DAY, 0);
returnCalendar.set(Calendar.MINUTE, 0);
returnCalendar.set(Calendar.SECOND, 0);
returnCalendar.set(Calendar.MILLISECOND, 0);
return returnCalendar;
CalendarremoveTime(final Calendar date)
Remote the time elements of the passed Calendar , setting the time to midnight of that day.
date.set(Calendar.SECOND, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.HOUR, 0);
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MILLISECOND, 0);
date.getTimeInMillis();
return date;
CalendarresetTime(Calendar cal)
Resets the time part of the date to 0:0:0
if (cal != null) {
    cal.set(Calendar.HOUR_OF_DAY, 0); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
return cal;
voidresetTime(Calendar cal)
reset Time
cal.set(11, 0);
cal.set(12, 0);
cal.set(13, 0);
cal.set(14, 0);