Thursday, April 16, 2009

Clampin a Java Date Object (Rounding Date to remove hours, minutes, and seconds)

Man. It seems like there should be a better way of doing this, but for now... here's what I came up with:
private static final SimpleDateFormat noHoursFormatter = new SimpleDateFormat("dd-MMM-yyyy");
private static final SimpleDateFormat millisFormatter = new SimpleDateFormat("SSS-mm-hh-dd-MMM-yyyy");

public static Date pushToBeginningOfDay(Date date){
try {
return noHoursFormatter.parse(noHoursFormatter.format(date));
} catch (ParseException e) {
return null;
}
}

public static Date pushToENDOfDay(Date date){
try {
return millisFormatter.parse("999-59-23-"+noHoursFormatter.format(date));
} catch (ParseException e) {
return null;
}
}

No comments:

Post a Comment