Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Countdown {
- public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("What is the day?");
- int dayInput = scan.nextInt();
- dayInput--;
- System.out.println("What is the month?");
- int monthInput = scan.nextInt();
- monthInput--;
- System.out.println("What is the year?");
- int yearInput = scan.nextInt();
- long lastSec = 0;
- while(true){
- Calendar thatDay = Calendar.getInstance();
- thatDay.setTime(new Date(0)); /* reset */
- thatDay.set(Calendar.DAY_OF_MONTH,dayInput);// -1
- thatDay.set(Calendar.MONTH,monthInput); // -1
- thatDay.set(Calendar.YEAR, yearInput);
- Calendar today = Calendar.getInstance();
- 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) + 5; // + 5 for the time zone
- long sec = System.currentTimeMillis() / 1000;
- if (sec != lastSec) {
- if (days == 0) {
- System.out.printf("%d hours, %d minutes and %d seconds left\n", hours, minutes, seconds);
- } else {
- System.out.printf("%d days, %d hours, %d minutes and %d seconds left\n", days, hours, minutes, seconds);
- }
- lastSec = sec;
- }//If():
- }//While
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement