5

I have to compare two dates in if/else, the current date and the predefined date (let's say 1 Jan 2011). This was supposed to be simple, but I can't find the way to set the predefined date something like:

Java.util.Date date = new Date("2011-01-01");

How to compare two dates? I really don't know why it's so complicated to do.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
sandalone
  • 41,141
  • 63
  • 222
  • 338
  • [Compare two dates in Java](http://stackoverflow.com/questions/3144387/compare-two-dates-in-java) – Jonathon Faust May 04 '11 at 17:32
  • 2
    @Jonathon: I don't think so. The OP here wants to know how to construct the predefined date. – Matt Ball May 04 '11 at 17:36
  • Agreed, too hasty on my part. I would retract if I could and simply link to it since it's still useful and relevant. – Jonathon Faust May 04 '11 at 17:36
  • 2
    `Date` objects have the `after(...)` and `before(...)` methods. The current date can be obtained by just creating a `new Date()`. To get a date from String, use `SimpleDateFormat`. Else use the calendar method like Matt Ball mentions. – Java Drinker May 04 '11 at 17:56
  • Modern approach: `java.time.LocalDate.parse( "2011-01-01" ).isBefore( LocalDate.now() )` – Basil Bourque Jan 23 '18 at 02:34

3 Answers3

15

Try:

import java.text.SimpleDateFormat;
import java.util.Date;

...

Date today = new Date();
Date predefined = new SimpleDateFormat("yyyy-MM-dd").parse("2011-01-01");

if(today.equals(predefined)) {
    ...
}
JAB
  • 20,783
  • 6
  • 71
  • 80
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • Here's the format string reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Nick Jun 26 '16 at 23:28
7

Use java.util.Calendar.

Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DATE, 1);
Date predefined = cal.getTime();

Date now = new Date();

if (now.after(predefined))
{
    // do something
}
else
{
    // do something else
}

or use JodaTime.

How to compare two dates? I really don't know why it's so complicated to do.

Because calendars/dates/times are really hard to get right, and the Java implementation of Date (and, in part Calendar) is an utter train wreck.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thanks very much for such detailed reply. I learned from it. However, I myself thought of doing it via `Calendar` object, but I fing it's much simpler to do it via `SimpleDateFormat` object. I've already tried with the same object in my code, but I did not use the `parse()` method. I will add plus to your reply, but I will mark the upper one as the answer. – sandalone May 04 '11 at 19:43
  • 1
    @askmo: no problem. Just an FYI, another good option with `DateFormat` is using the static factory methods, like `DateFormat.getDateInstance(DateFormat.SHORT).format(...)` – Matt Ball May 04 '11 at 19:54
  • Thanks. I am aware of that method, but my problem was because I did not use the `parse()` method. – sandalone May 04 '11 at 20:19
  • @askmo: yes, my apologies - I meant `DateFormat.getDateInstance(DateFormat.SHORT).parse(...)` not `...format(...)`! – Matt Ball May 04 '11 at 20:48
3

date.CompareTo(someOtherDate);

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#compareTo(java.util.Date)

debracey
  • 6,517
  • 1
  • 30
  • 56