I need today’s date – and zero anything else (” 05/06/08 00:00:00 “)
I’ve tried
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
Date date1 = calendar.getTime();
System.out.println(date1);
Run: (This is seriously messed up)
If the hour on the computer is < 12:00 at noon : Sun Mar 08 00:44:39 IST 2009
If the hour on the computer is > 12:00 at noon : Sun Mar 08 12:46:53 IST 2009
So I gave this up.
All the Date’s setters are deprecated (except the epoch time) – so I don’t want to use them either
The only thing I could think of is
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sDate = dateFormat.format(calendar.getTime());
Date today = dateFormat.parse(sDate);
But this is such a lame code I can’t bring myself to write it.
Any other option?
Thanks!
My standard advice for Java date/time questions: don’t use
java.util.{Calendar,Date}. Use Joda Time. That way you can represent a date as a date (with no associated time zone), instead of a date/time. Or you could use aDateMidnightif that’s what you want to represent. (Be careful of combinations of time zone and date where there is no midnight though…)What do you need to use the
Datewith? If you can get away with changing to use Joda throughout, that’s great. Otherwise, you can use Joda to do what you want and then convert to milliseconds (and then tojava.util.Date) when you really need to.(Michael’s solution when using
Date/Calendaris fine if you really want to stick within a broken API… but I can’t overstate how much better Joda is…)