Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the time of tomorrow xx:xx

Tags:

java

date

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

like image 559
Okami Avatar asked Sep 26 '08 12:09

Okami


2 Answers

I take the brute force approach

// make it now
Calendar dateCal = Calendar.getInstance();
// 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();
like image 181
Paul Tomblin Avatar answered Oct 04 '22 21:10

Paul Tomblin


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 Dates 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.

like image 35
Brandon DuRette Avatar answered Oct 04 '22 20:10

Brandon DuRette