Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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);
}

![]() |
![]() |
![]() |

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