• 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

Calculate date difference [Gregorian calender]

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry for this double-post, but I think this topic shouldn't have been posted in the beginners section.
The question was:
Hoe do I calculate the difference between 2 given dates? For example:
The input is 02-02-2001 and 02-01-2002
then the difference is 11 months.
How do I calculate this difference using the Gregorian Calender? (please give a code example)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Preferably, by avoiding the GregorianCalendar entirely. Aside from being needlessly complex, it doesn't actually have methods which will help you - except in the most ineffecient manner possible, which we won't get in to.
All you need is a SimpleDateFormat to parse the date (European style, apparently), plus the Date class and some simple math:

I calculated days rather than months since there is no single precise definition of how long a "month" is. You should of course adapt this to your needs.
This would probably have fit better in "Intermediate" than either Beginner or Advanced - but I don't really feel like moving it again.
[ February 05, 2002: Message edited by: Jim Yingst ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
You might consider that many of the methods of the Date class have been deprecated, and consider looking to the DateFormat class and perhaps the Calendar class. The DateFormatSymbols class might also contain something of use for you.
Good Luck,
-Dirk Schreckmann
[ February 06, 2002: Message edited by: Dirk Schreckmann ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - many specific methods (and even constructors) in Date have been deprecated. But the class as a whole has not been deprecated. In fact, the parse(String) method of DateFormat returns... a Date! As shown above.
 
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
Crazy Me!
Obviously, you're right. I stated that incorrectly. It's correct now.
Thank You
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic