1

I'm trying to get the number of days between two dates. I'm working from Agenda, JFXtras.

The start time = getStartTime() returns java.util.Calendar
The end time = getEndTime() returns java.util.Calendar

This is what I've got so far:

void printAppointment(Appointment a) throws ParseException {
        System.out.println(a.getSummary());
        System.out.println(a.getDescription());

        DateFormat formatter = new SimpleDateFormat("yyyy, MMM dd h:mm:ss a z");
        System.out.println(formatter.format(a.getStartTime().getTime()));
        System.out.println(formatter.format(a.getEndTime().getTime()));
        System.out.println(a.getAppointmentGroup());
        System.out.println(a.getLocation());

        // My problem starts here
        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");

        Date date = sdf.parse(formatter.format(a.getStartTime().getTime()));
        cal1.setTime(date);
        date = sdf.parse(formatter.format(a.getEndTime().getTime()));
        cal2.setTime(date);
        // System.out.println("Days difference = " + daysBetween(cal1.getTime(), cal2.getTime()));
        System.out.println("Days difference = can't be set");
    }

    private int daysBetween(Date d1, Date d2) {
        return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
    }

After // My problem starts here I get nothing. The following gets printed:

Another Morning appointment
Another Description of morning appointment
2014, Jun 12 6:00:00 AM PDT
2014, Jun 13 2:00:00 AM PDT
jfxtras.labs.scene.control.Agenda$AppointmentGroupImpl@16e4577
null

Note that the difference in days gets left out.

If anyone could help me out, I'll be more than greatful. Thank you all.

Update:

This is the final working implementation that does the difference in Days, Hours, Minutes and seconds as the attempt below shows.

Pel
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget ti
2014. Jun Mon.9 3:18PM
2014. Jun Wed.11 3:18PM
jfxtras.labs.scene.control.Agenda$AppointmentGroupImpl@12e3872
null
diffSeconds difference = 172800
diffMinutes difference = 2880
diffHours difference = 48
diffDays difference = 2

This is the code that does that

void printAppointment(Appointment a) {
    System.out.println(a.getSummary());
    System.out.println(a.getDescription());

    DateFormat formatter = new SimpleDateFormat("yyyy. MMM EEE.d h:mma");
    System.out.println(formatter.format(a.getStartTime().getTime()));
    System.out.println(formatter.format(a.getEndTime().getTime()));
    System.out.println(a.getAppointmentGroup());
    System.out.println(a.getLocation());
    this.daysBetween(a.getStartTime().getTimeInMillis(), a.getEndTime().getTimeInMillis());

}

private void daysBetween(long t1, long t2) {
    int diff = (int) (t2 - t1);

    long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);

    System.out.println("diffSeconds difference = " + diffSeconds);
    System.out.println("diffMinutes difference = " + diffMinutes);
    System.out.println("diffHours difference = " + diffHours);
    System.out.println("diffDays difference = " + diffDays);
}
user3719487
  • 15
  • 1
  • 1
  • 5
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Feb 09 '18 at 01:21

2 Answers2

5

Instead of passing a Date to daysBetween, pass a long. You can get the current time (since Jan 1 1970) from a Calendar using getTimeInMillis(). Your code will now look as follows:

void printAppointment(Appointment a) throws ParseException {
    System.out.println(a.getSummary());
    System.out.println(a.getDescription());

    System.out.println(formatter.format(a.getStartTime().getTime()));
    System.out.println(formatter.format(a.getEndTime().getTime()));
    System.out.println(a.getAppointmentGroup());
    System.out.println(a.getLocation());


    System.out.println("Days difference = " + daysBetween(a.getStartTime().getTimeInMillis(), a.getEndTime().getTimeInMillis()));
}

private int daysBetween(long t1, long t2) {
    return (int) ((t2 - t1) / (1000 * 60 * 60 * 24));
} 
JodyT
  • 4,324
  • 2
  • 19
  • 31
Simon
  • 1,416
  • 1
  • 15
  • 24
  • Thanks for the help @Simon. I tried it and it works with days. I tried to get the difference in Hours, Minutes and Seconds but was unable. Could you please look at my update above, if you could help please? – user3719487 Jun 08 '14 at 12:04
  • Your code is working for me. What is the issue you are getting? [The code I'm using](http://pastebin.com/e4SC3c7N). PS the calculation can be simplified by using: long diffSeconds = diff / 1000; long diffMinutes = diffSeconds / 60; long diffHours = diffMinutes / 60; long diffDays = diffHours / 24; – Simon Jun 08 '14 at 12:09
  • Sorry about that! Did Clean and Build, which refreshed the project. I think my IDE was not taking in new code edits. Thank you a million. – user3719487 Jun 08 '14 at 12:20
  • Silly IDE's. You're welcome! – Simon Jun 08 '14 at 12:29
  • @stuart, there are always more things you can [edit with a post](http://stackoverflow.com/review/suggested-edits/7558496), so one-character edits won't cut it. And please don't put edit summaries in the post, that's what the edit summaries are for. – gunr2171 Apr 02 '15 at 18:42
1

If you are allowed to use Java 8 than you should have a look at the answer from https://stackoverflow.com/users/288671/vitalii-fedorenko for the question Difference in days between two dates in Java?

There are several reasons for the introduction of the new API in Java 8 - the correct handling of time differences was one of those reasons. (In some timezones there might have been changes affecting the calculation - so just dividing the difference in millis will lead to unexpected results if there are daytime adjustments or similar things)

Community
  • 1
  • 1
mschenk74
  • 3,561
  • 1
  • 21
  • 34