我想在一个特定月份周数 [英] I want to get number of weeks in a particular month

查看:192
本文介绍了我想在一个特定月份周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在当前month.That包括在上周的previous月的天,在下个月的第一个星期天周数。

事情是这样的:

我想用这个在Android上有一定的GridView适配器,我试图操纵这个code座:

  // FIRST_DAY_OF_WEEK = 0;
    INT LASTDAY = month.getActualMaximum(Calendar.DAY_OF_MONTH);
    INT firstDay =(int)的month.get(Calendar.DAY_OF_WEEK);
    INT firstDayNextMonth = nextMonth.get(Calendar.DAY_OF_WEEK);
    INT lastDayNextMonth = nextMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
    INT nextMonthdays = nextMonth.getMinimalDaysInFirstWeek();

    //图阵列的大小
    如果(firstDay == 1){
        天=新的String [LASTDAY +(FIRST_DAY_OF_WEEK * 6)];
    }

    其他 {
        天=新的String [LASTDAY + firstDay  - (FIRST_DAY_OF_WEEK + 1)]。
    }
 

解决方案

Calendar类的WEEK_OF_YEAR属性可以对您有用。编号: Calendar类

创建一个新的日期将在某月的第一天。获得今年的一周这一天,让我们说你有启动值。

创建一个新的日期,这将是给定月份的最后一天。获得今年的一周这一天,所以现在你有终值。

现在停止 - 启动+ 1应该是一个你想要

您可能需要处理一些角落情况下,当周为一年或类似的重叠。但我认为,一旦你得到这个权利,你可以做到这一点简单的逻辑。

下面是简单的例子code。我想你可以把它变成函数,并传递任何你想要的。

 日历CAL = Calendar.getInstance();
        cal.set(Calendar.YEAR,2013年);
        cal.set(Calendar.MONTH,1);
        cal.set(Calendar.DAY_OF_MONTH,1);
        INT开始= cal.get(Calendar.WEEK_OF_YEAR);
        Log.d(BLA BLA,价值是+启动);
        cal.set(Calendar.YEAR,2013年);
        cal.set(Calendar.MONTH,1);
        cal.set(Calendar.DAY_OF_MONTH,28);
        INT端= cal.get(Calendar.WEEK_OF_YEAR);
        Log.d(BLA BLA,价值是+端);
        Log.d(BLA BLA,数周::+(结束 - 开始+1));
 

拐角情况:

角的情况下才会发生Januaray(例如2010年1月,2000年1月),在这些情况下,大多数的天月是previous年最后一周的一部分,所以一开始的价值将是52和结束值是5,出现这种情况时,如果检查,

 如果(开始>结束){
                numweeks =结束+ 1;
             }
 

PS:把它放到你有不同的测试输入。让我知道如果我能改善它,一旦你发现任何故障。

,甚至更简单的解决方案:

 日历CAL = Calendar.getInstance();
        的for(int i = 0; I< 11;我++){
            cal.set(Calendar.YEAR,2013年);
            cal.set(Calendar.DAY_OF_MONTH,1);
            cal.set(Calendar.MONTH,我);
            INT maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            Log.d(日志,最大周数+ maxWeeknumber);
        }

        1月1号至22日:49:03.591:D / LOG(573):最大周number5
        1月1号至22日:49:03.602:D / LOG(573):最大周number5
        1月1号至22日:49:03.602:D / LOG(573):最大周number6
        1月1号至22日:49:03.671:D / LOG(573):最大周number5
        1月1号至22日:49:03.671:D / LOG(573):最大周number5
        1月1号至22日:49:03.671:D / LOG(573):最大周number6
        1月1号至22日:49:03.681:D / LOG(573):最大周number5
        1月1号至22日:49:03.691:D / LOG(573):最大周number5
        1月1号至22日:49:03.691:D / LOG(573):最大周number5
        1月1号至22日:49:03.711:D / LOG(573):最大周number5
        1月1号至22日:49:03.711:D / LOG(573):最大周number5
 

简单的解决方案正常工作与其他的情况:

 日历CAL = Calendar.getInstance();
        的for(int i = 0; I< 11;我++){
            cal.set(Calendar.YEAR,2010);
            cal.set(Calendar.DAY_OF_MONTH,1);
            cal.set(Calendar.MONTH,我);
            INT maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            //月值从0开始到11 1至12月
            Log.d(日志,单月::+ I +民周刊::+ maxWeeknumber);
        }
 

