Home arrow static arrow Java Programming [Archive] - Counting dates
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Counting dates
This topic has 8 replies on 1 page.

Posts:2
Registered: 7/7/04
Counting dates  
Jul 7, 2004 11:38 AM



 
Hi Folks,

Does anyone know how to write a program for count days? (e.g. How many days between 1/1/99 and 2/3/2002?)

Regards
Ben
 

Posts:24,036
Registered: 2/3/03
Re: Counting dates  
Jul 7, 2004 11:42 AM (reply 1 of 8)



 
[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]

[url=http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html]Working in Java time: Learn the basics of calculating elapsed time in Java[/url]

[url=http://www.javaalmanac.com/egs/java.text/FormatDate.html]Formatting a Date Using a Custom Format[/url]
[url=http://www.javaalmanac.com/egs/java.text/ParseDate.html]Parsing a Date Using a Custom Format[/url]
 

Posts:24,036
Registered: 2/3/03
Re: Counting dates  
Jul 7, 2004 11:43 AM (reply 2 of 8)



 
Although the technical answer to your question would be simply, "yes."

Prune pointless queries
Resist the temptation to close your request for help with semantically-null questions like "Can anyone help me?" or "Is there an answer?" First: if you've written your problem description halfway competently, such tacked-on questions are at best superfluous. Second: because they are superfluous, Forum regulars find them annoying - and are likely to return logically impeccable but dismissive answers like "Yes, you can be helped" and "No, there is no help for you."
Paraphrased from [url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url] by Eric Steven Raymond
 

Posts:648
Registered: 6/26/00
Re: Counting dates  
Jul 7, 2004 11:47 AM (reply 3 of 8)



 
Construct your Date objects: date1 and date2, and assuming date1 is before date2.
/** * assuming date1 is before date2, validate or adjust if needed. * returns a whole day number without fraction. */ public int subtracDate(Date date1, Date date2) {long t1 = date1.getTime();long t2 = date2.getTime(); return (int)((t2 - t1) / 86400000L);}


--
 

Posts:1,183
Registered: 1/23/02
Re: Counting dates  
Jul 7, 2004 11:54 AM (reply 4 of 8)



 
Construct your Date objects: date1 and date2, and assuming date1 is before date2.

Unfortunately this solution is [url http://forum.java.sun.com/thread.jsp?forum=31&thread=534804&start=9&range=1] broken[/url]. Yawmark's links are the best available guidance.
 

Posts:648
Registered: 6/26/00
Re: Counting dates  
Jul 7, 2004 12:01 PM (reply 5 of 8)



 
Construct your Date objects: date1 and date2, and
assuming date1 is before date2.

Unfortunately this solution is [url
http://forum.java.sun.com/thread.jsp?forum=31&thread=53
804&start=9&range=1] broken[/url]. Yawmark's links
are the best available guidance.

Maybe, I just wrote the idea but didn't test it, it is the OP's job to verify it. :)

--
 

Posts:31,095
Registered: 4/30/99
Re: Counting dates  
Jul 7, 2004 12:03 PM (reply 6 of 8)



 
Maybe, I just wrote the idea but didn't test it, it is
the OP's job to verify it. :)

Then you ought to qualify the post with some disclaimers like "This might work, I haven't tried it myself".
 

Posts:648
Registered: 6/26/00
Re: Counting dates  
Jul 7, 2004 12:06 PM (reply 7 of 8)



 
sorry, I already put "validate or adjust if needed." in the code comment. Was typing so quick so may not read correctly. :)

--
 

Posts:2
Registered: 7/7/04
Re: Counting dates  
Jul 8, 2004 10:08 AM (reply 8 of 8)



 
Thank you so much !!! :) I think it will help.
 
This topic has 8 replies on 1 page.