Difference between two dates in Java | Code Factory

Reference Link : Link

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class Main {final static String[] dates = { "30-06-2014", "31-12-2014" };public static void main(String[] args) {
SimpleDateFormat myFormat = new SimpleDateFormat("dd-MM-yyyy");
String inputString1 = dates[0];
String inputString2 = dates[1];
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date2.getTime() - date1.getTime();
System.out.println("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
catch (ParseException e)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store