You can get the number of each day (Monday, Tuesday, etc.) for a specific month in Java using the java.time
package introduced in Java 8. In the following code snippet we will use a loop to iterate the dates in the month. The number of loop is equals to the number of days in the month.
You can run this code to get the count of each day of the week for any specific month and year. Here’s a sample code to achieve that:
package org.kodejava.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.EnumMap;
import java.util.Map;
public class DaysOfWeekInMonthWithLoop {
public static Map<DayOfWeek, Integer> getDaysCountForMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate firstOfMonth = yearMonth.atDay(1);
LocalDate lastOfMonth = yearMonth.atEndOfMonth();
Map<DayOfWeek, Integer> daysCount = new EnumMap<>(DayOfWeek.class);
for (DayOfWeek day : DayOfWeek.values()) {
daysCount.put(day, 0);
}
for (LocalDate date = firstOfMonth; !date.isAfter(lastOfMonth); date = date.plusDays(1)) {
DayOfWeek dayOfWeek = date.getDayOfWeek();
daysCount.put(dayOfWeek, daysCount.get(dayOfWeek) + 1);
}
return daysCount;
}
public static void main(String[] args) {
int year = 2024;
int month = 10; // October
Map<DayOfWeek, Integer> daysCount = getDaysCountForMonth(year, month);
for (Map.Entry<DayOfWeek, Integer> entry : daysCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
MONDAY: 4
TUESDAY: 5
WEDNESDAY: 5
THURSDAY: 5
FRIDAY: 4
SATURDAY: 4
SUNDAY: 4
What we do in the code snippet above:
- YearMonth: Used to represent the year and month. You create an instance of
YearMonth
for the desired year and month. - LocalDate: Represents a date (year, month, day).
firstOfMonth
is the first day of the month andlastOfMonth
is the last day of the month. - EnumMap: A specialized map for use with enum keys, which in this case are days of the week (from
DayOfWeek
enum). - Loop through Dates: Iterate from the first to the last day of the month. For each date, get the day of the week and update the count in the map.
Another solution that we can use is to calculate the number of days using a simple mathematical calculations instead of iterating through the dates of the month.
The refined approach:
- Determine the first day of the month.
- Calculate the base number of times each day appears:
- Each day will appear at least daysInMonth / 7 times because every 7-day week will have each day once.
- The remainder from daysInMonth % 7 will determine how many days are left over from complete weeks, starting from the first day of the month.
Here is how we can implement it:
package org.kodejava.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.EnumMap;
import java.util.Map;
public class DaysOfWeekInMonth {
public static Map<DayOfWeek, Integer> getDaysCountForMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate firstDayOfMonth = yearMonth.atDay(1);
int daysInMonth = yearMonth.lengthOfMonth();
DayOfWeek firstDayOfWeek = firstDayOfMonth.getDayOfWeek();
Map<DayOfWeek, Integer> daysCount = new EnumMap<>(DayOfWeek.class);
int baseCount = daysInMonth / 7;
int extraDays = daysInMonth % 7;
for (DayOfWeek day : DayOfWeek.values()) {
daysCount.put(day, baseCount);
}
for (int i = 0; i < extraDays; i++) {
DayOfWeek currentDay = firstDayOfWeek.plus(i);
daysCount.put(currentDay, daysCount.get(currentDay) + 1);
}
return daysCount;
}
public static void main(String[] args) {
int year = 2024;
int month = 10; // October
Map<DayOfWeek, Integer> daysCount = getDaysCountForMonth(year, month);
for (Map.Entry<DayOfWeek, Integer> entry : daysCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
MONDAY: 4
TUESDAY: 5
WEDNESDAY: 5
THURSDAY: 5
FRIDAY: 4
SATURDAY: 4
SUNDAY: 4
In this approach:
- YearMonth: Represents the year and month.
- LocalDate: Determines the first day of the month.
- DayOfWeek: Identifies the day of the week for the first day of the month.
- EnumMap: Stores the count of each day of the week.
- Base Count and Remainder:
baseCount
: Calculates how many whole weeks (7 days) fit in the month.extraDays
: Calculates the remaining days after accounting for whole weeks.- Initialize each count in the map to
baseCount
. - Add 1 to the first
extraDays
days in the week starting fromfirstDayOfWeek
.
This approach avoids explicitly iterating over each day in the month and relies on mathematical operations to determine the count of each day of the week.
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024