Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get week numbers of a certain month? [closed]

Tags:

java

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??
}
like image 546
Mellon Avatar asked Oct 25 '10 11:10

Mellon


People also ask

How do I get the week number in a month in Excel?

=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.

How do I automate the week number in Excel?

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.

How do you calculate week of month?

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.


2 Answers

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.

like image 56
Romain Linsolas Avatar answered Oct 09 '22 22:10

Romain Linsolas


Calendar cal = Calendar.getInstance();

int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);

like image 29
rajeesh Avatar answered Oct 09 '22 22:10

rajeesh