久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何找到給定月份的周六和周日?

How can I find Saturdays and Sundays in A given month?(如何找到給定月份的周六和周日?)
本文介紹了如何找到給定月份的周六和周日?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想找到給定月份的所有周六和周日.我該怎么做?

I want find all Saturdays and Sundays in A given month. How can I do so?

推薦答案

最簡單的方法是遍歷一個月中的所有日子,并檢查每個日子的星期幾.例如:

The simplest way is to just iterate over all the days in the month, and check the day of week for each of them. For example:

// This takes a 1-based month, e.g. January=1. If you want to use a 0-based
// month, remove the "- 1" later on.
public int countWeekendDays(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    // Note that month is 0-based in calendar, bizarrely.
    calendar.set(year, month - 1, 1);
    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int count = 0;
    for (int day = 1; day <= daysInMonth; day++) {
        calendar.set(year, month - 1, day);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == Calendar.SUNDAY || dayOfweek == Calendar.SATURDAY) {
            count++;
            // Or do whatever you need to with the result.
        }
    }
    return count;
}

絕對肯定有更有效的方法來做到這一點 - 但這是我要開始的,當我發現它太慢時進行優化.

I'm absolutely sure there are far more efficient ways of doing this - but that's what I'd start with, and optimize when I'd found it's too slow.

請注意,如果您能夠使用 Joda Time,您的生活會輕松很多...

Note that if you're able to use Joda Time that would make your life a lot easier...

這篇關于如何找到給定月份的周六和周日?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: www.欧美精品 | www日韩 | 久久成人免费视频 | 九色91在线 | 午夜av片 | 国产主播99 | 欧美不卡视频 | 一区二区视频在线 | 欧美综合视频 | 亚洲欧美精品 | 91欧美日韩 | 一区免费视频 | a级片在线免费观看 | 国精产品99永久一区一区 | 99热思思 | 五月婷婷av | 国产视频www| 午夜视频免费在线观看 | 亚洲精品大片 | 免费一级黄色片 | 三级a毛片| 97视频在线免费观看 | 成年人av | 欧美国产一区二区 | 亚洲精品不卡 | 亚洲精品一区二区三区在线 | 成人亚洲精品 | 国产黄a三级三级三级看三级男男 | 国产91av视频| 成人在线播放视频 | 新av在线 | 日韩在线综合 | 丁香九月婷婷 | 天堂在线中文资源 | 黄色免费av | avtt久久| 亚洲不卡 | 欧美啪啪网| 国产精品人人做人人爽人人添 | 三上悠亚一区二区 | 超碰97av|