Do Good
Do Good

Reputation: 1315

Compare dates using Calendar class using java

The user entered date is using a drop down separately for day, month and year. I have to compare the user entered date with today's date and check if it is same day or future day. I am a bit confused about the time portion because I am not interested in time, just the date. How to solve this without using the Date class (I read it is not recommended to use Date class).

Thanks.

Upvotes: 3

Views: 24396

Answers (5)

Mrseguin
Mrseguin

Reputation: 11

Simple calculation :

GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc2.add(GregorianCalendar.DAY_OF_MONTH, 2); // gc2 is 2 days after gc1
long duration = (gc2.getTimeInMillis() - gc1.getTimeInMillis() )
                         / ( 1000 * 60 * 60 * 24) ;
System.out.println(duration);

-> 2

Upvotes: 1

A_G
A_G

Reputation: 41

Try class DateUtils of library Apache Commons Lang.

This class provides the method truncatedEquals(cal1, cal2, field).

So you can check for equality with a single line of code:

Calendar user = new GregorianCalendar(2012, Calendar.MAY, 17);
Calendar now = Calendar.getInstance();

if(DateUtils.truncatedEquals(user, now, Calendar.DATE)){
    // your code goes here
}

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

You first need to create GregorianCalendar instance representing entered date:

Calendar user = new GregorianCalendar(2012, Calendar.MAY, 17);

And one for "now":

Calendar now = new GregorianCalendar();

This will yield positive value if date is in the future and negative - if in the past:

user.compareTo(now);

Few notes about constructing user object:

  • it uses current JVM time zone, so in my case it is midnight, 17th of May in CEST time zone
  • be careful with months, they are 0-based

Upvotes: 12

Xeon
Xeon

Reputation: 5989

Try this solution:

    int day = 0; //user provided
    int month = 0; //user provided
    int year = 0; //user provided
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONDAY, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    long millisUser = calendar.getTime().getTime();
    long nowMillis = System.currentTimeMillis();
    if(nowMillis < millisUser) {
        ...
    }

Above is check if date is in future.

There is nothing wrong in using java.util.Date You can use:

    int day = 0; //user provided
    int month = 0; //user provided
    int year = 0; //user provided
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONDAY, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    Date userSubmit = calendar.getTime();
    Date now = new Date();
    if(userSubmit.after(now)) {
        ...
    }

But if you want fluent, easy and intuitive API with dates I recommend using JodaTime

Upvotes: 0

ChadNC
ChadNC

Reputation: 2503

Use a gregorian calendar. If you wanted to know the number of days difference between two dates then you could make a method similar to the following.

  public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
  int elapsed = 0;
  GregorianCalendar gc1, gc2;
  if (g2.after(g1)) {
     gc2 = (GregorianCalendar) g2.clone();
     gc1 = (GregorianCalendar) g1.clone();
  }
  else   {
     gc2 = (GregorianCalendar) g1.clone();
     gc1 = (GregorianCalendar) g2.clone();
  }
  gc1.clear(Calendar.MILLISECOND);
  gc1.clear(Calendar.SECOND);
  gc1.clear(Calendar.MINUTE);
  gc1.clear(Calendar.HOUR_OF_DAY);
  gc2.clear(Calendar.MILLISECOND);
  gc2.clear(Calendar.SECOND);
  gc2.clear(Calendar.MINUTE);
  gc2.clear(Calendar.HOUR_OF_DAY);
  while ( gc1.before(gc2) ) {
     gc1.add(Calendar.DATE, 1);
     elapsed++;
  }
  return elapsed;

}

That would return you the difference in the number of days.

Upvotes: 0

Related Questions