• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Difference between two dates

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to find the no.of days between given two dates(MMDDYYYY) This is general question not for certification. Can one provide me with the code.
Regards,
Madhuri
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhuri,
I'm going to transfer this to Java in General(Intermediate)
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Madhuri,
It might not be the most efficient way, but the way you could make sure it works would be to write a little code like this after you parsed your date Strings:
int daysGoneBy = 0;
Calendar calToday = Calendar.getInstance();
calToday.set(2001,07,14);
Calendar calAMonthAgo = Calendar.getInstance();
calAMonthAgo.set(2001,06,12);
while (calToday.after(calAMonthAgo)) {
daysGoneBy++;
calAMonthAgo.add(Calendar.DATE, 1);
}
System.out.println("There were " + daysGoneBy + " days between now and then.");

Hope it helps,
Larry
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also use the time in milliseconds to calculate number of days.
long date1 = dateObject1.getTime();
long date2 = dateObject2.getTime();
long numDays = (date2 - date1) / (24 * 60 * 60 * 1000);
reply
    Bookmark Topic Watch Topic
  • New Topic