Day 12 - Java

Problem Statement: Workday Counter

You are given a Java class WorkdayCounter with a method countWorkdays that takes two strings representing start date (sd) and end date (ed) in ISO_DATE format, and returns the count of workdays (excluding Saturdays and Sundays) between those dates.

Your task is to complete the implementation of the countWorkdays method. Ensure that your implementation passes all the provided test cases.

Complete the function int countWorkdays(String sd, String ed).

Input Format:

The first line contains a string sd representing the start date in ISO_DATE format.

The second line contains a string ed representing the end date in ISO_DATE format.

Output Format:

Print an integer representing the count of workdays between the given start and end dates.

Constraints:

1 ≤ length of sd, ed ≤ 10

Start date ≤ End date

Sample Input:

2024-04-01

2024-04-10

Sample Output:

6

Explanation:

The workdays between April 1, 2024, and April 10, 2024, are April 1, April 2, April 3, April 4, April 7, and April 8. Hence, the output is 6.

Test Cases:

2024-06-18

2024-07-07

14

2024-04-19

2024-06-04

33

2024-01-01

2023-12-31

0


SOLUTION:

import java.time.DayOfWeek;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.util.Scanner;


public class WorkdayCounter {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String sd = scanner.next();

        String ed = scanner.next();

        int days = countWorkdays(sd, ed);

        System.out.println(days);

        scanner.close();

    }


    public static int countWorkdays(String sd, String ed) {

        LocalDate d1 = LocalDate.parse(sd, DateTimeFormatter.ISO_DATE);

        LocalDate d2 = LocalDate.parse(ed, DateTimeFormatter.ISO_DATE);

        int days = 0;

        LocalDate cd = d1;


        while (!cd.isAfter(d2)) {

            if (cd.getDayOfWeek() != DayOfWeek.SATURDAY && cd.getDayOfWeek() != DayOfWeek.SUNDAY) {

                days++;

            }

            cd = cd.plusDays(1);

        }

        return days;

    }

}


Insights:
  • The code utilizes classes from the java.time package introduced in Java 8, such as LocalDate, DayOfWeek, and DateTimeFormatter, for handling dates and times in a modern and efficient way.
  • The input strings representing dates (sd and ed) are parsed into LocalDate objects using the parse method of DateTimeFormatter, with the ISO_DATE format.
  • The countWorkdays method calculates the number of workdays (excluding Saturdays and Sundays) between the provided start and end dates. It iterates through the dates using a while loop, incrementing the days count for each date that is not a Saturday or Sunday.
  • The loop continues until the current date (cd) is not after the end date (d2).
  • The getDayOfWeek() method is used to determine the day of the week for each date (cd). If the day is not Saturday or Sunday, the day is considered a workday, and the days count is incremented.
  • The code demonstrates the immutability of LocalDate objects by using the plusDays method to obtain the next date (cd.plusDays(1)) without modifying the original cd object. By using the plusDays method to iterate through dates, the code ensures efficient date calculation without resorting to complex arithmetic or loops.
Happy Coding on Weekdays and Weekends! :)

Comments