It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Finding the difference in days between two dates

Finding the difference in days between two dates

Postby JakeD on Sun Mar 04, 2007 4:18 am

Im writing a metro card program and I want to output how many days a person has left on their card by subtracting the date current date from the expiration date they imputed. However im unsure how to do this. Ive looked at the Date class and the Gregorian calender class. However, java wont allow me to subtract one Gregorian date from another. The same is true for the date class.

What class should I use or what should I do differently to obtain the difference in days between two dates?
JakeD
 
Posts: 3
Joined: Sun Mar 04, 2007 3:54 am
Location: New York

Postby mwa103 on Mon Mar 05, 2007 2:44 pm

I found this site which talks about how to use the Calendar to find such things as the date 30 days from today. Not sure if this will be helpful or not though since you said you already tried the Calendar class.

-Mike

http://www.unix.org.ua/orelly/java-ent/jnut/ch04_04.htm
mwa103
100+ Club
 
Posts: 206
Joined: Mon Mar 07, 2005 10:55 pm

Postby JakeD on Mon Mar 05, 2007 5:48 pm

Thanks for the response, Ill try the site now.
JakeD
 
Posts: 3
Joined: Sun Mar 04, 2007 3:54 am
Location: New York

Hope this helps

Postby ram_2k on Wed Mar 07, 2007 6:16 am

Hi,

you can use java.util.Date compareTo(), it will return whether the given Date is less or greater than the current Date object.

If you want to get the no. of days difference, you have to do it manually by getDate(), getMonth for both the dates and compare it.

To my best knowledge, i dont think there is a method which provides the difference in dates.
ram_2k
 
Posts: 2
Joined: Wed Mar 07, 2007 5:52 am
Location: Bangalore

Postby JakeD on Wed Mar 07, 2007 1:01 pm

Hey, thanks for the help guys. I actually learned how. You have to get the dates, set the hours minutes and seconds to zero using
Code: Select all
Calendar.set(MINUTES, 00)
etc. and then convert them to gregorian calenders using
Code: Select all
GregorianCalendar.setTime(date).


Then you convert both your greograin calendar objects to milliseconds using
Code: Select all
Gregorian.getTimeMillis()
and then you subtract them from there.
JakeD
 
Posts: 3
Joined: Sun Mar 04, 2007 3:54 am
Location: New York


Who is online

Users browsing this forum: No registered users and 1 guest