D
D
Denis Yakovenko2015-04-12 18:11:24
Java
Denis Yakovenko, 2015-04-12 18:11:24

How to implement a timer that works on an array of dates?

Greetings!
There is such a task:
You need to create a CountDownTimer that would count the time on an array of dates, i.e., for example, there is an array

["12.04.2015 19:50", "13.04.2015 11:45", "17.05.2015 07:45"]
and you need the timer in the application to count down the time first to the first element of the array, then to the second, and so on until the end, and to show all this in TextView.
Perhaps the timer can be restarted somehow when it reaches 00:00? Or do I need to overload the method somehow onFinish()?
Can you please tell me how this can be implemented? Or maybe someone can share a piece of code)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur, 2015-04-12
@yakovenkodenis

Here's what I did (I hope I understood the problem correctly):

package application;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Timur Nikiforov
 */
public class Clazz {

  private Timer dateTimer;

  private Timer remainderTimer;

  private Date nextDate;

  private boolean remainderTimerStarted;
  
  private static final long REMINDER_UPDATE_INTERVAL = 1000; 

  private static final String[] DATES = { "12.04.2015 19:56", "12.04.2015 19:57", "12.04.2015 19:58" };

  private int currentIndex;

  public Clazz() {
    dateTimer = new Timer();
  }

  public static void main(String[] args) {
    Clazz instance = new Clazz();
    instance.run();
  }

  private void run() {
    nextDate = parseDate(DATES[currentIndex]);
    schedule();
  }

  public void schedule() {
    runSecondsCounter();
    dateTimer.schedule(new TimerTask() {

      @Override
      public void run() {

        System.out.println("Current date is:" + new Date());
        currentIndex++;
        if (currentIndex < DATES.length) {
          nextDate = parseDate(DATES[currentIndex]);
          System.out.println("Next date is:" + nextDate);
          schedule();
        } else {
          remainderTimer.cancel();
        }
      }
    }, nextDate);

  }

  private Date parseDate(String nextDate) {
    Date date = null;
    DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm",
        Locale.ENGLISH);
    try {
      date = format.parse(nextDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

  private void runSecondsCounter() {
    if (remainderTimerStarted) {
      remainderTimer.cancel();
    }

    remainderTimer = new Timer();
    remainderTimer.scheduleAtFixedRate(new TimerTask() {

      @Override
      public void run() {
        remainderTimerStarted = true;
        long remains = nextDate.getTime() - new Date().getTime();
        System.out.println("Remains: " + (remains / 1000) + " seconds");
      }
    }, REMINDER_UPDATE_INTERVAL, REMINDER_UPDATE_INTERVAL);
  }
}

Understand without comment?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question