I am setting a calendar date for an alarm to go off. Alarm time has the hour, minute, format, and day set. I also want to grab the now time and compare the difference. So, if alarm time is set on Wednesday and now time is Thursday I want how many days between Thursday until Wednesday. I also need hours and minutes.
I have tried every example and nothing seems to work.
JAVA METHOD
private void setAlarm(final int position, int day){
alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
int amPm = 0 ;
String hourOfDay = mDataset.get(position).getHourOfDay();
String minute = mDataset.get(position).getMinute();
String format = mDataset.get(position).getTimeSet();
if("PM".equalsIgnoreCase(format)){
amPm = 1;
}else{
amPm = 0;
}
Calendar alarmTime = Calendar.getInstance();
alarmTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourOfDay));
alarmTime.set(Calendar.MINUTE, Integer.parseInt(minute));
alarmTime.set(Calendar.AM_PM,amPm);
alarmTime.set(Calendar.DAY_OF_WEEK, day);
Calendar now = Calendar.getInstance();
I did attempt to try this.
alarmTime = Calendar.getInstance();
alarmTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourOfDay));
alarmTime.set(Calendar.MINUTE, Integer.parseInt(minute));
alarmTime.set(Calendar.AM_PM,amPm);
alarmTime.set(Calendar.DAY_OF_WEEK, day);
Calendar now = Calendar.getInstance();
long millis1 = now.getTimeInMillis();
long millis2 = alarmTime.getTimeInMillis();
long diff = millis1 - millis2;
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
It is just not quite right. It says 2 days so I am assuming it is taking the alarm day(Tuesday) to Thursday(today). What I need is from Today(Thursday) to Tuesday. Which is about 5 days from now. The hours should be around 5 hours but always seem to be 12.
How can I get the difference from current time to the alarm time that I am setting.?