Java program to find number of days between two dates
Chapter:
Date and Time
Last Updated:
29-06-2023 04:38:42 UTC
Program:
/* ............... START ............... */
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DaysBetweenDates {
public static void main(String[] args) {
// Define the two dates
LocalDate date1 = LocalDate.of(2023, 6, 15);
LocalDate date2 = LocalDate.of(2023, 6, 28);
// Calculate the number of days between the two dates
long daysBetween = ChronoUnit.DAYS.between(date1, date2);
System.out.println("Number of days between " + date1 + " and " + date2 + " is: " + daysBetween);
}
}
/* ............... END ............... */
Output
Number of days between 2023-06-15 and 2023-06-28 is: 13
Notes:
-
In this example, the program calculates the number of days between June 15, 2023 (date1) and June 28, 2023 (date2). You can modify the values of date1 and date2 variables with your desired dates. The ChronoUnit.DAYS.between() method is used to calculate the number of days between the two dates, and the result is stored in the daysBetween variable.
- When you run this program, it will output the number of days between the two given dates. In this case, the output would be: Number of days between 2023-06-15 and 2023-06-28 is: 13
- Note that this program uses the java.time.LocalDate class, which was introduced in Java 8 as part of the new date and time API. Make sure you're using Java 8 or a later version to run this code.
Tags
Java program to find number of days between two dates #How to Get the Number of Days Between Dates in Java #Number of days between two dates