Java 如何获取某个月的周数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4013917/
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-08-14 08:24:09  来源:igfitidea点击:

How to get week numbers of a certain month?

java

提问by Mellon

I just wanna ask what is the efficient way to get the week numbers of a certain month. I have a function with month number & year as arguments, the return value of the function should be a int array which contain the week numbers of the specific month.(like following...)

我只是想问一下获取某个月份的周数的有效方法是什么。我有一个以月份和年份为参数的函数,函数的返回值应该是一个 int 数组,其中包含特定月份的周数。(如下所示...)

public int[] getWeeksOfMonth(int month, int year){
            //what's the efficient way to implement this??
}

采纳答案by Romain Linsolas

The WEEK_OF_YEARattribute of the Calendarclass can be usefull for you.

类的WEEK_OF_YEAR属性Calendar对您很有用。

Create a new date that will be the first day of the given month. Get the week of the year for this day, let say you got startvalue.

创建一个新日期,作为给定月份的第一天。获取这一天的一年中的一周,假设您获得了start价值。

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 endvalue.

创建一个新日期,作为给定月份的最后一天。获取这一天的一年中的第几周,因此现在您获得了end价值。

Finally, create a simple int[]that will contains values from startto end.

最后,创建一个int[]包含从start到的值的简单对象end

回答by anirvan

  1. Create Calendar instances for the first and last day of the month.
  2. then on each instance invoke the get(Calendar.WEEK_OF_YEAR)
  3. Step [2] will return starting and ending index for the weeks
  1. 为一个月的第一天和最后一天创建日历实例。
  2. 然后在每个实例上调用 get(Calendar.WEEK_OF_YEAR)
  3. 步骤 [2] 将返回几周的开始和结束索引

回答by Grodriguez

I am not sure whether you want to return an array of length equal to the number of days in the month, with each value being the week number for the corresponding day, or an array of all distinctweek numbers for the days in the specified month. Assuming it is the former, this should work:

我不确定您是否要返回一个长度等于该月天数的数组,每个值是相应日期的周数,还是指定月份中所有不同周数的数组. 假设是前者,这应该有效:

public static int[] getWeeksOfMonth(int month, int year)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    int weeks[] = new int[ndays];
    for (int i = 0; i < ndays; i++)
    {
        weeks[i] = cal.get(Calendar.WEEK_OF_YEAR);
        cal.add(Calendar.DATE, 1);
    }
    return weeks;
}

If you want an array of distinct week numbers for the days in the specified month:

如果您想要指定月份中天数的不同周数数组:

public static Integer[] getWeeksOfMonth(int month, int year)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    Set<Integer> weeks = new HashSet<Integer>();
    int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    for (int i = 0; i < ndays; i++)
    {
        weeks.add(cal.get(Calendar.WEEK_OF_YEAR));
        cal.add(Calendar.DATE, 1);
    }

    return weeks.toArray(new Integer[0]);
}

(Note this last example returns an array of Integerobjects, but it is trivial to modify it to return an array of intinstead)

(请注意,最后一个示例返回一个Integer对象数组,但修改它以返回一个对象数组是微不足道的int

回答by rajeesh

Calendar cal = Calendar.getInstance();

日历校准 = Calendar.getInstance();

int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);

int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);

回答by CookieMonssster

I think, that this is much simpler way:

我认为,这是更简单的方法:

Calendar now = Calendar.getInstance();

System.out.println("Current week of month is : " +
            now.get(Calendar.WEEK_OF_MONTH));