tags:

views:

303

answers:

5

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 class Main {

    public static void main(String args[]) {
         Date now = new Date();
         now.setSeconds(now.getSeconds() + 5);
    }
}
+11  A: 

You can use:

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

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

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.

Jon Skeet
As I press F5, your post populates with more and more useful information, it's magic =/
Sbm007
What about using the Calendar?
Nick Stinemates
Well I couldn't let a java.util.Date question go without mentioning Joda :)
Jon Skeet
@Nick: Calendar is a very tricky API to work with properly, and doesn't let you express the idea of "just a date" (no time) or "a local date/time" etc. Go for Joda :)
Jon Skeet
Well, to add 5 seconds to "now", expressing a date with time seems useful :)
Pascal Thivent
@Sbm007: Jon Skeet invented the term "magic" when he was 3.
Shaharyar
+1 on the Joda time
Jimmy
+1  A: 

I just found this from java docs

import java.util.Calendar;

public class Main {

  public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    System.out.println("Current time : " + now.get(Calendar.HOUR_OF_DAY) + ":"
        + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

    now.add(Calendar.SECOND, 100);
    System.out.println("New time after adding 100 seconds : " + now.get(Calendar.HOUR_OF_DAY) + ":"
        + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
  }
}

Is there a convention I should be aware of?

Nick Stinemates
Wow. Java can be really verbose when it is correctly written. Your variable name was only three letters long!
Karl
+3  A: 

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());
Pascal Thivent
Does almost entirely deprecated mean its going to break if my clients upgrade their JVM? Holy crap... this could be really bad. Why would they kill Date?!
Karl
Very, very rarely is deprecated stuff really removed from the JRE and usually it's only done if the functionality is actively harmful. I think there is no danger that Date will be removed in any of the next few major releases. It's deprecated, because it has some severe drawbacks, but it will stay around.
Joachim Sauer
@Karl I have nothing to add to Joachim's answer.
Pascal Thivent
I accepted this as the answer because the getTime() setTime() methods are considered 'deprecated' by the Java API. This is succinct.
Nick Stinemates
Well, that was my point indeed. So thanks :)
Pascal Thivent
@Karl, as an FYI calendar.getTime() in this example returns a Date. I don't think they're deprecating Date as much as they're deprecating some of its functionality.
Nick Stinemates
+3  A: 

From the one-liner-hacky dep.:

new Date( System.currentTimeMillis() + 5000L)

As I understand it from your example, 'now' is really 'now', and "System.currentTimeMillis()' happens to represent that same 'now' concept :-)

But, yup, for everything more complicated than that the Joda time API rocks.

AlexPat
+1, I probably would have answered this same thing.
Karl
+1, for using KISS principal ( Keep It Simple, Stupid ).
Alexander Pogrebnyak
+3  A: 

As others have pointed out, in Joda it's much easier:

DateTime dt = new DateTime();
DateTime added = dt.plusSeconds(5);

I would strongly recommend you migrate to Joda. Almost any Java date-related question on SO resolves to a Joda recommendation :-) The Joda API is supposed to be the basis of the new standard Java date API (JSR310), so you'll be migrating towards a new standard.

Brian Agnew
"java.util.Date" is to "use Joda Time" as "Random is giving me repeated values" is to "don't keep creating new instances of Random" :)
Jon Skeet