Continuing from this discussion: java program to get the current date without timestamp
What is the most efficient way to get a Date object without the time? Is there any other way than these two?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateWithoutTime = sdf.parse(sdf.format(new Date()));
Calendar cal = Calendar.getInstance();
dateWithoutTime = cal.getTime();
Do you absolutely have to use
java.util.Date? I would thoroughly recommend that you use Joda Time instead. In particular, while Date and Calendar always represent a particular instant in time, with no such concept as "just a date", Joda Time does have a type representing this (
LocalDate). Your code will be much clearer if you're able to use types which represent what you're actually trying to do.
There are many, many other reasons to use Joda Time instead of the built-in types - it's generally a far better API. You can always convert to/from a
java.util.Date at the boundaries of your own code if you need to, e.g. for database interaction.