0

I would like to know the best way to compare two Dates in Java , such as in this following scenario:

Date date = new Date(time);
Date now = new Date();
//
long day = 86400000L;
long days = day * 7;
if ((date.getTime() + days) <= now.getTime()) {
     // DO 1
} else {
     // DO 2
}

In other words: I would like to get a Date object, add in some days and compare them.

Question: Is there a good solution for Java 6? And for Java 8 (using the new Time API)? I would like to use the Date.after(Date date) and Date.before(Date date) for this (after adding the days to a Date)

Thank you!

EDIT

Looks like this can be done, according to Coffee. I dont know if there's a better way.

Calendar date = Calendar.getInstance();
date.setTime(new Date(time));
date.add(Calendar.DATE, 7);
Calendar now = Calendar.getInstance();
//
if(now.after(date)) {
     // DO 1
} else {
     // DO 2
}
renatoaraujoc
  • 309
  • 2
  • 13
  • 2
    it's the java.util.Calendar library that you need to familiarize with. The above code looks like pseudocode – Caffeinated Sep 15 '14 at 15:01
  • http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCAQFjAA&url=http%3A%2F%2Fwww.mkyong.com%2Fjava%2Fhow-to-modify-date-time-date-manipulation-java%2F&ei=Gv8WVOitDoKWyATEnIDICw&usg=AFQjCNGZ9vMlqiW0kf6ISm4gG7vVMsB9yA&sig2=1DRSP5JOcmJC8lM0BvOG-A&bvm=bv.75097201,d.aWw – Caffeinated Sep 15 '14 at 15:01
  • 3
    Use Calendar object. – brso05 Sep 15 '14 at 15:02
  • Allmost all methods in java.util.Date have been deprecated; because the API is considered unfixably broken by design; so asking for "I want to use Date" is asking how to get yourself in trouble. Listen to the advice you are given and use the recommended API's. – Durandal Sep 15 '14 at 15:16
  • @Durandal, I am now using the Calendar API. Thanks for your input. – renatoaraujoc Sep 15 '14 at 15:58
  • @Renatinn As long as you operate on dates after 1970 - you can add days by adding millisends and compare milliseconds between dates - this is fair enough, and has better performance (if important in your case) – przemek hertel Sep 15 '14 at 16:00
  • @przemekhertel Probably better performance, but in many cases wrong. Not all days have 86400000ms. – jarnbjo Sep 15 '14 at 16:26
  • @jarnbjo - you're right – przemek hertel Sep 15 '14 at 19:54
  • 1
    possible duplicate of [How can I increment a date by one day in Java?](http://stackoverflow.com/q/428918/642706). And [this](http://stackoverflow.com/q/2507377/642706) and [this](http://stackoverflow.com/q/12087419/642706) and many others. – Basil Bourque Sep 16 '14 at 07:46

2 Answers2

2

You can use Joda-Time and make your life a quite a bit easier:

DateTime dt = new  DateTime(time);
if (dt.plusDays(7).isBeforeNow()) {
 // DO 1
} else {
 // DO 2
}
Andres Olarte
  • 4,260
  • 3
  • 21
  • 44
  • Thank you, I have heard of this library already but I'm always tempted not to bind my application to external libraries. In the future, depending on how stuff gets more complex I might consider it. – renatoaraujoc Sep 15 '14 at 15:14
  • @Renatinn JSR-310 which is based on this library is in Java 8. You could try a back port of JSR-310 which you can drop in Java 8. – Peter Lawrey Sep 15 '14 at 15:25
2

In Java 8:

LocalDate date = LocalDate.ofEpochDay(time); // days!
if (date.plusDays(7).isBefore(LocalDate.now())) {
    // DO 1
} else {
    // DO 2
}

EDIT

If time is in millis:

Date d = new Date();
d.setTime(time);
LocalDate date = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

Explanation is here.

Community
  • 1
  • 1
Krayo
  • 2,415
  • 4
  • 23
  • 43
  • Amazing, looks like a clean solution on Java 8! – renatoaraujoc Sep 15 '14 at 15:41
  • Ok, not so clean: the time is not millis but days! – Krayo Sep 15 '14 at 15:52
  • 1
    Good answer. I suggest also passing the optional `ZoneId` object to the `LocalDate.now` method. Otherwise your JVM’s current default time zone is applied implicitly to determine the date. That default varies, even *during* execution of your app. Better to specify the desired/expected time zone. `ZoneId.of( "America/Montreal" )` – Basil Bourque Sep 01 '16 at 21:43