How to find working and non-working days for given month and year in Java?



Problem Description :


Write a program in Java that finds working and non-working days for given month and year with proper validations.

Concept :


As we know that working and non-working (Saturday and Sunday) days can be vary based on month and year. For example in Jan 2016, there are 21 working days and 10 non-working days but in Jan 2017, there are 22 working days and 9 non-working days.

Algorithm :


We are using following steps for writing code.

  1. Get month number and number of days in given month.
  2. Set month number to Calendar class constant MONTH and given year to constant YEAR.
  3. Run the loop until the number of days in given month.
  4. In the loop, set day number to Calendar class constant DAY_OF_MONTH and now this becomes our current date.
  5. Find what is the day on that date using Calendar class constant DAY_OF_WEEK. If its value is 1 or 7 then non-working day otherwise working day.
  6. Repeat step 4, 5 and count working and non-working days.

Recommended :




Java Program :


package com.javamultiplex.datetime;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author Rohit Agarwal
 * @category Date and Time Problems
 * @problem Find working and non-working days in given month and year
 *
 */
public class WorkingAndNonWorkingDays {

 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(System.in);
   System.out.println("Enter month name : ");
   String monthName = input.next();
   String year = null;
   int dayAndMonthNumber[] = new int[2];
   int days = 0, workingDays = 0, nonWorkingDays = 0, monthNumber=0;
   // Converting String to uppercase
   monthName = monthName.toUpperCase();
   System.out.println("Enter Year (yyyy) : ");
   year = input.next();
   if (isValidMonth(monthName) && isValidYear(year)) {
    dayAndMonthNumber = getNumberofDaysAndMonthNumberByMonthName(monthName, year);
    days = dayAndMonthNumber[0];
    monthNumber = dayAndMonthNumber[1];
    // Converting String to int.
    int myYear = Integer.parseInt(year);
    // Creating Calendar class instance.
    Calendar cal = Calendar.getInstance();
    // Adding given month and year in Calendar.
    cal.set(Calendar.MONTH, monthNumber);
    cal.set(Calendar.YEAR, myYear);

    /*
     * 
     * Adding all the dates related to given month and year to Calendar.
     * and checking day for each date. 
     * If it is other than Saturday and Sunday then counting as a working day.
     * otherwise non working day.
     * 
     */
    for (int i = 1; i <= days; i++) {
     
     // Adding day of month in Calendar.
     cal.set(Calendar.DAY_OF_MONTH, i);
     /*
      * cal.get(Calendar.DAY_OF_WEEK) = 1 (Sunday)
      * cal.get(Calendar.DAY_OF_WEEK) = 7 (Saturday)
      */
     if (cal.get(Calendar.DAY_OF_WEEK) > 1 && cal.get(Calendar.DAY_OF_WEEK) < 7) {
      workingDays++;
     } else {
      nonWorkingDays++;
     }
    }
    System.out.println("Working days : " + workingDays);
    System.out.println("Non working days :" + nonWorkingDays);
   } else {
    System.out.println("Month name should be valid and Year should be 4 digit long.");
   }
  } finally {
   if (input != null) {
    input.close();
   }
  }

 }

 private static int[] getNumberofDaysAndMonthNumberByMonthName(String monthName, String year) {

  int dayAndMonthNumber[] = new int[2];
  int days = 0;
  int monthNumber = 0;
  switch (monthName) {
  case "JANUARY":
  case "JAN":
   days = 31;
   monthNumber = 0;
   break;
  case "FEBRUARY":
  case "FEB":
   if (isLeapYear(year)) {
    days = 29;
   } else {
    days = 28;
   }
   monthNumber = 1;
   break;
  case "MARCH":
  case "MAR":
   days = 31;
   monthNumber = 2;
   break;
  case "APRIL":
  case "APR":
   days = 30;
   monthNumber = 3;
   break;
  case "MAY":
   days = 31;
   monthNumber = 4;
   break;
  case "JUNE":
   days = 30;
   monthNumber = 5;
   break;
  case "JULY":
   days = 31;
   monthNumber = 6;
   break;
  case "AUGUST":
  case "AUG":
   days = 31;
   monthNumber = 7;
   break;
  case "SEPTEMBER":
  case "SEP":
   days = 30;
   monthNumber = 8;
   break;
  case "OCTOBER":
  case "OCT":
   days = 31;
   monthNumber = 9;
   break;
  case "NOVEMBER":
  case "NOV":
   days = 30;
   monthNumber = 10;
   break;
  case "DECEMBER":
  case "DEC":
   days = 31;
   monthNumber = 11;
   break;
  }
  dayAndMonthNumber[0] = days;
  dayAndMonthNumber[1] = monthNumber;
  return dayAndMonthNumber;
 }

 private static boolean isLeapYear(String year) {

  boolean result = false;
  // Converting String to int.
  int myYear = Integer.parseInt(year);
  if ((myYear % 4 == 0 && myYear % 100 != 0) || myYear % 400 == 0) {
   result = true;
  }
  return result;
 }

 private static boolean isValidMonth(String monthName) {

  boolean result = false;
  String[] fullMonthNames = { "JANUARY", "FEBRUARY", "MARCH", "APRIL",
    "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
    "NOVEMBER", "DECEMBER" };
  String[] halfMonthNames = { "JAN", "FEB", "MAR", "APR", "AUG", "SEP",
    "OCT", "NOV", "DEC" };
  // Converting Array to List
  List<String> fullMonths = new ArrayList<>(Arrays.asList(fullMonthNames));
  List<String> halfMonths = new ArrayList<>(Arrays.asList(halfMonthNames));
  if (fullMonths.contains(monthName) || halfMonths.contains(monthName)) {
   result = true;
  }
  return result;
 }

 private static boolean isValidYear(String year) {

  boolean result = false;
  // Regular expression that matches a String contains 4 digits.
  String pattern = "[0-9]{4}";
  if (year.matches(pattern)) {
   result = true;
  }
  return result;
 }

}



Output :


Output of Java program that finds working and non-working days for given month and year.
Output - How to find working and non-working days for given month and year in Java?

References :






Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Date and Time problems, Working and non-working days in Java, Solution in Java, List, ArrayList, String, Regular expressions, switch statement, if else statement, Calendar, Leap year.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to find working and non-working days for given month and year in Java? How to find working and non-working days for given month and year in Java? Reviewed by Rohit Agarwal on 1/23/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.