-2

How to calculate the difference between current day and date of the object that user had previously selected from jXDatePicker swing component and that had been added as Date to that object.

In my current code at the last line I'm getting this error message:

no suitable method found for between(Date, Date)

Date currentDate = new Date();          
Date objDate = obj.getSelectedDate(); //getting date the user had 
                                      //previously selected and that been 
                                      //added to object      
long daysDifference = ChronoUnit.DAYS.between(objDate, currentDate);
mat2
  • 11
  • 4
  • 6
    Possible duplicate of [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – afifit Jun 10 '17 at 12:13
  • 1
    @afifit, while that question is also concerned with calculating the difference bewteen two dates, it certainly does not duplicate the compiler error message that this question is asking about. – Ole V.V. Jun 10 '17 at 12:42

2 Answers2

6

You are mixing up the legacy Date-Time code with the new Java 8 Date-Time API. The ChronoUnit.between(Temporal, Temporal) method is from java.time.temporal package which takes two Temporal objects. It does not support the java.util.Date as an argument, hence the compilation error.

Instead of using the legacy Date class, you can use java.time.LocalDate class , and then get the difference between the two dates.

LocalDate currentDate = LocalDate.now(ZoneId.systemDefault());
LocalDate objDate = obj.getSelectedDate();  // object should also store date as LocalDate
long daysDifference = ChronoUnit.DAYS.between(objDate, currentDate);

Update

As per your comment , the objDate can only be a Date, so in this case you can use the inter-operability between the Legacy Date -Time and the Java 8 Date-Time classes.

LocalDateTime currentDate =  LocalDateTime.now(ZoneId.systemDefault());
Instant objIns = obj.getSelectedDate().toInstant();
LocalDateTime objDtTm = LocalDateTime.ofInstant(objIns, ZoneId.systemDefault());
long daysDifference = ChronoUnit.DAYS.between(objDtTm, currentDate);

Update 2

As pointed out by Ole V.V in the comments, to handle Time Zone issues that may occur , calculating the difference using Instant is a better approach.

Instant now = Instant.now();
long daysDifference = obj.getSelectedDate()
                         .toInstant()
                         .until(now, ChronoUnit.DAYS);
Pallavi Sonal
  • 3,661
  • 1
  • 15
  • 19
  • 1
    The problem is that user selects the date from JXDatePicker Swing component that gives date only Date class that is then stored in an object. I cant find the way to convert the Date to LocalDate or how to add Swing component that would directly give date in LocalDate class. – mat2 Jun 10 '17 at 12:55
  • 2
    Thanks for the update. There is a theoretical bad corner case: if someone changes the time zone setting between your two calls to `ZoneId.systemDefault()`, you may get an incorrect result. This can happen, even from a completely different program running in the same JVM. Better just to pass two `Instant` objects to `between()`. – Ole V.V. Jun 10 '17 at 16:02
  • LocalDateTime is not appropriate here as you are losing time zone information. – Basil Bourque Jun 10 '17 at 16:52
3

I agree with Pallavi Sonal’s answer that when you can use the modern java.time classes, you should keep your use of the oldfashioned classes like Date to an absolute minimum. I don’t know JXDatePicker, but I see that its getDate method returns a Date. So the first thing you should do with this is convert it to a more modern thing.

It may seem from your question that in this case you are only concerned with days, not times. If this is correct, Pallavi Sonal is also correct that LocalDate is the correct class for you. I think that this conversion should work for you

    LocalDate selectedDate = jXDatePicker.getDate()
            .toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDate();

This is with a bit of reservation for time zone issues since I don’t know in which time zone the date picker is giving you the date. Once you know that, you can fill in the correct time zone instead of ZoneId.systemDefault().

Unfortunately I am not aware of a date picker component that can give you a LocalDate directly. There could well be one, I hope there is, so it’s probably worth searching for one.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161