Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with calculating difference of 2 date in days

 
Sandeep Ghosh
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have ya got so far? What object type are you using to represent your dates?
 
Sandeep Ghosh
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: the method Calendar::getTimeInMillis() "gets this Calendar's current time as a long."
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic