0

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.

enter image description here

How can I get the difference from current time to the alarm time that I am setting.?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

2

Couple of things that I see 'might' be the problem with your code are:

alarmTime.set(Calendar.DAY_OF_WEEK, day);

Are you getting the day of the week from the dataset or day of the month? Make sure it is returning the day of the week or change it to DAY_OF_MONTH.

Once you do that, you can do something like this

long millis1 = now.getTimeInMillis();
long millis2 = alarmTime.getTimeInMillis();

//Take the absolute difference between the two millis so you don't get a negative valure
long diff = millis1 > millis2 ? (millis1 - millis2) :(millis2 - millis1);
int days = TimeUnit.MILLISECONDS.toDays(diff)
int hours = TimeUnit.MILLISECONDS.toHours(diff)
int minutes = TimeUnit.MILLISECONDS.toMinutes(diff)

This will give you the difference between any two times in millis.

LeoNeo
  • 739
  • 1
  • 9
  • 28
  • As you can see in the screenshot day = 3 for tuesday –  Dec 22 '17 at 04:36
  • I am passing in day as an integer. Sunday=1 monday = 2, tuesday = 3 ect –  Dec 22 '17 at 04:38
  • okay that's a better explanation coz day=3 could also mean 3rd day of the month :). But try the rest of the solution to get the difference and let us know if that satisfies your requirements. – LeoNeo Dec 22 '17 at 04:39
  • Also as a debugging tip. after the lines `long millis1 = now.getTimeInMillis(); long millis2 = alarmTime.getTimeInMillis();` can you add the following line to see if what you think is what is getting set in the alarmTime variable by doing so : `new SimpleDateFormat("dd-MM-yyyy hh:mm:ss", Locale.ENGLISH).format(millis1)` – LeoNeo Dec 22 '17 at 04:48
  • I tried using your code but its completely wrong. I set wednsday for the alarm day and today is thursday and it gave me one day where it should have given me around 5 days –  Dec 22 '17 at 04:49
  • Oh.. I am afraid you'll have to use calendar dates in that case. I misunderstood the problem my bad! If you want the number of days until the **next** Wednesday from today (Thursday) then you need that reference to be told to your code. The best way to do that is grabbing today's date and next Wednesday's date and finding the difference between them. You may still be able to do it using weekdays but you'll have to say something like day1(Thu-4) > day2(Wed-3) so the difference in days is [7-4(Thu)] + 3(Wed) = 6 days. You'll still be stuck with the proper computation of hrs,min difference. – LeoNeo Dec 22 '17 at 04:58
  • I thought i might have to go that route. I will work out the logic then. –  Dec 22 '17 at 05:01