We have dates in the program as strings. We first parse them into dates and then find the difference between them in milliseconds. Later we convert the millisecond to DAYS and display the results as output.
import java.util.Date; import java.text.SimpleDateFormat; class Main { public static void main(String args[]) { SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy"); String start_date = "01 01 2019"; String end_date = "03 04 2019"; try { Date date_Before = myFormat.parse(start_date); Date date_After = myFormat.parse(end_date); long date_difference = date_After.getTime() - date_Before.getTime(); float final_days = (date_difference / (1000*60*60*24)); System.out.println("Number of Days between Two dates are: "+final_days); } catch (Exception e) { e.printStackTrace(); } } }
Output:-

here’s a way to do it with the better date library added in java 8.
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DurationCalculator {
public static void main(String… args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“dd MM yyyy”);
String start_date = “01 01 2019”;
String end_date = “03 04 2019”;
long final_days = start_date.until(end_date, ChronoUnit.DAYS);
System.out.println(“Number of Days between Two dates are: ” + final_days);
}
}
Thanks for your suggestion..