Difference between two dates in Java | Code Factory

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];
try
{
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)
{
e.printStackTrace();
}
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

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
Code Factory

Code Factory

More from Medium

HashMap in Java

How Java Works

Anonymous/Unnamed code blocks in Java

Annotations In Java