Advertisement
shady_obeyd

20.CountWorkingDays

Dec 6th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.text.*;
  4. import java.util.Arrays;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.Scanner;
  8.  
  9. public class CountWorkingDays {
  10.     public static void main(String[] args){
  11.         Scanner s = new Scanner(System.in);
  12.  
  13.         DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
  14.  
  15.         Date[] holidays = {
  16.                 new Date(2000, 1, 1),
  17.                 new Date(2000, 3, 3),
  18.                 new Date(2000, 5, 1),
  19.                 new Date(2000, 5, 6),
  20.                 new Date(2000, 5,24),
  21.                 new Date(2000, 9, 6),
  22.                 new Date(2000, 9, 22),
  23.                 new Date(2000, 10, 1),
  24.                 new Date(2000, 12, 24),
  25.                 new Date(2000, 12, 25),
  26.                 new Date(2000, 12, 26)
  27.  
  28.         };
  29.  
  30.         try {
  31.             Date startDate = format.parse(s.nextLine());
  32.             Date endDate = format.parse(s.nextLine());
  33.  
  34.             Calendar start = Calendar.getInstance();
  35.             start.setTime(startDate);
  36.  
  37.             Calendar end = Calendar.getInstance();
  38.  
  39.             end.setTime(endDate);
  40.  
  41.             int cntr = 0;
  42.  
  43.             for (Date i = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), i = start.getTime()){
  44.                 Calendar c = Calendar.getInstance();
  45.                 c.setTime(i);
  46.                 int currentDay = c.get(Calendar.DAY_OF_WEEK);
  47.  
  48.                 Date currentDate = new Date(2000, i.getMonth(), i.getDate());
  49.  
  50.                 if(!Arrays.asList(holidays).contains(currentDate) && (currentDay != 6 && currentDay != 7)){
  51.                     cntr++;
  52.                 }
  53.             }
  54.  
  55.             System.out.println(cntr);
  56.  
  57.         } catch (ParseException e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement