1

I'd like set a java.util.Date-Objekt to its minimum or maximum time values for a Date (one day). The way I do it currently (for minimum):

Calendar cal = Calendar.getInstance(); cal.setTime(aDate); cal.set(Calendar.HOUR, 0); //... same for minutes, seconds, milliseconds 

But this seems a little bit cumbersome. And how do I set the maximum?

3

1 Answer 1

2

Yeah, use JodaTime's DateMidnight class, as others have suggested.

Or if you can't switch to JodaTime, use DateUtils.truncate(Date, field) and the DateUtils.addXyz() methods from Commons Lang:

Date lastMidnight = DateUtils.truncate(new Date(), Calendar.DATE); Date endOfToday = DateUtils.addMilliseconds( DateUtils.addDays(lastMidnight, 1), -1); 
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, as I already have commons-lang in my classpath. But how would I set the maximum?
@Mulmoth as long as you don't say Here's your Sign ... :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.