java 如何找到给定月份的星期六和星期日?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9909361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:51:31  来源:igfitidea点击:

How can I find Saturdays and Sundays in A given month?

javaandroiddatecalendar

提问by Venkat

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

我想找到给定月份中的所有周六和周日。我该怎么做?

回答by Jon Skeet

The simplestway 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 surethere 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.

绝对确定有更有效的方法来做到这一点 - 但这是我的开始,并在我发现它太慢时进行优化。

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

请注意,如果您能够使用Joda Time,那将使您的生活变得更加轻松......

回答by Enrique Marcos

Try this:

试试这个:

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Sundays {
    public static void main(String[] args) {
        int year = 2012;

        // put the month you want
        int month = Calendar.JANUARY;

        Calendar cal = new GregorianCalendar(year, month, 1);
        do {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (day == Calendar.SATURDAY || day == Calendar.SUNDAY) {
                System.out.println(cal.get(Calendar.DAY_OF_MONTH));
            }
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }  while (cal.get(Calendar.MONTH) == month);
    }
}

回答by Bear

Base on the previous answer, I have a small modification When u find the first Saturday and Sunday, you can simply add 7 days to it until the month is changed.

基于之前的答案,我有一个小的修改,当你找到第一个星期六和星期日时,你可以简单地添加7天,直到月份改变。

回答by Prabhat Kumar Kashyap

Please go through this method, I faced a lot finally I created my own.

请通过这种方法,我遇到了很多,最后我创建了自己的方法。

public static int getDayDiff(String dateFrom, String dateTo){ // DD/MM/YYYY
    Timestamp tDtF = getTimestampDDmmYYYY(dateFrom);//returning timestamp from / DD/MM/YYYY
    Timestamp tDtT = getTimestampDDmmYYYY(dateTo);
    Calendar dtF = new GregorianCalendar();
    Calendar dtT = new GregorianCalendar();
    dtF.setTimeInMillis(tDtF.getTime());
    dtT.setTimeInMillis(tDtT.getTime());
    int count = 0;
    while(dtF.before(dtT)){
        count++;
        dtF.add(Calendar.DAY_OF_YEAR, 1);
    }
    return count;
}

public static int countDateSatOrSun(String dateFrom, String dateTo){//DD/MM/YYYY
    Timestamp tDtF = getTimestampDDmmYYYY(dateFrom);//returning timestamp from / DD/MM/YYYY
    Timestamp tDtT = getTimestampDDmmYYYY(dateTo);
    Calendar dtF = new GregorianCalendar();
    Calendar dtT = new GregorianCalendar();
    dtF.setTimeInMillis(tDtF.getTime());
    dtT.setTimeInMillis(tDtT.getTime());
    int count = 0;
    while(dtF.before(dtT)){
        if((dtF.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY| dtF.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
                count++;
        dtF.add(Calendar.DAY_OF_YEAR, 1);
    }
    return count;
}

It will give you exact result.

它会给你确切的结果。