This is just a complement to the originally published code. It sets the year, month, and day in the new GregorianCalendar object, instead of clearing everything except Year, Month, and Day in a calendar variable. This has not improved much, but I think itβs clearer to say which fields you copy and not which fields you ignore.
public Date day(Date creation) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(creation);
return new GregorianCalendar(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)).getTime();
}
Personally, I think I will go with the Jod library offered by others.
source
share