1.16.5 Timer "hh: mm: ss"

Discussion in 'Spigot Plugin Development' started by Pierre26, Jul 5, 2021.

  1. Hello, I would like to know how to make a timer like "hh: mm: ss" which starts at 00:00:00 and each second increases by 1 second ...
     
  2. Pace

    Pace Previously Pace1337

    • Funny Funny x 2
  3. Hi,

    you can do that with a scheduler and then format it:

    Code (Java):
               Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                   int duration = 0;
                   @Override
                   public void run() {
                     
                       p.sendMessage(convertTime(duration));
                       duration++;
                     
                   }
               }, 0, 20);
    convertTime method:

    Code (Java):
        public String convertTime(int duration) {
            String string = "";
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            if (duration / 60 / 60 / 24 >= 1) {
                duration -= duration / 60 / 60 / 24 * 60 * 60 * 24;
            }
            if (duration / 60 / 60 >= 1) {
                hours = duration / 60 / 60;
                duration -= duration / 60 / 60 * 60 * 60;
            }
            if (duration / 60 >= 1) {
                minutes = duration / 60;
                duration -= duration / 60 * 60;
            }
            if (duration >= 1)
                seconds = duration;
            if (hours <= 9) {
                string = String.valueOf(string) + "0" + hours + ":";
            } else {
                string = String.valueOf(string) + hours + ":";
            }
            if (minutes <= 9) {
                string = String.valueOf(string) + "0" + minutes + ":";
            } else {
                string = String.valueOf(string) + minutes + ":";
            }
            if (seconds <= 9) {
                string = String.valueOf(string) + "0" + seconds;
            } else {
                string = String.valueOf(string) + seconds;
            }
            return string;
        }
    If you have further questions feel free to ask me :)
     
    • Useful Useful x 1
  4. BukkitRunnable#runTaskTimer()

    Keep track of a variable as well for the seconds that has gone by.

    Edit: Sniped, was typing when the message above me was posted
     
    • Like Like x 1
  5. I would use dates timestamps (see) to do that. So, you could get the difference between now and the date of the start. Here is a method to do what you want:
    Code (Java):
    public String formatDuration(long timestamp) {
        long seconds = timestamp / 1000;
        long absSeconds = Math.abs(seconds);
        String positive = String.format(
            "%02d:%02d:%02d", // If you don't want leading zeros, use "%d" instead
            absSeconds / 3600,
            (absSeconds % 3600) / 60,
            absSeconds % 60);
        return (seconds < 0 ? "-" : "") + positive;
    }
    Usage:
    Code (Java):
    long start = System.currentTimeMillis() - (1000 * 60 * 61); // Removing 1 hour and 2 minutes from the current time to test.
    long current = System.currentTimeMillis();

    System.out.println(formatDuration(current - start)); // Prints 01:02:00
    Source: https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss
     
    #5 TheCaptainSleepy, Jul 5, 2021
    Last edited: Jul 5, 2021
    • Like Like x 1
  6. Thanks for your help ! However I would like to integrate this timer in a scoreboard. How can I do this with the convertTime () function?
     
  7. Strahan

    Benefactor

    That's a very long way to do it. All you needs do is:
    Code (Text):
    int secs = inputSecondsValue % 60;
    int hours = inputSecondsValue / 60;
    int mins = hours % 60;
    hours = hours / 60;
     
    • Agree Agree x 1
  8. Thanks but I would like the timer to start at 00:00:00 and increment by 1 second every second ... The problem is that I can't do this with Duration.between?
     
  9. Don’t use date for a simple timer… long is more than enough.
    OP, You should use this code: (replace int with long for higher timer)
     
  10. I think I misspoke (sorry I'm not English). In fact, when a player is going to do a certain action, in the scoreboard there will be a timer to count the time elapsing since this action. I already did the scoreboard, I'm just missing this timer.
     
  11. Use the code we sent… start the runnable when action is done, and inside the runnable chance the scoreboard value
     
  12. If you are not going to freeze that timer then you could use it. Just think about it, you have your start time and current time. if you do currentTime - startTime that would give you how long the timer has been working (in milliseconds). So, you can simply format milliseconds to hours/minutes/seconds whatever. If you still didn't understand, let me give an example:

    Let's assume that currentTime is 1000 (The current timestamp is way greater than this and every millisecond changes, I just wanted to give an example and make it easy to understand)
    and at that time, you start your timer and save that current timestamp somewhere as "timerStart". So your timerStart is certain and won't change, is always 1000. Since current time always gets bigger, you don't need to count seconds or anything. After -for example- 2 seconds, currentTime is going to be 3000 and when you calculate 3000 - 1000 (currentTime - startTime) that will give you 2000 milliseconds and when you divide that with 1000, you get 2 which is how long the timer has been working (in seconds).

    You are right, long would be better. I am going to edit the method, in case someone would like to use it.
     
    #12 TheCaptainSleepy, Jul 5, 2021
    Last edited: Jul 5, 2021
    • Like Like x 1