0

The date is selected by the user using a drop down for year, month and day. I have to compare the user entered date with today's date. Basically see if they are the same date. For example the user entered 02/16/2012. And if today is 02/16/2012 then I have to display a message. How do I do it? I tried using milliseconds but that gives out wrong results.

Do Good
  • 1,285
  • 4
  • 13
  • 17
  • 1
    What about the Date.equals method? – Edwin Dalorzo May 16 '12 at 19:06
  • 5
    almost all of your previous questions have correct answers, you should accept them :) For this one, we really need to see the code you're using to convert the String into a date to start from. – Affe May 16 '12 at 19:07

4 Answers4

0

And what kind of object are you getting back? String, Calendar, Date? You can get that string and compare it, at least that you think you'll have problems with order YYYY MM DD /// DD MM YYY in that case I suggest to create a custom string based on your spec YYYYMMDD and then compare them.

    Date d1 = new Date();
    Date d2 = new Date();

    String day1 = d1.getYear()+"/"+d1.getMonth()+"/"+d1.getDate(); 
    String day2 = d2.getYear()+"/"+d2.getMonth()+"/"+d2.getDate(); 
    if(day1.equals(day2)){
        System.out.println("Same day");
    }
porfiriopartida
  • 1,546
  • 9
  • 17
0

Dates in java are moments in time, with a resolution of "to the millisecond". To compare two dates effectively, you need to first set both dates to the "same time" in hours, minutes, seconds, and milliseconds. All of the "setTime" methods in a java.util.Date are depricated, because they don't function correctly for the internationalization and localization concerns.

To "fix" this, a new class was introduced GregorianCalendar

GregorianCalendar cal1 = new GregorianCalendar(2012, 11, 17);
GregorianCalendar cal2 = new GregorianCalendar(2012, 11, 17);
return cal1.equals(cal2); // will return true

The reason that GregorianCalendar works is related to the hours, minutes, seconds, and milliseconds being initialized to zero in the year, month, day constructor. You can attempt to approximate such with java.util.Date by using deprecated methods like setHours(0); however, eventually this will fail due to a lack of setMillis(0). This means that to use the Date format, you need to grab the milliseconds and perform some integer math to set the milliseconds to zero.

date1.setHours(0);
date1.setMinutes(0);
date1.setSeconds(0);
date1.setTime((date1.getTime() / 1000L) * 1000L);
date2.setHours(0);
date2.setMinutes(0);
date2.setSeconds(0);
date2.setTime((date2.getTime() / 1000L) * 1000L);
return date1.equals(date2); // now should do a calendar date only match

Trust me, just use the Calendar / GregorianCalendar class, it's the way forward (until Java adopts something more sophisticated, like joda time.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

There is two way you can do it. first one is format both the date in same date format or handle date in string format.

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String date1 = sdf.format(selectedDate);
        String date2 = sdf.format(compareDate);
        if(date1.equals(date2)){
        }else{
        }

Or

    Calendar toDate = Calendar.getInstance();
    Calendar nowDate = Calendar.getInstance();
    toDate.set(<set-year>,<set-month>,<set-date->);  
    if(!toDate.before(nowDate))
    //display your report
    else
    // don't display the report
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Above answers are correct but consider using JodaTime - its much simpler and intuitive API. You could set DateTime using with* methods and compare them.

Look at this answer

Community
  • 1
  • 1
Xeon
  • 5,949
  • 5
  • 31
  • 52