Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program to calculate the difference between two dates in number of days.
- * The dates are entered at two consecutive lines in format day-month-year.
- * Days are in range [1…31]. Months are in range [1…12].
- * Years are in range [1900…2100]. */
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.time.temporal.ChronoUnit;
- import java.util.Scanner;
- public class _07_DaysBetweenTwoDates_TheBestMethod {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter 2 dates exactly in this format [day-month-year]: ");
- String firstInputStr = scan.next();
- String secondInputStr = scan.next();
- LocalDate startDate = convertSpecificInputDateStringToLocalDate(firstInputStr);
- LocalDate endDate = convertSpecificInputDateStringToLocalDate(secondInputStr);
- long totalDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
- System.out.printf("The number of Days between these 2 dates is: %d !\n",
- totalDaysBetween);
- }
- private static LocalDate convertSpecificInputDateStringToLocalDate(
- String inputStr) {
- // TODO Auto-generated method stub
- DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d-MM-yyyy");
- return LocalDate.parse(inputStr, dateFormat);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement