How do I say 5 seconds from now in Java?
Date is almost entirely deprecated and is still there for backward compatibility reasons. If you need to set particular dates or do date arithmetic, use a Calendar:
Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
calendar.add(Calendar.SECOND, 5);
System.out.println(calendar.getTime());
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.