get Calendar For Time - Android java.util

Android examples for java.util:Date Time

Description

get Calendar For Time

Demo Code


//package com.java2s;
import java.util.Calendar;

import android.util.Log;

public class Main {
    public static Calendar getCalendarForTime(int repeatMode,
            int dayOfWeek, int hour, int minute, int second) {
        Calendar calendarNow = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        calendar.set(Calendar.MILLISECOND, 0);
        if (repeatMode == 2) {
            int dayOfWeekAndroid = 0; // 7 stands for sunday for interface, but for android, sunday stands for 1.
            dayOfWeekAndroid = dayOfWeek % 7 + 1;
            calendar.set(Calendar.DAY_OF_WEEK, dayOfWeekAndroid);
        }//  w  ww .j a v a2s.c o  m

        // make sure the desire alarm time is in future.
        int tryCount = 0;
        int tryCountMax = 62;
        while (calendar.getTimeInMillis() < calendarNow.getTimeInMillis()
                && tryCount < tryCountMax) {
            if (repeatMode == 1) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
            } else if (repeatMode == 2) {
                calendar.add(Calendar.DAY_OF_YEAR, 7);
            }
            tryCount++;
        }
        Log.v("cpeng",
                "getCalendearForTime target info: " + calendar.toString());
        return calendar;
    }
}

Related Tutorials