Display an alert when 5 minutes are missing for the event

1

I'm developing a Java calendar, where I have the date and time saved in the database. However, what would be the best way to display an alert 5 minutes before the event? Do I have to run the query method all the time until the hours hit? Or is there a simpler way to do this?

    
asked by anonymous 16.07.2015 / 15:59

1 answer

1

Techies

You can use Timer (java.util.Timer) and set it to run 5 minutes before your time:

private Timer timer;

private void programaPara(Date data){// mande a data com 5 minutos de antecedência.

       timer = new Timer();

       timer.schedule(new TimerTask() {

           @Override
           public void run() {
               //faça algo
           }
      }, data);
   }

To subtract 5 minutes from the date you may be using Calendar :

public Date getDataMenos5Minutos(Date data){
     Calendar calendar =Calendar.getInstance();
     calendar.setTime(data);
     calendar.add(Calendar.MINUTE, -5);

     return calendar.getTime();
}
    
16.07.2015 / 17:01