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();
}
}
}
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();
}
}
}
Tags :
Difference between two dates in java
How to find the duration of difference between two dates in java
Date difference in java in days
How to find difference between two dates in java using calendar
Difference between two dates in java in days example
Calculating days between two dates with in java
Java date difference in terms of days
Comments
Post a Comment