SHARE
TWEET

CountWorkingDays

a guest Dec 6th, 2017 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.text.ParseException;
  2. import java.time.DayOfWeek;
  3. import java.time.LocalDate;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. public class CountWorkingDays {
  9.     public static void main(String[] args) throws ParseException {
  10.         Scanner scan = new Scanner(System.in);
  11.  
  12.  
  13.         DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy");
  14.         LocalDate firstDate = LocalDate.from(format.parse(scan.nextLine()));
  15.         LocalDate secondDate = LocalDate.from(format.parse(scan.nextLine()));
  16.  
  17.         LocalDate[] holidays = getHolidays();
  18.  
  19.         int count = 0;
  20.  
  21.         for (LocalDate i = firstDate; i.isBefore(secondDate.plusDays(1)); i = i.plusDays(1)) {
  22.  
  23.             boolean isWeekend = i.getDayOfWeek().equals(DayOfWeek.SATURDAY) || i.getDayOfWeek().equals(DayOfWeek.SUNDAY);
  24.             boolean isHoliday = Arrays.asList(holidays).contains(LocalDate.of(2016, i.getMonth(), i.getDayOfMonth()));
  25.  
  26.             boolean isWorkingDay = !isWeekend && !isHoliday;
  27.             if (isWorkingDay) {
  28.                 count++;
  29.             }
  30.         }
  31.  
  32.         System.out.println(count);
  33.     }
  34.  
  35.     public static LocalDate[] getHolidays(){
  36.         LocalDate[] holidays = new LocalDate[] {
  37.                 LocalDate.of(2016, 1, 1),
  38.                 LocalDate.of(2016, 3, 3),
  39.                 LocalDate.of(2016, 5, 1),
  40.                 LocalDate.of(2016, 5, 6),
  41.                 LocalDate.of(2016, 5, 24),
  42.                 LocalDate.of(2016, 9, 6),
  43.                 LocalDate.of(2016, 9, 22),
  44.                 LocalDate.of(2016, 11, 1),
  45.                 LocalDate.of(2016, 12, 24),
  46.                 LocalDate.of(2016, 12, 25),
  47.                 LocalDate.of(2016, 12, 26)
  48.         };
  49.  
  50.         return holidays;
  51.     }
  52. }
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