I just wanna ask what is the efficient way to get the week numbers of a certain month. I have a function with month number & year as arguments, the return value of the function should be a int array which contain the week numbers of the specific month.(like following...)
public int[] getWeeksOfMonth(int month, int year){
//what's the efficient way to implement this??
}
=WEEKNUM(serial_number,[return_type]) The WEEKNUM uses the following arguments: Serial_number (required argument) – This is an Excel date for which we want to return the week number. When entering the argument, we should enter the date using the DATE function or as a result of other formulas or functions.
Select a blank cell you will return the week number, enter this formula: =WEEKNUM(B1,1), and press the Enter key. See screenshot: Notes: (1) In above formula, B1 contains the date that you want to use.
We can easily count the number of weeks in a month by first counting the number of days in the month. Then, we divide the number of days by 7 since 1 week has 7 days. For example, the month of January has 31 days, so the number of weeks in January would be: 31/7 = 4 weeks + 3 days.
The WEEK_OF_YEAR
attribute of the Calendar
class can be usefull for you.
Create a new date that will be the first day of the given month. Get the week of the year for this day, let say you got start
value.
Create a new date that will be the last day of the given month. Get the week of the year for this day, so now you got end
value.
Finally, create a simple int[]
that will contains values from start
to end
.
Calendar cal = Calendar.getInstance();
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With