Code example for Calendar

Methods: add, getDisplayName, getTime, getTimeInMillis

0
 
            if (day != null)
            { 
                tmpCalendar.setTime(day);
                //extract day 
                copyCalendarField(calendar, tmpCalendar, Calendar.DAY_OF_WEEK);
                //if setting next day, and day == curDay, set it to next week 
                if (isNextDay)
                { 
                    int nextDay = tmpCalendar.get(Calendar.DAY_OF_WEEK);
                    if (curDay == nextDay)
                        calendar.add(Calendar.DAY_OF_WEEK, 7);
                } 
            } 
            if (date != null)
            { 
                tmpCalendar.setTime(date);
                //extract month, day, year 
                copyCalendarField(calendar, tmpCalendar, Calendar.MONTH);
                copyCalendarField(calendar, tmpCalendar, Calendar.DAY_OF_MONTH);
                if (date.isYearSet())
                    copyCalendarField(calendar, tmpCalendar, Calendar.YEAR);
            } 
            if (time != null)
            { 
                tmpCalendar.setTime(time);
                //extract hour, min, sec 
                copyCalendarField(calendar, tmpCalendar, Calendar.HOUR_OF_DAY);
                copyCalendarField(calendar, tmpCalendar, Calendar.MINUTE);
                copyCalendarField(calendar, tmpCalendar, Calendar.SECOND);
            } 
            else //set the default time 
            { 
                // get default time from defaultTimeStr and assign it to defaultTimeCal 
                SimpleDateFormat sdf = new SimpleDateFormat("h:mma");
                Date defaultTime = sdf.parse(defaultTimeStr, new ParsePosition(0));
                defaultTimeCal = initCalendar(defaultTime);
 
                copyCalendarField(calendar, defaultTimeCal, Calendar.HOUR_OF_DAY);
                copyCalendarField(calendar, defaultTimeCal, Calendar.MINUTE);
            } 
            if (date == null && day == null)
            { 
                // we're here because you didn't set a date or a day. Ex: task 8pm 
                // need to figure out if calendar date points to date in past 
                if (calendar.getTimeInMillis() < curTime)
                    // if so then point to tomorrow 
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
            } 
            else if (day != null)
            { 
                // we're here because you set the day but not a date 
                // need to figure out if calendar date points to date in past 
                if (calendar.getTimeInMillis() < curTime)
                    // then move date to 1 week later 
                    calendar.add(Calendar.DAY_OF_MONTH, 7);
            } 
            else if (date != null && !date.isYearSet())
            { 
                // we're here because you set a year-less date but not a day 
                if (calendar.getTimeInMillis() < curTime)
                    // then move date to 1 year later 
                    calendar.add(Calendar.YEAR, 1);
            } 
 
        } 
    } 
 
    /** 
     * Set a date such as "June 2" or "May 1, 2000" 
     * 
     * @param date Date object containing the desired date 
     */ 
    public void setDate(ReDate date)
    { 
        calendar = null;
        this.date = date;
    } 
 
    /** 
     * Set a time such as "8pm" 
     * 
     * @param time date containing the desired time 
     */ 
    public void setTime(ReDate time)
    { 
        calendar = null;
        this.time = time;
    } 
 
    /** 
     * Set a day such as "Monday" 
     * 
     * @param day date containing the desired day 
     */ 
    public void setDay(ReDate day)
    { 
        calendar = null;
        this.day = day;
    } 
 
    public void setTimeInMillis(long time)
    { 
        calendar = getCalendar();
        calendar.setTimeInMillis(time);
    } 
 
    /** 
     * Special case of setting the "next" day. 
     * 
     * Ex: given "buy milk next monday" and today is Monday, sets the date to 
     * next Monday. (Otherwise, setNextDay is the same as setDay.) 
     * 
     * @param day 
     */ 
    public void setNextDay(ReDate day)
    { 
        calendar = null;
        this.day = day;
        isNextDay = true;
    } 
 
    public void setTomorrow() 
    { 
        calendar = null;
        tmpCalendar = getCalendar();
        tmpCalendar.add(Calendar.DAY_OF_WEEK, 1);
        setNextDay(new ReDate(tmpCalendar.getTime()));
    } 
 
    public Date getDate()
    { 
        calculateTimeAndDate(); 
        return calendar.getTime();
    } 
 
    /** 
     * Gets date in user's locale 
     * 
     * @return 
     */ 
    public String getLocaleDate()
    { 
        calculateTimeAndDate(); 
        return medDateFormat.format(calendar.getTime());
    } 
 
    /** 
     * Gets time in user's locale 
     * 
     * @return 
     */ 
    public String getLocaleTime()
    { 
        calculateTimeAndDate(); 
        return shortTimeFormat.format(calendar.getTime());
    } 
 
    /** 
     * Gets day in user's locale 
     * 
     * @return 
     */ 
    @SuppressLint("NewApi") 
    public String getLocaleDay()
    { 
        calculateTimeAndDate(); 
        return calendar.getDisplayName(Calendar.DAY_OF_WEEK,
                                       Calendar.LONG,
                                       locale);
    } 
 
    public int get(int field)