Calculate difference of dates in days

I need to translate two functions from excel into java:

1)
Date1 = 01/30/2002
Today()-Date1 <= 365 * 3

2)
Date2 = 04//07/1997
Today - Date2 <  365 * 6

Thank you !!!



sasha_kvAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
Calendar cal = new GregorianCalendar();
cal.set(2003,1,31);
Date date1 = cal.getTime();
cal.set(1997,7,4);
Date now = new Date();
long d1 = date1.getTime(); //returns in milliseconds
long d2 = now.getTime(); //returns in milliseconds

daysSeparated = (d1-d2) /(1000*60*60*24*365);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
offcourse the result is:
daysSeparated = now - 31/1/2003

but a little correction to my first comment:
>> daysSeparated = (d1-d2) /(1000*60*60*24*365);
I mean:
daysSeparated = (d2-d1) /(1000*60*60*24*365);

0
JK2429Commented:
import java.util.*;

public class CalcAge {

public static void main(String args[])
     {
     Calendar today = new GregorianCalendar();
     Calendar PastDate = new GregorianCalendar();
     
     today.setTime(new Date());

        PastDate.setTime(new Date(96,1,15,12,20));
// You can set the PastDate however you want to


     long InDays = (today.getTime().getTime() - PastDate.getTime().getTime())/86400000;
     int InYears = today.get(Calendar.YEAR)-PastDate.get(Calendar.YEAR);

        if (today.get(Calendar.MONTH) < PastDate.get(Calendar.MONTH))
        AgeInYears--;
   
     System.out.println( "Today is "+today.getTime());
     System.out.println( "Past Date was "+PastDate.getTime());
     System.out.println( "in days is "+AgeInDays);
     System.out.println( "in Years is "+AgeInYears);

     }
}
0
petmagdyCommented:
JK2429,
take care that our page editior is not of encourging of providing complete code with Class and main method ;)
we just provide tips
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.