问题
I'm making an alarm, get the current time as follows:
public String getAlarmTimeStringFive2db() {
String timef2db = "";
if (alarmTime.get(Calendar.HOUR_OF_DAY) <= 9)
timef2db += "0";
timef2db += String.valueOf(alarmTime.get(Calendar.HOUR_OF_DAY));
timef2db += ":";
if (alarmTime.get(Calendar.MINUTE) <= 9)
timef2db += "0";
timef2db += String.valueOf(alarmTime.get(Calendar.MINUTE));
return timef2db;
}
...
public static long create(Alarm alarm) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_ALARM_ACTIVE, alarm.getAlarmActive());
cv.put(COLUMN_ALARM_TIME, alarm.getAlarmTimeStringFive2db());
...
public void setAlarmTime(String alarmTime) {
String[] timePieces = alarmTime.split(":");
Calendar newAlarmTime = Calendar.getInstance();
newAlarmTime.set(Calendar.HOUR_OF_DAY,
Integer.parseInt(timePieces[0]));
newAlarmTime.set(Calendar.MINUTE, Integer.parseInt(timePieces[1]));
newAlarmTime.set(Calendar.SECOND, 0);
setAlarmTime(newAlarmTime);
}
...
This works fine, for example returns 5:35 ... all right.
My problem is I want to subtract 5 minutes always on time. If the time is 5:35, I want the alarm time starts at 5:30.
My problem is I do not know how to subtract those 5 minutes. I tried
Calendar.MINUTE, Integer.parseInt(timePieces[1],5)
Calendar.MINUTE, -5)
...but nothing works
I read this link Set Alarm To Alert 5 Minutes Before a certian Time .. but I could not apply it to my code
Can anyone tell me as subtracting 5 minutes of my alarm?
thanks in advance
Regards
回答1:
I'm not sure if you are using the Calendar instance in the right way, but here is an example shows that time changes when use add -5 to the current calendar
Calendar cal = Calendar.getInstance();
System.out.println("Before :" + cal.get(Calendar.MINUTE));
cal.add(Calendar.MINUTE, -5);
System.out.println("After :" + cal.get(Calendar.MINUTE));
and the result will be something like this:
Before :37
After :32
来源:https://stackoverflow.com/questions/18645074/android-subtract-minutes-to-my-alarm