In this article, we will learn how to calculate number of Days between two Date instances
1. Number of Days between two LocalDate :
Both the below methods uses ChronoUnit but slightly in a different way,
- Using ChronoUnit.DAYS.between() method
- Using LocalDate.until() method
1.1 Using ChronoUnit.DAYS.between() method :
- ChronoUnit.DAYS.between() method accepts 2 input-arguments of LocalDate instances which calculates difference between them in terms of number of Days
- Note: if 1st argument Date is greater-than 2nd argument Date then result will be negative
CalculateDaysBetweenTwoDates1.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class CalculateDaysBetweenTwoDates1 {
public static void main(String[] args) {
// 1. semester start date
LocalDate startLocalDate = LocalDate.of(2022, Month.SEPTEMBER, 8); // today
System.out.println("Semester start date is :- \n" + startLocalDate);
// 2. semester end date
LocalDate endLocalDate = LocalDate.of(2022, Month.DECEMBER, 22);
System.out.println("\nSemester end date is :- \n" + endLocalDate);
// find difference in DAYS
long diffInDays = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
}
}
Output:
Semester start date is :-
2022-09-08
Semester end date is :-
2022-12-22
Number of Days in above Semester is :-
105
1.2 Using LocalDate.until() method :
- LocalDate.until() method accepts 2 input-arguments –
- 1st argument is the Date to which invoking Date needs to be compared/calculated
- 2nd argument is the ChronoUnit in which result/difference is required like DAYS, MONTHS, HOURS, etc.,
- Note: if invoking Date is greater-than passed/supplied Date then result will be negative
CalculateDaysBetweenTwoDates2.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
public class CalculateDaysBetweenTwoDates2 {
public static void main(String[] args) {
// 1. semester start date
LocalDate localDate1 = LocalDate.of(2023, Month.JANUARY, 17); // today
System.out.println("Semester start date is :- \n" + localDate1);
// 2. semester end date
LocalDate localDate2 = LocalDate.of(2023, Month.JUNE, 5);
System.out.println("\nSemester end date is :- \n" + localDate2);
// find difference in DAYS
long diffInDays = localDate1.until(localDate2, ChronoUnit.DAYS);
System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
}
}
Output:
Semester start date is :-
2023-01-17
Semester end date is :-
2023-06-05
Number of Days in above Semester is :-
139
2. Number of Days between two Dates :
- Using toInstant() and ChronoUnit.DAYS.between() methods
- Using TimeUnit
- Using getTime() method of Date class
- Using parse() and format() methods
2.1 Using toInstant() and ChronoUnit.DAYS.between() methods :
- First, parse the given String to Date using DateFormat/SimpleDateFormat classes
- To find the difference between two Dates, use ChronoUnit.DAYS.between() method after converting Date to Instant using toInstant() method
- ChronoUnit.DAYS.between() method accepts 2 input-arguments of Instant which calculates difference between them in terms of number of Days
- Note: if 1st argument Instant is greater-than 2nd argument Instant then result will be negative
CalculateDaysBetweenTwoDates3.java
package in.bench.resources.java8.localdate.examples;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class CalculateDaysBetweenTwoDates3 {
public static void main(String[] args) throws ParseException {
// Instantiating SimpleDateFormat class
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
// 1. semester start date
String strDate1 = "08-09-2022";
Date date1 = formatter.parse(strDate1); // today's date
System.out.println("Semester start date is :- \n" + date1);
// 2. semester end date
String strDate2 = "23-12-2022";
Date date2 = formatter.parse(strDate2); // future date
System.out.println("\nSemester start date is :- \n" + date2);
// find difference in DAYS
long diffInDays = ChronoUnit.DAYS.between(date1.toInstant(), date2.toInstant());
System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
}
}
Output:
Semester start date is :-
Thu Sep 08 00:00:00 IST 2022
Semester start date is :-
Fri Dec 23 00:00:00 IST 2022
Number of Days in above Semester is :-
106
2.2 Using TimeUnit :
- First, parse the given String to Date using DateFormat/SimpleDateFormat classes
- To find the difference between two Dates, convert Date into milliseconds using getTime() method and calculate their difference in terms of milliseconds
- For conversion from Milliseconds to Days, use TimeUnit.DAYS.convert() method which accepts 2 input-arguments,
- 1st argument is the difference of two Date instances
- 2nd argument is the TimeUnit in which difference is passed
CalculateDaysBetweenTwoDates4.java
package in.bench.resources.java8.localdate.examples;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class CalculateDaysBetweenTwoDates4 {
public static void main(String[] args) throws ParseException {
// Instantiating SimpleDateFormat class
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
// 1. semester start date
String strDate1 = "08-09-2022";
Date date1 = formatter.parse(strDate1); // today's date
System.out.println("Semester start date is :- \n" + date1);
// 2. semester end date
String strDate2 = "23-12-2022";
Date date2 = formatter.parse(strDate2); // future date
System.out.println("\nSemester start date is :- \n" + date2);
// find difference in DAYS
long diff = date2.getTime() - date1.getTime();
long diffInDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);
}
}
Output:
Semester start date is :-
Thu Sep 08 00:00:00 IST 2022
Semester start date is :-
Fri Dec 23 00:00:00 IST 2022
Number of Days in above Semester is :-
106
2.3 Using getTime() method of Date class :
- First, parse the given String to Date using DateFormat/SimpleDateFormat classes
- To find the difference between two Dates, convert Date into milliseconds using getTime() method and calculate their difference in terms of milliseconds
- For conversion from Milliseconds to Days, divide the difference in milliseconds by (1000 * 60 * 60 * 24 = 8,64,00,000) which will return absolute/rounded-off value
CalculateDaysBetweenTwoDates5.java
package in.bench.resources.java8.localdate.examples;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CalculateDaysBetweenTwoDates5 {
public static void main(String[] args) throws ParseException {
// Instantiating SimpleDateFormat class
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
// 1. semester start date
String strDate1 = "08-09-2022";
Date date1 = formatter.parse(strDate1); // today's date
System.out.println("Semester start date is :- \n" + date1);
// 2. semester end date
String strDate2 = "23-12-2022";
Date date2 = formatter.parse(strDate2); // future date
System.out.println("\nSemester start date is :- \n" + date2);
// find difference in DAYS
long diff = date2.getTime() - date1.getTime();
float days = (diff / (1000 * 60 * 60 * 24));
System.out.print("\nNumber of Days in above Semester is :- \n" + Math.round(days));
}
}
Output:
Semester start date is :-
Thu Sep 08 00:00:00 IST 2022
Semester start date is :-
Fri Dec 23 00:00:00 IST 2022
Number of Days in above Semester is :-
106
2.4 Using parse() and format() methods :
- First, parse the given String to Date using DateFormat/SimpleDateFormat classes
- And then convert Date to LocalDate using LocalDate.parse() method
- To find the difference between two Dates, use LocalDate.until() method
- LocalDate.until() method accepts 2 input-arguments –
- 1st argument is the Date to which invoking Date needs to be compared/calculated
- 2nd argument is the ChronoUnit in which result is required like DAYS, MONTHS, HOURS, etc.,
- Note: if invoking Date is greater-than passed/supplied Date then result will be negative
CalculateDaysBetweenTwoDates6.java
package in.bench.resources.java8.localdate.examples;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class CalculateDaysBetweenTwoDates6 {
public static void main(String[] args) throws ParseException {
// 1. Parsing String to java.util.Date
DateFormat formatter1 = new SimpleDateFormat("dd-MM-yyyy");
// 2. Formatting java.util.Date to LocalDate
DateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
// 1. semester start date
String strDate1 = "08-09-2022";
Date date1 = formatter1.parse(strDate1); // today's date
LocalDate localDate1 = LocalDate.parse(formatter2.format(date1));
System.out.println("Semester start date is :- \n" + localDate1);
// 2. semester end date
String strDate2 = "23-12-2022";
Date date2 = formatter1.parse(strDate2); // future date
LocalDate localDate2 = LocalDate.parse(formatter2.format(date2));
System.out.println("\nSemester start date is :- \n" + localDate2);
// find difference in DAYS
long diffInDays = localDate1.until(localDate2, ChronoUnit.DAYS);
System.out.print("\nNumber of Days in above Semester is :- \n" + diffInDays);}
}
Output:
Semester start date is :-
2022-09-08
Semester start date is :-
2022-12-23
Number of Days in above Semester is :-
106
Related Articles :
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- Java 8 – How to calculate number of Days between two LocalDate instances ?
- Java 8 – What are the different ways to calculate difference between 2 Dates ?
- Java 8 – How to get all dates between two LocalDate ?
- Java 8 – How to get next and previous date ?
- Java 8 – How to check whether a given Date is weekend ?
- Java 8 – How to check whether a given Year is leap year ?
- Java 8 – What are all the Temporal Fields supported by LocalDate ?
- Java 8 – What are all the Temporal Units supported by LocalDate ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html
- https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Happy Coding !!
Happy Learning !!