0

I have a TimePicker in android to set an hour for a Alarm (AlarmManager) the problem its i got this

 public void onTimeSet( TimePicker view, int hourOfDay, int minute )
 { // Some logical code here }

so in my alarm the user can set how much time before the notify should start, 5 minutes, 10 minutes or 30. so

How can i get hourOfDay and Minute in order to substract the time before the alarm should notify ?

Example:

User set alarm: 13:30 and 10 Minutes before, i need to get 13:20.

Another example: user sets 2:5AM and 30 minutes before, i need to get 1:35AM
k_g
  • 4,333
  • 2
  • 25
  • 40
Oku
  • 61
  • 10
  • Have you seen fields in [SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#fields_inherited_from_class_java.text.DateFormat) – Neeraj Jain Mar 10 '15 at 11:48
  • Nope, im going to check thx ! – Oku Mar 10 '15 at 12:20

1 Answers1

0

first of all create :

Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hourOfDay, minute);
long exact_alarm_time = cal.getTimeInMillis();

// if user want to set before 10 minutes
long before = 10*60*1000;

// now subtract the before time from current time
long newTime = exact_alarm_time - before;

Calendar cNew = Calendar.getInstance();
cal.setTimeInMillis(newTime);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int date = cal.get(Calendar.DATE);
int min = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR);

// now set alarm for the new time
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43
  • Just Read once [Why Calendar Instances are particularly expensive to create](http://stackoverflow.com/q/28704832/3143670) and [here](http://cephas.net/blog/2006/02/25/the-cost-of-calendar-object-creation/) – Neeraj Jain Mar 10 '15 at 12:24