Advertisement
fbinnzhivko

Count Working Days

Jul 30th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.temporal.ChronoUnit;
  4. import java.util.Locale;
  5. import java.util.Scanner;
  6.  
  7. public class _24_Count_Working_Days {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         String firstLine = sc.nextLine();
  11.         String secondLine = sc.nextLine();
  12.  
  13.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
  14.         LocalDate firstDate = LocalDate.parse(firstLine, formatter);
  15.         LocalDate secondDate = LocalDate.parse(secondLine, formatter);
  16.  
  17.         long days = ChronoUnit.DAYS.between(firstDate, secondDate);
  18.  
  19.         int workingDays = 0;
  20.         for (int i = 0; i <= days; i++) {
  21.             boolean workday = checkIfHoliday(firstDate);
  22.  
  23.             if (!workday ) {
  24.                 workingDays++;
  25.             }
  26.             firstDate = firstDate.plusDays(1);
  27.         }
  28.         System.out.println(workingDays);
  29.     }
  30.  
  31.     private static boolean checkIfHoliday(LocalDate date) {
  32.         if
  33.                 ((date.getDayOfMonth() == 1 && date.getMonth().getValue() == 1) ||
  34.                 (date.getDayOfMonth() == 3 && date.getMonth().getValue() == 3) ||
  35.                 (date.getDayOfMonth() == 1 && date.getMonth().getValue() == 5) ||
  36.                 (date.getDayOfMonth() == 6 && date.getMonth().getValue() == 5) ||
  37.                 (date.getDayOfMonth() == 24 && date.getMonth().getValue() == 5) ||
  38.                 (date.getDayOfMonth() == 6 && date.getMonth().getValue() == 9) ||
  39.                 (date.getDayOfMonth() == 22 && date.getMonth().getValue() == 9) ||
  40.                 (date.getDayOfMonth() == 1 && date.getMonth().getValue() == 11) ||
  41.                 (date.getDayOfMonth() == 24 && date.getMonth().getValue() == 12) ||
  42.                 (date.getDayOfMonth() == 25 && date.getMonth().getValue() == 12) ||
  43.                 (date.getDayOfMonth() == 26 && date.getMonth().getValue() == 12)
  44.                 ) {
  45.             return true;
  46.         } else if (
  47.                 (date.getDayOfWeek().getValue() == 6) ||
  48.                         (date.getDayOfWeek().getValue() == 7)
  49.                 ) {
  50.             return true;
  51.         } else {
  52.             return false;
  53.         }
  54.  
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement