Given two dates start_date and end_date with time in the form of strings, the task is to find the difference between two dates in Java. Examples:
Input: start_date = “10-01-2018 01:10:20”, end_date = “10-06-2020 06:30:50” Output: 2 years, 152 days, 5 hours, 20 minutes, 30 seconds Input: start_date = “10-01-2019 01:00:00”, end_date = “10-06-2020 06:00:00” Output: 1 years, 152 days, 5 hours, 0 minutes, 0 seconds
Method 1: Use SimpleDateFormat and Date class to find the difference between two dates. Below are the steps:
- Create an object of SimpleDateFormat class and converts string format to date object.
- Parse both start_date and end_date from a string to produce the date, this can be done by using parse() method of the simpleDateFormat class.
- Find the time difference between two dates in milliseconds by using the method getTime() in Java as d2.getTime() – d1.getTime().
- Use date-time mathematical formula to find the difference between two dates. It returns the years, days, hours, minutes, and seconds between the two specifies dates.
- Print the final result.
Below is the implementation of the above approach:
Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class GFG {
static void
findDifference(String start_date,
String end_date)
{
SimpleDateFormat sdf
= new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss" );
try {
Date d1 = sdf.parse(start_date);
Date d2 = sdf.parse(end_date);
long difference_In_Time
= d2.getTime() - d1.getTime();
long difference_In_Seconds
= (difference_In_Time
/ 1000 )
% 60 ;
long difference_In_Minutes
= (difference_In_Time
/ ( 1000 * 60 ))
% 60 ;
long difference_In_Hours
= (difference_In_Time
/ ( 1000 * 60 * 60 ))
% 24 ;
long difference_In_Years
= (difference_In_Time
/ (1000l * 60 * 60 * 24 * 365 ));
long difference_In_Days
= (difference_In_Time
/ ( 1000 * 60 * 60 * 24 ))
% 365 ;
System.out.print(
"Difference "
+ "between two dates is: " );
System.out.println(
difference_In_Years
+ " years, "
+ difference_In_Days
+ " days, "
+ difference_In_Hours
+ " hours, "
+ difference_In_Minutes
+ " minutes, "
+ difference_In_Seconds
+ " seconds" );
}
catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
String start_date
= "10-01-2018 01:10:20" ;
String end_date
= "10-06-2020 06:30:50" ;
findDifference(start_date, end_date);
}
}
|
Output:Difference between two dates is: 2 years, 152 days, 5 hours, 20 minutes, 30 seconds
Method 2: We can find the difference between two dates in a better way by using Java built-in class TimeUnit. Below is the implementation of the above approach:
Java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.util.Date;
class GFG {
static void findDifference(String start_date,
String end_date)
{
SimpleDateFormat sdf
= new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss" );
try {
Date d1 = sdf.parse(start_date);
Date d2 = sdf.parse(end_date);
long difference_In_Time
= d2.getTime() - d1.getTime();
long difference_In_Seconds
= TimeUnit.MILLISECONDS
.toSeconds(difference_In_Time)
% 60 ;
long difference_In_Minutes
= TimeUnit
.MILLISECONDS
.toMinutes(difference_In_Time)
% 60 ;
long difference_In_Hours
= TimeUnit
.MILLISECONDS
.toHours(difference_In_Time)
% 24 ;
long difference_In_Days
= TimeUnit
.MILLISECONDS
.toDays(difference_In_Time)
% 365 ;
long difference_In_Years
= TimeUnit
.MILLISECONDS
.toDays(difference_In_Time)
/ 365l;
System.out.print(
"Difference"
+ " between two dates is: " );
System.out.println(
difference_In_Years
+ " years, "
+ difference_In_Days
+ " days, "
+ difference_In_Hours
+ " hours, "
+ difference_In_Minutes
+ " minutes, "
+ difference_In_Seconds
+ " seconds" );
}
catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
String start_date
= "10-01-2018 01:10:20" ;
String end_date
= "10-06-2020 06:30:50" ;
findDifference(start_date,
end_date);
}
}
|
Output:Difference between two dates is: 2 years, 152 days, 5 hours, 20 minutes, 30 seconds
Method 3: Use Period class in Java to find the difference between two days. The Period.between() method is used to calculate the difference between two dates in years, months, and days. Below is the implementation of the above approach:
Java
import java.time.*;
import java.util.*;
class GFG {
static void
findDifference(LocalDate start_date,
LocalDate end_date)
{
Period diff
= Period
.between(start_date,
end_date);
System.out.print(
"Difference "
+ "between two dates is: " );
System.out.printf(
"%d years, %d months"
+ " and %d days " ,
diff.getYears(),
diff.getMonths(),
diff.getDays());
}
public static void main(String[] args)
{
LocalDate start_date
= LocalDate.of( 2018 , 01 , 10 );
LocalDate end_date
= LocalDate.of( 2020 , 06 , 10 );
findDifference(start_date,
end_date);
}
}
|
Output:Difference between two dates is: 2 years, 5 months and 0 days