Make a simple timer in Java

后端 未结 3 737
离开以前
离开以前 2021-01-31 13:06

I can\'t seem to figure out how to make a simple timer in java. All I need it to do is just display time, really. So just a start method, and it keeps counting up like 0:00, 0:0

3条回答
  •  情深已故
    2021-01-31 13:29

    For creating a simple timer as you explained as per your need , it is very easy to write a code for that. I have written the below code for your reference. If you wish you can enhance it.

    import java.util.concurrent.TimeUnit; public class PerfectTimer {

    public static void main(String[] args) throws InterruptedException 
    {
        boolean x=true;
        long displayMinutes=0;
        long starttime=System.currentTimeMillis();
        System.out.println("Timer:");
        while(x)
        {
            TimeUnit.SECONDS.sleep(1);
            long timepassed=System.currentTimeMillis()-starttime;
            long secondspassed=timepassed/1000;
            if(secondspassed==60)
            {
                secondspassed=0;
                starttime=System.currentTimeMillis();
            }
            if((secondspassed%60)==0)
            displayMinutes++;
    
        System.out.println(displayMinutes+"::"+secondspassed);
        }
    
    }
    

    }

提交回复
热议问题