I read 2 strings from the console in the format - yyyy-mm-dd
, and I want to print the number of days between them. However, when I try to print the result, a strange error occurs
- Error:(12, 43) java: no suitable method found for between(java.util.Date,java.util.Date)
method java.time.temporal.TemporalUnit.between(java.time.temporal.Temporal,java.time.temporal.Temporal) is not applicable
(argument mismatch; java.util.Date cannot be converted to java.time.temporal.Temporal)
method java.time.temporal.ChronoUnit.between(java.time.temporal.Temporal,java.time.temporal.Temporal) is not applicable
(argument mismatch; java.util.Date cannot be converted to java.time.temporal.Temporal)
and I cannot figure out why?
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine(); // for example - 2017-02-01
String b = sc.nextLine(); // 2017-10-05
Date d1 = new Date(String.format(a, "yyyy-mm-dd"));
Date d2 = new Date(String.format(b, "yyyy-mm-dd"));
System.out.println(ChronoUnit.DAYS.between(d1,d2));
}
}