Computing.Net > Forums > Programming > comparing a date object with today

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

comparing a date object with today

Reply to Message Icon

Name: cesar_NZ
Date: November 3, 2008 at 13:31:23 Pacific
OS: solaris
CPU/Ram: na
Product: na
Comment:

hi all,
is there an easy way to compare if a dates object passed in is equal to today ??

my code so far :

today = new Date(); //this has time as well which is causing the problem

datePassedIn = [input]

if(today == datePassedIn){
.
.
}


thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 4, 2008 at 10:05:31 Pacific
Reply:

You don't specify which language you're talking about. I assume it's Java. If it isn't please ignore the rest of this post.

I guess you expected this to be simple, didn't you? After all, Java is supposed to be a easy-to-use language. Well, the best I could come up with was the following. I wonder if anyone still thinks Java is easy to use after seeing this. If anyone has a better way to compare dates, let me know.

Your code won't work because the == sign compares addresses (object IDs) and not the dates. Date.equals() won't work either because it will compare to the millisecond, whereas you just want to compare to the day.
================================================================

public static boolean isToday(Date date) {
Calendar today = Calendar.getInstance();
today.setTime(new Date());

Calendar otherday = Calendar.getInstance();
otherday.setTime(date);

return otherday.get(Calendar.YEAR) == today.get(Calendar.YEAR)
&& otherday.get(Calendar.MONTH) == today.get(Calendar.MONTH)
&& otherday.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH);
}


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: comparing a date object with today

Java- Autofinding date www.computing.net/answers/programming/java-autofinding-date/8842.html

batch file compare creation date of www.computing.net/answers/programming/batch-file-compare-creation-date-of/18002.html

Java Array Of Date Objects...help www.computing.net/answers/programming/java-array-of-date-objectshelp/12294.html