Get date 21 days from now

  • Thread starter gimme_this_gimme_that
  • Start date
D

daniel.ostermeier

One way to do this is to use the java.util.Calendar. For example:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 21);

Have a look at the Calendar API. It should cover all of the date
related functionality you need.

Regards,
-Daniel
 
T

Tony Morris

How do I get the date exactly 21 days from now?

Thanks.

final long now = System.currentTimeMillis();
final long then = now + 1000 * 60 * 60 * 24 * 21;
 
S

Stefan Ram

Tony Morris said:
final long now = System.currentTimeMillis();
final long then = now + 1000 * 60 * 60 * 24 * 21;

If this time span contains a day that includes a change of
daylight saving time mode, that day might have more or less
than 24 hours. For certain values of »now« (around midnight)
the second »then« might be part of a day, whose date might not
be considered to be »21 days from now«. Far more rare, a
similar effect could be triggered by leap seconds.
 
P

P.Hill

Stefan said:
If this time span contains a day that includes a change of
daylight saving time mode, that day might have more or less
than 24 hours. For certain values of »now« (around midnight)
the second »then« might be part of a day, whose date might not
be considered to be »21 days from now«.


Far more rare, a
similar effect could be triggered by leap seconds.

Stephan's warning about 23 and 25 hour days related to Daylight Savings
is worth noting. The concern with leap seconds is much less likely.
That is unless the hardware on your computer includes the leap second,
your VM understands that, the libraries work with it, and the binary
value you are working with was stored and converted with those rules,
but otherwise you won't have to worry about this particular problem.

A general rule for calendar calculations is to convert all items to the
target units first then combine them. In this case (if you weren't
using the Calendar classes), you'd convert Now to some linear number of
days (Julian Days is a good one) then add 21 and convert back to
whatever form you want.

-Paul
 
F

Furious George

How do I get the date exactly 21 days from now?

Thanks.

No problem. Use my method. It will either return the date exactly 21
days from now or throw a RuntimeException (if there is a problem).

public void java.util.Date daysFromNow ( int days ) {
java.util.Date then=null;
long millis=days*24*60*60*1000;
try {
java.lang.Thread.sleep(millis);
then=new java.util.Date();
}
catch(java.lang.InterruptedException e) {
throw(new RuntimeException(e)) ;
}
return(then) ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top