我怎样才能倒计时器

时间:2017-04-01 12:01:20

标签: java

如何在倒计时上设置时差?

我和我的java代码有时差,而我想做的就是倒计时。

这是我的java代码。

public class time {

    public void printDifference( Date endDate){


        Date now = new Date();
        Date startDate = now;
        //milliseconds
        long different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n",
            elapsedDays,
            elapsedHours, elapsedMinutes, elapsedSeconds);

    }

    public static void main(String[] args) {


    }
}

1 个答案:

答案 0 :(得分:0)

假设您需要在后台使用倒数计时器,比如测验应用程序,在后台运行单独的线程是一个可行的解决方案。 如果您有其他想法,请告诉我。

public class time implements Runnable{
   private Thread counter;
   //making your local varibles members so that they could be used by      "counter" thread
   private long different, secondsInMilli,  minutesInMilli, 
        hoursInMilli, daysInMilli;

   public void printDifference( Date endDate){
        Date now = new Date();
        Date startDate = now;
        //milliseconds
        this.different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        //Initializing your variables
        this.secondsInMilli = 1000;
        this.minutesInMilli = secondsInMilli * 60;
        this.hoursInMilli = minutesInMilli * 60;
        this.daysInMilli = hoursInMilli * 24;
        this.counter = new Thread(this, "counter");
        counter.start();
  }

  @Override
  public void run() {
      if(Thread.currentThread().getName().equals("counter")){
          try{
              long differentCopy = different;
                  while(differentCopy > 0 ){
                      //dropping your calculations here
                      long elapsedDays = different / daysInMilli;
                      different = different % daysInMilli;

                      long elapsedHours = different / hoursInMilli;
                      different = different % hoursInMilli;

                      long elapsedMinutes = different / minutesInMilli;
                      different = different % minutesInMilli;

                      long elapsedSeconds = different / secondsInMilli;
                      System.out.printf("%d days, %d hours, %d minutes, %d seconds\n",
                            elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
                      different = differentCopy-=1000;
                      Thread.sleep(1000);
                  }
          }catch(InterruptedException | HeadlessException e){
            e.printStackTrace();
          }
      }
  }
  public static void main(String[] args) {
    // TODO code application logic here
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.MILLISECOND, 10000);//current time + 10s
    Date endDate = calendar.getTime();
    time t = new time();
    t.printDifference(endDate);
}}

输出:

startDate : Sat Apr 01 19:30:12 IST 2017
endDate : Sat Apr 01 19:30:22 IST 2017
different : 9999
0 days, 0 hours, 0 minutes, 9 seconds
0 days, 0 hours, 0 minutes, 8 seconds
0 days, 0 hours, 0 minutes, 7 seconds
0 days, 0 hours, 0 minutes, 6 seconds
0 days, 0 hours, 0 minutes, 5 seconds
0 days, 0 hours, 0 minutes, 4 seconds
0 days, 0 hours, 0 minutes, 3 seconds
0 days, 0 hours, 0 minutes, 2 seconds
0 days, 0 hours, 0 minutes, 1 seconds
0 days, 0 hours, 0 minutes, 0 seconds