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 Problem with calculating difference of 2 date in days 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 "Problem with calculating difference of 2 date in days" Watch "Problem with calculating difference of 2 date in days" New topic
Author

Problem with calculating difference of 2 date in days

Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi,
I am struck in midest of a problem in calculating difference between 2 dates in day units.
Could anybody suggest me some solution.
Thanks in adv
Sandeep
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What have ya got so far? What object type are you using to represent your dates?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
This is what I have tried .Though isValid() is giving proper result but datediff is not .Please help me out.
Thanks in adv.
Sandeep
import java.util.*;
class DateUtility
{
private String date;
private int year,month,day;
private boolean valid;
public DateUtility(String date)
{
this.date=date;
int day=Integer.parseInt(date.substring(0,date.indexOf('/')));
date=date.substring(date.indexOf('/')+1);
int month=Integer.parseInt(date.substring(0,date.indexOf('/')));
int year=Integer.parseInt(date.substring(date.indexOf('/')+1));
if(valid=isValid(day,month,year))
{
this.day=day;
this.month=month;
this.year=year;

}
else
{
this.day=0;
this.month=0;
this.year=0;
}
}
//Checks the validitiy of date
public boolean isValid(int day,int month,int year)
{
GregorianCalendar obj=new GregorianCalendar(year,(month-1),1);
if(((month<=obj.getActualMaximum(Calendar.MONTH)) && (month>=obj.getActualMinimum(Calendar.MONTH))) && ((day<=obj.getActualMaximum(Calendar.DATE)) && (day>=obj.getActualMinimum(Calendar.DATE) )))
return true;
else
return false;
}
//calculates the difference of 2 dates in days
public static int dateDiff(GregorianCalendar st,GregorianCalendar en)
{
/* Just for testing
GregorianCalendar st=new GregorianCalendar(2002,4,12);
GregorianCalendar en=new GregorianCalendar(2002,5,12);
*/
int count=0;
while(st.before(en))
{
count++;
st.roll(Calendar.DATE,1);
if (st.get(Calendar.DATE)==st.getActualMaximum(Calendar.DATE))
{
st.roll(Calendar.MONTH,1);
st.set(st.get(Calendar.YEAR),st.get(Calendar.MONTH),st.getActualMinimum(Calendar.DATE));
}
if (st.get(Calendar.MONTH)==st.getActualMaximum(Calendar.MONTH))
{
st.roll(Calendar.YEAR,1);
st.set(st.get(Calendar.YEAR),st.getActualMinimum(Calendar.MONTH),st.getActualMinimum(Calendar.DATE));
}
st.set(st.get(Calendar.YEAR),st.get(Calendar.MONTH),st.get(Calendar.DATE));

}

System.out.println("count is \n"+count); //Not giving proper result
return count;
}
}
[ April 21, 2002: Message edited by: Sonu Ghosh ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I understand the way you've tried to solve this problem. If I get a chance, I might try to get it to work.
Otherwise, if I were to tackle this problem, I'd first get the millesecond representation of the two dates, subtract the smaller from the larger, and then convert the differences in milleseconds into days.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note: the method Calendar::getTimeInMillis() "gets this Calendar's current time as a long."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Urgh. The Calendar class is needlessly complex for this (as it is for most applications, really). Look at java.text.SimpleDateFormat for an easy way to convert Strings to Dates - then use the Date currentTimeMillis() method as Dirk says above. If that's not clear, try doing a search for SimpleDateFormat, or for "Date difference" - both have been discussed a lot here.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with calculating difference of 2 date in days
 
Similar Threads
program calculating the mode
LeapCentury--Date fun. fails to identify?
3 Oct 2010 only has 23 hours?
Date?????
Calculating with Java