-2

So what I have is 3 variable of the hour minutes and seconds of the time i want to countdown towards.

how can I make it to count towards this time that I have and it will output 3 variables that contains the remaining hours minutes and seconds. can anyone help me?

ItzGuy
  • 9
  • 3
  • 1
    Could you please share your existing implementation? – old greg Dec 12 '20 at 14:06
  • i dont have. i was hoping to get help on how to – ItzGuy Dec 12 '20 at 14:13
  • 1
    Does this answer your question? [How to calculate time difference in java?](https://stackoverflow.com/questions/4927856/how-to-calculate-time-difference-in-java) – old greg Dec 12 '20 at 14:15
  • its counting up i want it to count down – ItzGuy Dec 12 '20 at 14:31
  • Try reversing the parameters. – old greg Dec 12 '20 at 14:42
  • which parameters to reverse? – ItzGuy Dec 12 '20 at 14:46
  • well this is the thing i dont have code i want code to a system that will give me the remaining time between two different hours minutes and seconds – ItzGuy Dec 12 '20 at 15:07
  • Don’t use the accepted answer under the question that @jimmysumshugar linked to. It’s using `SimpleDateFormat` and `Date`, a couple of poorly designed and age-old classes that we should under no circumstances be using any more. There may be other helpful answers there (I haven’t checked yet). – Ole V.V. Dec 12 '20 at 16:50

2 Answers2

2

You can use java.time.LocalTime to create the start and end times and loop through all times between them with the specified interval e.g. the following code loops from the start until the end at an interval of one second.

import java.time.LocalTime;

class Main {
    public static void main(String[] args) throws InterruptedException {
        LocalTime start = LocalTime.of(10, 20, 15);
        LocalTime end = LocalTime.of(10, 15, 20);

        // Count down every second from start until end
        for (LocalTime time = start; time.isAfter(end); time = time.minusSeconds(1)) {
            System.out.println(String.format("%02d:%02d:%02d", time.getHour(), time.getMinute(), time.getSecond()));
            Thread.sleep(1000);
        }
    }
}

Output:

10:20:15
10:20:14
10:20:13
...
...
...

Learn more about the modern date-time API at Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

java.time

I strongly recommend that you use java.time, the modern Java date and time API, for your time work. This should get you started:

    int hour = 14;
    int minute = 23;
    int second = 45;
    
    ZoneId zone = ZoneId.systemDefault();
    ZonedDateTime endTime = LocalDate.now(zone)
            .plusDays(1)
            .atTime(LocalTime.of(hour, minute, second))
            .atZone(zone);
    ZonedDateTime now = ZonedDateTime.now(zone);
    Duration remainingTime = Duration.between(now, endTime);
    
    long hoursRemaining = remainingTime.toHours();
    int minutesRemaining = remainingTime.toMinutesPart();
    int secondsRemaining = remainingTime.toSecondsPart();
    
    System.out.format("Remaining: %d hours %d minutes %d seconds%n",
            hoursRemaining, minutesRemaining, secondsRemaining);

When I ran this snippet in my time zone just now, the output was:

Remaining: 15 hours 55 minutes 27 seconds

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161