How do I say 5 seconds from now in Java?

后端 未结 11 1837
青春惊慌失措
青春惊慌失措 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:41

    You can use:

    now.setTime(now.getTime() + 5000);
    

    Date.getTime() and setTime() always refer to milliseconds since January 1st 1970 12am UTC.

    Joda-Time

    However, I would strongly advise you to use Joda Time if you're doing anything more than the very simplest of date/time handling. It's a much more capable and friendly library than the built-in support in Java.

    DateTime later = DateTime.now().plusSeconds( 5 );
    

    java.time

    Joda-Time later inspired the new java.time package built into Java 8.

提交回复
热议问题