Advertisement
svetlozar_kirkov

Days Between Two Dates (Homework)

Jan 27th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Problem7_DaysBetweenTwoDates {
  8.  
  9.     public static void main(String[] args) throws ParseException {
  10.         Scanner input = new Scanner(System.in);
  11.         String start = input.nextLine();
  12.         String end = input.nextLine();
  13.         DateTimeFormatter format = DateTimeFormatter.ofPattern("d-MM-yyyy"); //creates the format for the input date
  14.         LocalDate startDate = LocalDate.parse(start,format);
  15.         LocalDate endDate = LocalDate.parse(end,format);
  16.         int count = 0;
  17.         if (startDate.isAfter(endDate)){  // checking if the starting date is after the end date
  18.             for (LocalDate dt = endDate; dt.isBefore(startDate); dt=dt.plusDays(1) ){
  19.                 count--;
  20.             }
  21.         }
  22.         else {
  23.             for (LocalDate dt = startDate; dt.isBefore(endDate); dt=dt.plusDays(1) ){
  24.                 count++;
  25.             }
  26.         }
  27.         System.out.println(count);
  28.         input.close();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement