Calculating days between two dates in JAVA (catch ParseException error)

Viewed 568

I’m trying to calculate the number of days between 2 dates. When I run this, it throws the catch (ParseException ex).

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {

        String date1 = "11/11/2020";
        String date2 = "13/11/2020";

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");
            Date date_1 = dateFormat.parse(date1);
            Date date_2 = dateFormat.parse(date2);

            System.out.println(date_1);
            System.out.println(date_2);

            long numberOfDays = date_2.getTime() - date_1.getTime();
            numberOfDays = TimeUnit.DAYS.convert(numberOfDays, TimeUnit.MILLISECONDS);

            System.out.println(numberOfDays);

        } 
        catch (ParseException ex)
        {
            System.out.println("error");
        }
    }
}

other than the catch, there are no errors, so I’m kind of lost.

6 Answers

Don't use Date. Try this.

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String date1 = "11/11/2020";
        String date2 = "13/11/2020";

        LocalDate d1 = LocalDate.parse(date1,dtf);
        LocalDate d2 = LocalDate.parse(date2,dtf);

        long ndays = d1.datesUntil(d2).count();
        System.out.println(ndays);

If you had printed the catched exception:

System.out.println("error: " + ex.getLocalizedMessage());

You would have seen:

error: Unparseable date: "11/11/2020"

The problem is in:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");

change it to:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Since the provided dates are in that format.

Just change this :

 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy");

to that :

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

The date you are trying to parse 11/11/2020 does not match the date format you are trying to use dd-mm-yyyy

You can resolve problems like that on your own by printing out the stack trace inside catch :

ex.printStackTrace();

If Java 8 is an option I'd recommend using the Time API.

Example:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Main
{
    public static void main(String[] args) {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String date1 = "11/11/2020";
        String date2 = "13/11/2020";

        LocalDate firstDate = LocalDate.parse(date1, format);
        LocalDate secondDate = LocalDate.parse(date2, format);

        long days = ChronoUnit.DAYS.between(firstDate, secondDate);
        System.out.println("Days between: " + days);
    }
}

First, you have different formats in input dates and defined format. Therefore, you're getting a parsing exception.

Secondly, We can java.time.Duration class in Java8 for such calculation. Example:

public static void main(String[] args) throws ParseException {
    String date1 = "11/11/2020";
    String date2 = "13/11/2020";
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy");

    Duration duration = Duration.between(format.parse(date1).toInstant(), format.parse(date2).toInstant());
    System.out.println("Days between: " + duration.toDays());
}

Your pattern is incorrect.

You use:

new SimpleDateFormat("dd-mm-yyyy");

but you need use:

new SimpleDateFormat("dd/mm/yyyy");

because yours date's have "/" instead of "-"

Related