本文介紹了根據周開始獲取每月的周數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我在我的應用程序中使用自定義日歷.我讓用戶可以選擇一周的第一天,這可能是:Saturday, Sunday, Monday
I am using a custom calendar in my app. I have given the users option to select the first day of the week which could be: Saturday, Sunday, Monday
我想獲取一個月中的周數 - 取決于一周的開始時間,覆蓋周開始的默認值 - Sunday
.
I want to get the number of weeks in a month - depending on when the week starts, overriding the default value of week start - Sunday
.
代碼:
public int getWeeksOfMonth(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.YEAR, year);
int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
return numOfWeeksInMonth;
}
推薦答案
試試
calendar.setFirstDayOfWeek(Calendar.MONDAY); //sets first day to monday, for example.
在您的情況下,您可能想要這樣做:
In your case you might want to do this:
public int getWeeksOfMonth(int year, int month, int weekStart) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.YEAR, year);
calendar.setFirstDayOfWeek(weekStart);
int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
return numOfWeeksInMonth;
}
并用一行如
int weeks = getWeeksOfMonth(2012, 5, Calendar.WEDNESDAY);
這篇關于根據周開始獲取每月的周數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!