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.

2
  • 1
    What about the Date.equals method? Commented May 16, 2012 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. Commented May 16, 2012 at 19:07

4 Answers 4

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");
    }
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.