I’m trying an example:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String date = "03/09/2010"; //30 days from today
Date usefullDate = df.parse(date);
Calendar todaysDatePlus30Days = Calendar.getInstance();
todaysDatePlus30Days.add(Calendar.DAY_OF_YEAR, 30);
System.out.println ("Compared Value: " +
usefullDate.compareTo(todaysDatePlus30Days.getTime()))
the printed value is -1. why is it not printing 0?
I think the problem is that todaysDatePlus30Days.getTime() is bringing the actual time as well. whereas usefullDate‘s time is 00:00:00.
So next question is how can I just get the date?
The date you parse is implicitly midnight of that date. The one you construct will be the current time of day.
Try:
I might have a couple of those constants wrong, and you can do this with Calendar.newInstance() if you like, though the syntax is a little different.