2017-04-01 8 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 Question Checklist](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 
関連する問題