Advertisement
anon_1294s

Countdown

Nov 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Countdown {
  4.     public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         System.out.println("What is the day?");
  9.         int dayInput = scan.nextInt();
  10.         dayInput--;
  11.  
  12.         System.out.println("What is the month?");
  13.         int monthInput = scan.nextInt();
  14.         monthInput--;
  15.  
  16.         System.out.println("What is the year?");
  17.         int yearInput = scan.nextInt();
  18.  
  19.  
  20.  
  21.  
  22.         long lastSec = 0;
  23.         while(true){
  24.             Calendar thatDay = Calendar.getInstance();
  25.             thatDay.setTime(new Date(0)); /* reset */
  26.             thatDay.set(Calendar.DAY_OF_MONTH,dayInput);// -1
  27.             thatDay.set(Calendar.MONTH,monthInput); // -1
  28.             thatDay.set(Calendar.YEAR, yearInput);
  29.  
  30.             Calendar today = Calendar.getInstance();
  31.             long diff =  thatDay.getTimeInMillis() - today.getTimeInMillis();
  32.             long diffSec = diff / 1000;
  33.  
  34.             long days = diffSec / SECONDS_IN_A_DAY;
  35.             long secondsDay = diffSec % SECONDS_IN_A_DAY;
  36.             long seconds = secondsDay % 60;
  37.             long minutes = (secondsDay / 60) % 60;
  38.             long hours = (secondsDay / 3600) + 5; // + 5 for the time zone
  39.  
  40.             long sec = System.currentTimeMillis() / 1000;
  41.             if (sec != lastSec) {
  42.                 if (days == 0) {
  43.                     System.out.printf("%d hours, %d minutes and %d seconds left\n", hours, minutes, seconds);
  44.                 } else {
  45.                     System.out.printf("%d days, %d hours, %d minutes and %d seconds left\n", days, hours, minutes, seconds);
  46.                 }
  47.                 lastSec = sec;
  48.             }//If():
  49.         }//While
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement