i want to get number of weeks in current month.That including the previous month\'s days in the last week and the first week days in the next month.
Something like t
if you want to do it by "hand"
use this:
Date.prototype.getMonthWeek = function(monthAdjustement)
{
var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
var returnMessage = (Math.ceil(this.getDate()/7) + Math.floor(((7-firstDay)/7)));
return returnMessage;
}
Where monthAdjustement
is just an Integer adjusting the month you seek compared to Today's date.
We can use the calendar class and then do something like
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
this will return the number of weeks in the current month
use WEEK_OF_MONTH will be better, no cross year issue.
public static void main(String[] args) throws Exception {
for (String m : new String[] { "2012-11", "2012-12", "2013-01", "2013-02", "2013-03",
"2013-04", "2013-05", "2013-06" }) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
Date date = format.parse(m);
Calendar c = Calendar.getInstance();
c.setTime(date);
int start = c.get(Calendar.WEEK_OF_MONTH);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
int end = c.get(Calendar.WEEK_OF_MONTH);
System.out.println(" # of weeks in " + format.format(c.getTime())
+ ": " + (end - start + 1));
}
}
results:
# of weeks in 2012-11: 5
# of weeks in 2012-12: 6
# of weeks in 2013-01: 5
# of weeks in 2013-02: 5
# of weeks in 2013-03: 6
# of weeks in 2013-04: 5
# of weeks in 2013-05: 5
# of weeks in 2013-06: 6
The WEEK_OF_YEAR attribute of the Calendar class can be useful for you. Ref: Calendar class
Create a new date that will be the first day of the some 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.
Now end - start + 1 should be one that you want.
You may have to handle some corner case when the week overlaps to another year or similar. But I think once you get this right, you can do that with simple logic.
Here is simple example code. I think you can make it into function and pass whatever you want.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int start = cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + start);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 28);
int end = cal.get(Calendar.WEEK_OF_YEAR);
//Above line will not work for December Month, use following line for this
int end = isDecember?53:cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + end);
Log.d("BLA BLA", "Num weeks:: " + (end - start +1 ));
For Corner case:
Corner case will only occur for the month of Januaray (E.g Jan 2010, Jan 2000) in those cases most of the days are part of the last week of the previous year so the start value will be 52 and end value will be 5. When this occurs check if,
if(start > end) {
numweeks = end + 1;
}
P.S: Put it to different testing inputs that you got. Let me know If I can improve on it once you find any fault.
And Even much simpler solution:
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
Log.d("LOG","max week number" + maxWeeknumber);
}
01-22 01:49:03.591: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number6
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number6
01-22 01:49:03.681: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
Simple solution works fine with corner cases:
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2010);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
// Month value starts from 0 to 11 for Jan to Dec
Log.d("LOG","For Month :: "+ i + " Num Week :: " + maxWeeknumber);
}
Log:
01-22 01:59:35.841: D/LOG(629): For Month :: 0 Num Week :: 6
01-22 01:59:35.841: D/LOG(629): For Month :: 1 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 2 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 3 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 4 Num Week :: 6
01-22 01:59:35.852: D/LOG(629): For Month :: 5 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 6 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 7 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 8 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 9 Num Week :: 6
01-22 01:59:35.871: D/LOG(629): For Month :: 10 Num Week :: 5
Let's assume we know the first day of the month (Saturday, Sunday etc).
Sunday
and it's not a leap-year, then the (correction edit) February
month will have only 4 weeks. Otherwise, in all the cases, it should have at least 5 and at most 6 weeks.Saturday
, you'll always have 6 weeks (excluding if it is February, when it there will be 5)Friday
, you'll have 6 weeks for months having 31 days and 5 for all others.Finding out the 1st day of the month shouldn't be difficult, you can use Calendar
or other date/time utility library classes. However, I guess there could be cleaner solutions removing the corner cases and unnecessary complexity.