Dear all,
what is an efficient way to get a certain time for the next day in Java? Let's say I want the long for tomorrow 03:30:00. Setting Calendar fields and Date formatting are obvious. Better or smarter ideas, thanks for sharing them!
Okami
Dear all,
what is an efficient way to get a certain time for the next day in Java? Let's say I want the long for tomorrow 03:30:00. Setting Calendar fields and Date formatting are obvious. Better or smarter ideas, thanks for sharing them!
Okami
I'm curious to hear what other people have to say about this one. My own experience is that taking shortcuts (i.e., "better or smarter ideas") with Date
s almost always lands you in trouble. Heck, just using java.util.Date
is asking for trouble.
Added: Many have recommended Joda Time in other Date-related threads.
I would consider using the predefined api the smart way to do this.
Not sure why you wouldn't just use the Calendar object? It's easy and maintainable. I agree about not using Date, pretty much everything useful about it is now deprecated. :(
I take the brute force approach
Calendar dateCal = Calendar.getInstance();
// make it now
dateCal.setTime(new Date());
// make it tomorrow
dateCal.add(Calendar.DAY_OF_YEAR, 1);
// Now set it to the time you want
dateCal.set(Calendar.HOUR_OF_DAY, hours);
dateCal.set(Calendar.MINUTE, minutes);
dateCal.set(Calendar.SECOND, seconds);
dateCal.set(Calendar.MILLISECOND, 0);
return dateCal.getTime();