How do I say 5 seconds from now in Java?

后端 未结 11 1786
青春惊慌失措
青春惊慌失措 2020-12-02 15:29

I am looking at the Date documentation and trying to figure out how I can express NOW + 5 seconds. Here\'s some pseudocode:

import java.util.Date
public clas         


        
11条回答
  •  情深已故
    2020-12-02 15:44

    Ignoring Dates and focusing on the question.

    My preference is to use java.util.concurrent.TimeUnit since it adds clarity to my code.

    In Java,

    long now = System.currentTimeMillis();
    

    5 seconds from now using TimeUtil is:

    long nowPlus5Seconds = now + TimeUnit.SECONDS.toMillis(5);
    

    Reference: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html

提交回复
热议问题