0

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));
    }
}
Y.I
  • 25
  • 5
  • Would you please tell us the error? Best to edit your question and paste the error and stack trace. – NomadMaker Mar 19 '20 at 06:26
  • what kind of error does it show? – tcf01 Mar 19 '20 at 06:28
  • Try using SimpleDateFormat - have a look at this tutorial https://www.baeldung.com/java-simple-date-format – Scary Wombat Mar 19 '20 at 06:34
  • I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And @ScaryWombat I also recommend not to use `SimpleDateFormat`it’s notoriously troublesome. – Ole V.V. Mar 20 '20 at 19:44
  • If using Java 9 or later, I recommend [the excellent answer by Meno Hochschild here](https://stackoverflow.com/a/42344215/5772882). – Ole V.V. Mar 20 '20 at 19:51

1 Answers1

0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

public class StackOverFlow {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine(); // for example  - 2017-02-01
        String b = sc.nextLine(); // 2017-10-05
        List<Date> datesInRange = new ArrayList<>();
        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(a);
        Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(b);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(d1);

        Calendar endCalendar = new GregorianCalendar();
        endCalendar.setTime(d2);

        while (calendar.before(endCalendar)) {
            Date result = calendar.getTime();
            datesInRange.add(result);
            calendar.add(Calendar.DATE, 1);
        }
        System.out.println(datesInRange);

    }
}
Greenbox
  • 113
  • 1
  • 4
  • 13
  • While I believe it’s correct, it’s using a lot of the old, poorly designed and long outdated date and time classes of Java and has skipped the attempt of the question to use java.time, the modern Java date and time API. The poor design of the old classes is illustrated by the fact that this code is more than twice as many lines as in the question. This solution is not recommended. – Ole V.V. Mar 20 '20 at 19:48