• 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

Programming and manipulation based on date

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I have to find number of days between any given two dates and do some calculations based on it.I thought of three ways to do it(?)
First by converting date into int either as second, minute, hour, date, month, hour by using the Calendar.set() and Calendar.get() methods. Second by converting date as millisecond by using Calendar.set() and Calendar.getTime() and Date.getTime(). Third pass them as string and converting them as integer and doing the calculations. In all the above cases I am planning to use 60*60*24 with proper number of days per month taking leap year also into account.
In other words, I am converting my some method date into int or long and doing the calculations. Whether there is any better way to do it directly using Calendar or Date package ? Am I reinventing the wheel with fancy? Any suggestions regarding this is helpful.
Regards,
Subramanian
[This message has been edited by subramanian_k (edited November 27, 2000).]
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you just use the Date methods: after, before, and compareTo? (And maybe equals, but make sure you know what exactly its checking.)
--Mark
hershey@eyeshake.com
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private int noOfDays(Date date1, Date date2)
{
return (int)( d2.getTime() - d1.getTime())/ (1000 * 60 * 60 * 24));
//(1000 * 60 * 60 * 24) gives the no of seconds in a day
}
Just pass 2 date objects and it will return the no of days between 2 months
 
He's my best friend. Not yours. Mine. You can have this 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