Code example for Calendar

Methods: add, getTimeInMillis, set, setTimeInMillis

0
 
    static long computeCalendarTime(Calendar c, long curTime,
            long secondsSinceMidnight) {
 
        // start with now 
        c.setTimeInMillis(curTime);
 
        int val = (int)secondsSinceMidnight / (60*60);
        c.set(Calendar.HOUR_OF_DAY, val);
        secondsSinceMidnight -= val * (60*60);
        val = (int)secondsSinceMidnight / 60;
        c.set(Calendar.MINUTE, val);
        c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
        c.set(Calendar.MILLISECOND, 0);
 
        long newTime = c.getTimeInMillis();
        if (newTime < curTime) {
            // The given time (in seconds since midnight) has already passed for today, so advance 
            // by one day (due to daylight savings, etc., the delta may differ from 24 hours). 
            c.add(Calendar.DAY_OF_MONTH, 1);
            newTime = c.getTimeInMillis();
        } 
 
        return newTime;
    }