-3

i am trying to get the difference between two dates. one of the dates was parsed from a string(dateEmployd) and the other date is the current date (currentDate). This is what i did to get the dates...

 public static Date getActiveService(String DtEmplydString){
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");//formater for parsed String date
    Date dateEmployd, currentDate,periodDifference = null;
    try{

        dateEmployd = ft.parse(DtEmplydString);
        currentDate = new Date();
    }catch(ParseException e){
        JOptionPane.showMessageDialog(null, e);
    }
    return periodDifference;
}

Now, i am meant to return periodDifference but i dont know how i would find the difference betweent the two dates (dateEmployd and currentDate) and display it in years or days or a combination of both.

please guys much help is needed. thanks in advance...

spynoble
  • 33
  • 1
  • 9
  • maybe this article help you [http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java](http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java) – Victor Sep 01 '14 at 14:18
  • 1
    How can a difference be a Date? – Bohemian Sep 01 '14 at 14:19
  • So if you have two Dates one day a part you want it to return `2/1/1970 00:00:00` Are you sure it's a Date you want? – Peter Lawrey Sep 01 '14 at 14:24

3 Answers3

0
long diff = date2.getTime() - date1.getTime();
Nathan Norman
  • 145
  • 1
  • 6
  • 2
    That does not answer his question... He wants the difference in days or years, or a combination of both. – Balduz Sep 01 '14 at 14:20
0

Take a look at the jodatime library. They have functions like

DateTime dateEmployd = DateTimeFormat.forPattern("yyyy-MM-dd").parse(DtEmplydString);
Years.yearsBetween(dateEmployd, DateTime.now())

The same for Days.daysbetween, Seconds, Hours etc.

nilsmagnus
  • 2,210
  • 23
  • 33
0

You could try out the Java.Calendar for date functions because Date is deprecated . Here is an example with Cdate comparators

Calendar start = Calendar.getInstance();
   start.setTime(from ( your initial Date object here ) );
   Calendar end = Calendar.getInstance();
   end.setTime(new Date());
   int actualDays = start.compareTo(end);
AntJavaDev
  • 1,204
  • 1
  • 18
  • 24