Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Get date 21 days from now

Thread view: 
gimme_this_gimme_that@yahoo.com - 19 May 2006 01:48 GMT
How do I get the date exactly 21 days from now?

Thanks.
daniel.ostermeier@gmail.com - 19 May 2006 02:15 GMT
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
Tony Morris - 19 May 2006 03:03 GMT
> 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;

Signature

Tony Morris
http://tmorris.net/

Stefan Ram - 19 May 2006 03:23 GMT
>> How do I get the date exactly 21 days from now?
>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.Hill - 21 May 2006 20:43 GMT
>>>How do I get the date exactly 21 days from now?
>>
[quoted text clipped - 6 lines]
>   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
Furious George - 22 May 2006 02:25 GMT
> 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) ;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.