Java: How to check if a date Object equals yesterday?

Posted by tzippy on Stack Overflow See other posts from Stack Overflow or by tzippy
Published on 2010-06-09T13:21:22Z Indexed on 2010/06/09 13:32 UTC
Read the original article Hit count: 181

Filed under:
|
|

Right now I am using this code

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1, 12, 0, 0); //Sets Calendar to "yeserday, 12am"
if(sdf.format(getDateFromLine(line)).equals(sdf.format(cal.getTime())))                         //getDateFromLine() returns a Date Object that is always at 12pm
{...CODE

There's got to be a smoother way to check if the date returned by getdateFromLine() is yesterday's date. Only the date matters, not the time. That's why I used SimpleDateFormat. Thanks for your help in advance!

© Stack Overflow or respective owner

Related posts about java

Related posts about date