SHARE
TWEET

Java Fundamentals - Holidays Between Two Dates

mark79 Aug 5th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Scanner;
  4.  
  5. public class HolidaysBetweenTwoDates {
  6.  
  7.     public static LocalDate convertStringToDate(String dateString) {
  8.         LocalDate date = null;
  9.         DateTimeFormatter df = DateTimeFormatter.ofPattern("d.M.yyyy");
  10.         try {
  11.             date = LocalDate.parse(dateString, df);
  12.         } catch (Exception ex) {
  13.             System.out.println(ex);
  14.         }
  15.         return date;
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         Scanner scanner = new Scanner(System.in);
  20.  
  21.         LocalDate startDate = convertStringToDate(scanner.nextLine());
  22.         LocalDate endDate = convertStringToDate(scanner.nextLine());
  23.         int weekDayCount = 0;
  24.  
  25.         for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
  26.             boolean isWeekDay = date.getDayOfWeek().getValue() == 6 || date.getDayOfWeek().getValue() == 7;
  27.             if (isWeekDay) {
  28.                 weekDayCount++;
  29.             }
  30.         }
  31.         System.out.println(weekDayCount);
  32.     }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top