倒计时不会每秒更新

时间:2014-12-04 18:02:45

标签: java android

我正在开发一款Android应用程序,要求我按天,小时,分钟和秒计算到圣诞节的时间。此代码必须稍后在项目中的模拟器上运行。

我无法及时更新。有时候我可以让代码连续打印出确切的时间,但就是这样。它不会更新和减去秒,分,小时和天。

当我使用try / catch在当前条件下运行代码时,它不会每秒更新时间。是否有一个循环可以每秒更新时间而不会导致编译器有太多的处理?

     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+"")

3 个答案:

答案 0 :(得分:0)

这可能会帮到你。这是一个简单的倒数计时器:

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;
}
}

从那里,你只需要得到圣诞节的时间。

来自how to make a countdown timer in java

答案 1 :(得分:0)

您需要将Calendar today = Calendar.getInstance();置于循环中才能使其正常工作。您需要比较当前时间,而不是创建实例的时间!!!!

而不是使用循环,最好使用 Executor 类。

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);

}

可运行的课程

import java.util.Calendar;

public class ReadPeriodically实现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);
}

}

并注意您必须将thatDay设置为秒级别,因为您需要计算秒数差异。 希望这会有所帮助!!

答案 2 :(得分:-1)

您可以使用倒计时器来执行此操作。试试这个示例应用程序:

http://www.androidhub4you.com/2014/01/countdown-example-in-android-custom.html