File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Customer Requirements for Developers
this week in the
Java in General
difference in weeks
Joined: Aug 27, 2003
Jun 06, 2008 02:48:00
I want to know the difference between two dates in weeks. How should i do this
for e.g. 01/04/2008-06/06/2008. How many weeks fall between these two dates?
Joined: Feb 20, 2002
Jun 06, 2008 04:26:00
// Get msec from each, and subtract. // d1 - First Date // d2 - Second date long diff = d2.getTime() - d1.getTime(); int Weeks = Abs(diff / (1000 * 60 * 60 * 24*7))
But Since date has been depreciated better use Calender
Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.set(YYYY, MM, DD); calendar2.set(YYYY, MM, DD); long milliseconds1 = calendar1.getTimeInMillis(); long milliseconds2 = calendar2.getTimeInMillis(); int diffWeeks = diff / (7*24 * 60 * 60 * 1000);
Hope it helps
Being defeated is often a temporary condition. Giving up is what makes it permanent.
It is sorta covered in the
JavaRanch Style Guide
subject: difference in weeks
How to calculate difference between two dates in months
difference between two dates
Problem in setting the date in a servlet
Number of weeks between two dates
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015