2017-04-01 212 views
0

如何在倒計時上設置我的時差?如何倒數計時器

我有我的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) { 


    } 
} 
+0

歡迎堆棧溢出!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –

回答

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