日志:

  1月1号至22日:59:35.841:D / LOG(629):对于月:: 0民周刊:: 6
        1月1号至22日:59:35.841:D / LOG(629):对于每月:: 1民周刊:: 5
        1月1号至22日:59:35.841:D / LOG(629):对于每月:: 2民周刊:: 5
        1月1号至22日:59:35.841:D / LOG(629):对于月:: 3民周刊:: 5
        1月1号至22日:59:35.841:D / LOG(629):对于月:4民周刊:: 6
        1月1号至22日:59:35.852:D / LOG(629):对于每月:: 5民周刊:: 5
        1月1号至22日:59:35.871:D / LOG(629):对于月:: 6民周刊:: 5
        1月1号至22日:59:35.871:D / LOG(629):对于每月:: 7民周刊:: 5
        1月1号至22日:59:35.871:D / LOG(629):对于每月:: 8民周刊:: 5
        1月1号至22日:59:35.871:D / LOG(629):对于每月:: 9民周刊:: 6
        1月1号至22日:59:35.871:D / LOG(629):对于月:: 10民周刊:: 5
 

i want to get number of weeks in current month.That including the previous month's days in the last week and the first week days in the next month.

Something like this:

I want to use this in a certain gridview adapter on android and am trying to manipulate this code block:

            //FIRST_DAY_OF_WEEK =0;
    int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);
    int firstDay = (int) month.get(Calendar.DAY_OF_WEEK);
    int firstDayNextMonth = nextMonth.get(Calendar.DAY_OF_WEEK);
    int lastDayNextMonth=nextMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
    int nextMonthdays=nextMonth.getMinimalDaysInFirstWeek();

    // figure size of the array
    if (firstDay == 1) {
        days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)];
    }

    else {
        days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)];
    }

解决方案

The WEEK_OF_YEAR attribute of the Calendar class can be useful for you. Ref: Calendar class

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

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 end value.

Now end - start + 1 should be one that you want.

You may have to handle some corner case when the week overlaps to another year or similar. But I think once you get this right, you can do that with simple logic.

Here is simple example code. I think you can make it into function and pass whatever you want.

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 2013);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int start = cal.get(Calendar.WEEK_OF_YEAR);
        Log.d("BLA BLA", "Value is " + start);
        cal.set(Calendar.YEAR, 2013);
        cal.set(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 28);
        int end = cal.get(Calendar.WEEK_OF_YEAR);
        Log.d("BLA BLA", "Value is " + end);
        Log.d("BLA BLA", "Num weeks:: " + (end - start +1 ));

For Corner case:

Corner case will only occur for the month of Januaray (E.g Jan 2010, Jan 2000) in those cases most of the days are part of the last week of the previous year so the start value will be 52 and end value will be 5. When this occurs check if,

          if(start > end) {
                numweeks = end + 1;
             }

P.S: Put it to different testing inputs that you got. Let me know If I can improve on it once you find any fault.

And Even much simpler solution:

        Calendar cal = Calendar.getInstance();
        for(int i = 0 ; i < 11;i++){
            cal.set(Calendar.YEAR, 2013);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.MONTH, i);
            int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            Log.d("LOG","max week number" + maxWeeknumber);
        }

        01-22 01:49:03.591: D/LOG(573): max week number5
        01-22 01:49:03.602: D/LOG(573): max week number5
        01-22 01:49:03.602: D/LOG(573): max week number6
        01-22 01:49:03.671: D/LOG(573): max week number5
        01-22 01:49:03.671: D/LOG(573): max week number5
        01-22 01:49:03.671: D/LOG(573): max week number6
        01-22 01:49:03.681: D/LOG(573): max week number5
        01-22 01:49:03.691: D/LOG(573): max week number5
        01-22 01:49:03.691: D/LOG(573): max week number5
        01-22 01:49:03.711: D/LOG(573): max week number5
        01-22 01:49:03.711: D/LOG(573): max week number5

Simple solution works fine with corner cases:

        Calendar cal = Calendar.getInstance();
        for(int i = 0 ; i < 11;i++){
            cal.set(Calendar.YEAR, 2010);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.MONTH, i);
            int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
            // Month value starts from 0 to 11 for Jan to Dec
            Log.d("LOG","For Month :: "+ i + " Num Week :: " + maxWeeknumber);
        }

Log:

        01-22 01:59:35.841: D/LOG(629): For Month :: 0 Num Week :: 6
        01-22 01:59:35.841: D/LOG(629): For Month :: 1 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 2 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 3 Num Week :: 5
        01-22 01:59:35.841: D/LOG(629): For Month :: 4 Num Week :: 6
        01-22 01:59:35.852: D/LOG(629): For Month :: 5 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 6 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 7 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 8 Num Week :: 5
        01-22 01:59:35.871: D/LOG(629): For Month :: 9 Num Week :: 6
        01-22 01:59:35.871: D/LOG(629): For Month :: 10 Num Week :: 5

这篇关于我想在一个特定月份周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