Ivelin_1936

WorkDays

Mar 2nd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.company.countWorkingDays;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Calendar;
  9. import java.util.Locale;
  10.  
  11. public class CountWorkingDays {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  14.  
  15.         SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
  16.  
  17.         Calendar startDate = DateFormatted(reader.readLine(), format);
  18.         Calendar endDate = DateFormatted(reader.readLine(), format);
  19.  
  20.         int[][] holidays = {
  21.                 { 1, 1 },
  22.                 { 3, 3 },
  23.                 { 1, 5 },
  24.                 { 6, 5 },
  25.                 { 24, 5 },
  26.                 { 6, 9 },
  27.                 { 22, 9 },
  28.                 { 1, 11 },
  29.                 { 24, 12 },
  30.                 { 25, 12 },
  31.                 { 26, 12 }
  32.         };
  33.  
  34.         SimpleDateFormat formatDayNumber = new SimpleDateFormat("dd", Locale.ENGLISH);
  35.         SimpleDateFormat formatMonthNumber = new SimpleDateFormat("MM", Locale.ENGLISH);
  36.         SimpleDateFormat formatDayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH);
  37.         int workingDays = 0;
  38.         boolean lastLoop = false;
  39.  
  40.         while (true) {
  41.             if (lastLoop) {
  42.                 break;
  43.             }
  44.  
  45.             if (startDate.compareTo(endDate) == 0) {
  46.                 lastLoop = true;
  47.             }
  48.  
  49.             if (IsWorkingDay(startDate, holidays, formatDayNumber, formatMonthNumber, formatDayOfWeek)) {
  50.                 workingDays++;
  51.             }
  52.  
  53.             startDate.add(Calendar.DATE, 1);
  54.         }
  55.  
  56.         System.out.println(workingDays);
  57.     }
  58.  
  59.     private static boolean IsWorkingDay(Calendar date, int[][] holidays, SimpleDateFormat formatDayNumber, SimpleDateFormat formatMonthNumber, SimpleDateFormat formatDayOfWeek) {
  60.         int t = date.get(Calendar.DAY_OF_WEEK);
  61.         if (date.get(Calendar.DAY_OF_WEEK) == 1 || date.get(Calendar.DAY_OF_WEEK) == 7) {
  62.             return false;
  63.         }
  64.  
  65.         for (int i = 0; i < holidays.length; i++) {
  66.             int day = date.get(Calendar.DAY_OF_MONTH);
  67.             int month = date.get(Calendar.MONTH);
  68.             if (date.get(Calendar.DAY_OF_MONTH) == holidays[i][0] && date.get(Calendar.MONTH) == holidays[i][1] - 1) {
  69.                 return false;
  70.             }
  71.         }
  72.  
  73.         return true;
  74.     }
  75.  
  76.     private static Calendar DateFormatted(String string, SimpleDateFormat format) {
  77.         Calendar date = Calendar.getInstance();
  78.  
  79.         try {
  80.             date.setTime(format.parse(string));// all done
  81.         } catch (ParseException e) {
  82.             e.printStackTrace();
  83.         }
  84.  
  85.         return date;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment