本文介紹了根據(jù)周開(kāi)始獲取每月的周數(shù)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我在我的應(yīng)用程序中使用自定義日歷.我讓用戶(hù)可以選擇一周的第一天,這可能是: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
我想獲取一個(gè)月中的周數(shù) - 取決于一周的開(kāi)始時(shí)間,覆蓋周開(kāi)始的默認(rèn)值 - 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);
這篇關(guān)于根據(jù)周開(kāi)始獲取每月的周數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!