/
Workdays.java
48 lines (34 loc) · 1.09 KB
/
Workdays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package theweeklychallenge;
/*
Week 138:
https://theweeklychallenge.org/blog/perl-weekly-challenge-138
Task #1: Wrokdays
You are given a year in 4-digits form. Write a script to calculate the total number of workdays in the given year.
*/
import java.time.LocalDate;
import junit.framework.TestCase;
import static junit.framework.Assert.*;
public class Workdays extends TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run(theweeklychallenge.Workdays.class);
}
public void testWorkdays() {
assertEquals(workdays(2021), 261);
assertEquals(workdays(2020), 262);
}
public static int workdays(int year) {
int d = 1;
int m = 1;
int y = year;
int workdays = 0;
while (year == y) {
LocalDate date = LocalDate.of(y, m, d);
if (date.getDayOfWeek().getValue() < 6) workdays += 1;
date = date.plusDays(1);
y = date.getYear();
m = date.getMonthValue();
d = date.getDayOfMonth();
}
return workdays;
}
}