如何在特定时刻设置android中的日历

时间:2011-04-24 11:21:28

标签: java android

我不想添加年份或月份。只需每天的小时,分​​钟和秒。 如何使用Calendar对象来做呢? 这是我的代码

// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object

cal.add(Calendar.HOUR, 4);
cal.add(Calendar.MINUTE, 40);




Intent i = new Intent(this, Receiver.class);
i.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, i, PendingIntent.FLAG_UPDATE_CURRENT);

// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

2 个答案:

答案 0 :(得分:52)

尝试使用

calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 40);
calendar.set(Calendar.SECOND, 0);

答案 1 :(得分:12)

试试这个

cal.set(2013,2,7,15,42,30);

注意:月份值为MonthNumber-1Jan is 0Feb is 1等等。