Code example for GregorianCalendar

Methods: clear, get, set, setTimeInMillis

0
        return TICS_STEPS[idx];
    } 
 
    public static Calendar roundTimeByTicsStep(long time, TicsStep step)
    { 
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(time);
 
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        if(step == TicsStep.WEEK) {
            int week = cal.get(Calendar.WEEK_OF_MONTH);
            cal.clear();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.WEEK_OF_MONTH, week);
            cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        } 
        else { 
            int day = (step == TicsStep.MONTH ?
                       1 : cal.get(Calendar.DAY_OF_MONTH));
            int hour = (step == TicsStep.HOUR ?
                        cal.get(Calendar.HOUR) : 0);
 
            if(step == TicsStep.FIVE_DAYS) {
                day = (day / 5) * 5;
                day = (day == 0 ? 1 :
                       day > 25 ? 25 :
                       day);
            } 
            if(step == TicsStep.TEN_DAYS) {
                day = (day / 10) * 10;
                day = (day == 0 ? 1 :
                       day > 20 ? 20 :
                       day);
            } 
 
            cal.clear();
            cal.set(year, month, day, hour, 0);
        } 
 
        return cal;
    }