• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with calculating difference of 2 date in days

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: the method Calendar::getTimeInMillis() "gets this Calendar's current time as a long."
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic