This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help reg. Days & Dates... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help reg. Days & Dates..." Watch "Help reg. Days & Dates..." New topic
Author

Help reg. Days & Dates...

Jack Daniel
Ranch Hand

Joined: Jun 15, 2002
Posts: 163
Hey e'body,...
It would be Great if anybody can help me with, how I can find the number of Days between two dates..
like for example # days between 08/07/2003 & 09/15/2003 ..
Which Class do you suggest me to Use.. ( Date,Calendar,DateFormat ??? how?)
Thx a lot for your help.. Appreciate it..
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
The way I usually do something like that is...
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = dateFormat.parse("08/07/2003");
Date d2 = dateFormat.parse("09/15/2003");
long time = d2.getTime() - d1.getTime(); // t dif in ms
// then convert time to the time units you want


- Chris Stehno, SCPJ
Jack Daniel
Ranch Hand

Joined: Jun 15, 2002
Posts: 163
Thanks a lot Chris..
So If you want me to get the # of days ...
I think i'll Go this way :
long time = d2.getTime() - d1.getTime(); // t dif in ms
int days=(int)time/(24*36*100000);
I hope this is going to work always..
Let me know, if by chance i'm going wrong somewhere..
Thx anyway
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help reg. Days & Dates...
 
Similar Threads
days between dates
Extracting months from Date object data
Calculating no. of working days between tow dates
How to get the difference between 2 dates ?
Subtracting Dates in JAVA