hasancse1991

next reminder

Oct 2nd, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public Calendar getNextAlarmTime() {
  2.         Calendar now = Calendar.getInstance();
  3.         Calendar reminderTime = Calendar.getInstance();
  4.         Calendar expireDate = Calendar.getInstance();
  5.        
  6.         SimpleDateFormat hourMinuteFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
  7.         SimpleDateFormat expireDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
  8.        
  9.         try {
  10.             expireDate.setTime(expireDateFormat.parse(expired));
  11.         } catch (ParseException e) {
  12.             e.printStackTrace();
  13.         }
  14.         expireDate.add(Calendar.DATE, 1);
  15.  
  16.         String nowHourString = hourMinuteFormat.format(now);
  17.  
  18.         int listSize = reminderTimes.size();
  19.         for (int i = 0; i<listSize; i++) {
  20.             String time = reminderTimes.get(i);
  21.  
  22.             int stringComp = nowHourString.compareTo(time);
  23.             if (stringComp<0) {
  24.                 try {
  25.                     reminderTime.setTime(hourMinuteFormat.parse(time));
  26.  
  27.                     if (reminderTime.before(expireDate))
  28.                         return reminderTime;
  29.                     else
  30.                         return null;
  31.  
  32.                 } catch (ParseException e) {
  33.                     e.printStackTrace();
  34.                     return null;
  35.                 }
  36.             } else if (i==listSize-1) {
  37.                 try {
  38.                     reminderTime.setTime(hourMinuteFormat.parse(reminderTimes.get(0)));
  39.                     reminderTime.add(Calendar.DATE, reminderInterval);
  40.  
  41.                     if (reminderTime.before(expireDate))
  42.                         return reminderTime;
  43.                     else
  44.                         return null;
  45.                    
  46.                 } catch (ParseException e) {
  47.                     e.printStackTrace();
  48.                     return null;
  49.                 }
  50.             }
  51.  
  52.         }
  53.         return null;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment