3

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.

Do Good
  • 1,285
  • 4
  • 13
  • 17
  • 1
    possible duplicate of [Compare two dates in Java](http://stackoverflow.com/questions/3144387/compare-two-dates-in-java) – Andrew May 16 '12 at 18:13

5 Answers5

12

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
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

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
}
A_G
  • 41
  • 6
1

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

Mrseguin
  • 11
  • 1
0

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

Xeon
  • 5,949
  • 5
  • 31
  • 52
0

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.

ChadNC
  • 2,528
  • 4
  • 25
  • 39