Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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);
        }

        // 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;
    }
}