title: how to schedule a task every night at 12 AM
body: <p>I want to start ScheduledExecutorService exactly on 12 AM daily, but in my code schedule is not started. Can any one tell me Whether my code is Correct or not? If any other way available that would also helpful</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity{  
  ScheduledExecutorService scheduler;
  
  @Override
  
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  scheduler = Executors.newScheduledThreadPool(1);
  
  scheduleTask();
  
  }
  
   public void scheduleTask() {
  
          final Runnable beeper = new Runnable() {
              public void run() {
                  MainActivity.this.runOnUiThread(new Runnable() {
                      public void run() {
  
  
  Toast.makeText(getApplicationContext(),"SCHEDULE STARTED",Toast.LENGTH_LONG).show();
  
                                      }
                                  }
                              }
                          }, 1500);
                      }
                      });
              }
              };
  
          Calendar midnight = Calendar.getInstance();
          midnight.set(Calendar.HOUR_OF_DAY, 0);
          midnight.set(Calendar.MINUTE, 0);
          midnight.set(Calendar.SECOND, 0);
          midnight.set(Calendar.MILLISECOND, 1);
          midnight.set(Calendar.DAY_OF_YEAR, midnight.get(Calendar.DAY_OF_YEAR) + 1);
          long tillMidnight = midnight.getTimeInMillis() - System.currentTimeMillis() - 1;
          long ticksTillMidnight = tillMidnight / 50;
  
          final ScheduledFuture&lt;?&gt; beeperHandle = scheduler.scheduleAtFixedRate(beeper, ticksTillMidnight,20*60*60*24, TimeUnit.MILLISECONDS
          );
          scheduler.schedule(new Runnable() {
              public void run() {
                  beeperHandle.cancel(true);
              }
          }, 6*6,     TimeUnit.MILLISECONDS
          );
      }}
  </code></pre>