0

I am working on an android app that requires me to count down the time to Christmas by days, hours, minutes and seconds. This code must be run on an emulator later in the project.

I cannot get the time to update correctly. Sometimes I am able to have the code print out continually the exact time, but that is it. It will not update and subtract the seconds, minutes, hours and days.

When I run the code in its current condition even with try/catch, it does not update the time every second. Is there a loop that can update the time every second without causing the compiler to have too much to process out?

     Calendar thatDay = Calendar.getInstance();
     thatDay.setTime(new Date(0)); /* reset */
     thatDay.set(Calendar.DAY_OF_MONTH, 25);
     thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
     thatDay.set(Calendar.YEAR, 2014);

     Calendar today = Calendar.getInstance();

     while(true)
     {

     long diff = thatDay.getTimeInMillis() - today.getTimeInMillis();
     long diffSec = diff / 1000;

     long days = diffSec / SECONDS_IN_A_DAY;
     long secondsDay = diffSec % SECONDS_IN_A_DAY;
     long seconds = secondsDay % 60;
     long minutes = (secondsDay / 60) % 60;
     long hours = (secondsDay / 3600); // % 24 not needed


     print (days+""+hours+""+minutes+""+seconds+"")
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188

2 Answers2

0

This may help you out. Here is a simple countdown timer:

import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Stopwatch {
static int interval;
static Timer timer;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Input seconds => : ");
    String secs = sc.nextLine();
    int delay = 1000;
    int period = 1000;
    timer = new Timer();
    interval = Integer.parseInt(secs);
    System.out.println(secs);
    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            System.out.println(setInterval());

        }
    }, delay, period);
}

private static final int setInterval() {
    if (interval == 1)
        timer.cancel();
    return --interval;
}
}

From there, you just need to get the time until Christmas.

From how to make a countdown timer in java

Community
  • 1
  • 1
Jase Pellerin
  • 397
  • 1
  • 2
  • 13
0

You need to put Calendar today = Calendar.getInstance(); into while loop to make it work. You need to compare current time, not the the time when you created the instance!!!!

instead of using loop it is good practice to use Executor class.

public static void main(String[] args) {
    Calendar thatDay = Calendar.getInstance();
    thatDay.set(Calendar.DAY_OF_MONTH, 25);
    thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
    thatDay.set(Calendar.YEAR, 2014);
    thatDay.set(Calendar.HOUR, 0);
    thatDay.set(Calendar.MINUTE, 0);
    thatDay.set(Calendar.SECOND, 0);
    thatDay.set(Calendar.AM_PM, 0);
    System.out.println(thatDay.getTime());
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
    scheduledExecutorService.scheduleAtFixedRate(new ReadPeriodically(thatDay), 0, 1, TimeUnit.SECONDS);

}

Runnable Class

import java.util.Calendar;

public class ReadPeriodically implements Runnable {

Calendar    thatDay = null;

ReadPeriodically(Calendar thatDay) {
    this.thatDay = thatDay;
}

@Override
public void run() {

    Calendar today = Calendar.getInstance();

    long diff = (thatDay.getTimeInMillis() - today.getTimeInMillis()) / 1000;
    long days = diff / (60 * 60 * 24);
    long hours = diff / (60 * 60) % 24;
    long mins = diff / 60 % 60;
    long secs = diff % 60;

    System.out.println(days + ":" + hours + ":" + mins + ":" + secs);
}

}

and note that you have to set your thatDay to the seconds level, as you want counter to the seconds difference. Hope this will help!!

aadi53
  • 439
  • 4
  • 17