0

I have a list of alarms specified by the user like a typical alarm clock app as:

  • day-of-week
  • hour-of-day
  • minute-of-hour

I'm hand-writing a function to go through a list of times specified this way and determine which is the next up-coming alarm compared to current time. I'll use AlarmManager ultimately to scheduling the next upcoming alarm once I determine which alarm is next. I have considered Date, Time, and GregorianCalendar classes because they have before() and after() methods but they are all a pain to construct given my parameters. Is there a better way than writing all the date/time subtraction math myself?

Kyle Sweet
  • 306
  • 1
  • 3
  • 15

2 Answers2

0

You could use the Calendar class for aiding the construction of dates. Then with the getDate method you could get milliseconds and deal with them for finding the closest alarm. Check an example of usage of Calendar here http://www.tutorialspoint.com/java/util/calendar_add.htm

solver
  • 56
  • 3
  • Hi. How does that solve the problem of converting day-of-week, hour-of-day, minute-of-hour into [futuristic] Calendar objects such as a developer would need to do in an alarm clock app? – Kyle Sweet Jul 28 '16 at 19:41
  • Hi, from the documentation of the [Android developers site](https://developer.android.com/reference/java/util/Calendar.html), there is a number of constants that I think could be helpful. Check the `add()`, `set()` and `roll()` methods with the constants `DAY_OF_WEEK`, `HOUR_OF_DAY` and others. You could compose a future date using these facilities and then get remaining time to such date by substracting `System.currentTimeMillis()`. Hope it helps. – solver Aug 01 '16 at 16:38
0

You can use date object.Here is video lesson I found in youtube. create digital clock

    while(time == 0){
                        Calendar calen = new GregorianCalendar();

                        int hr = calen.get(Calendar.HOUR);
                        int min = calen.get(Calendar.MINUTE);
                        int sec = calen.get(Calendar.SECOND);
                        int AP = calen.get(Calendar.AM_PM);
                        String d_n = "";

                        if(AP == 1){
                            d_n = "PM";
                        }else{
                            d_n = "AM";
                        }
}
Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • There are a few shortcomings here. 1) Calendar gives time in Greenwhich, London. The user specified their times most likely in some other time zone. So I prefer to use the Date class because it returns the local/system time. 2) The crux of the problem is really "How do you convert alarms specified as day-of-week, hour-of-day, and minute-of-hour into Calender or Date objects?".I figured it out, but it took a lot of code and several hours to write the logic. Seems like it would be a common problem with a better solution. – Kyle Sweet Jul 26 '16 at 15:44