Is it possible that i can get todays date , rather than time ??
This is my code
public static void main(String args[]) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
}
Why is todays date is shown as before date ??
public class Ravi {
public static void main(String args[]) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println("Todays Date"+dateFormat.format(date));
List currentObject = new ArrayList();
currentObject.add("2012-09-27");
Date ExpDate = dateFormat.parse((String) currentObject.get(0));
System.out.println("ddd"+ExpDate);
if (ExpDate.before(date)) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
Because Date is always the full current time e.g. 2012.09.27 12:45:23
Whilst your new Formated date is 2012.09.27 00:00:00 therefor the output is correct.
If you want to get false you will need to set hours, minutes and seconds to 0.
Using Calendar:
Using Date:
Comparing Dates with Calendar:
Note you may want to set Hours Minutes and Seconds to 0.