首页>Program>source

我想获得当月的周数,包括上个月的上个月的天数和下个月的第一周的天数。

类似这样的东西:

我想在Android上的某个gridview适配器中使用它,并试图操纵此代码块:

           //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)];
    }
最新回答
  • 14天前
    1 #

    Calendar类的wEEK_OF_yEAR属性可能对您有用.参考:日历类

    创建一个新的日期,该日期将是某个月的第一天.获取这一天的一年中的某个星期,假设您获得了开始value。

    创建一个新日期,该日期将是给定月份的最后一天.获取这一天的一年中的星期,因此现在您有了最终value。

    现在结束-开始+ 1应该是您想要的。

    当一周与另一年或类似年份重叠时,您可能必须处理一些特殊情况.但是我认为,一旦正确选择,就可以用简单的逻辑做到这一点。

    这是简单的示例代码.我认为您可以使其发挥作用并传递您想要的任何内容。

           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);
            //Above line will not work for December Month, use following line for this
            int end = isDecember?53: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:

    仅在Januaray月份(例如2010年1月,2000年1月)发生拐角案例,在大多数情况下,这些日子是上一年最后一周的一部分,因此起始值为52,终止值为 5.发生这种情况时,请检查

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

    P.S:将其放入您得到的不同测试输入中.让我知道,一旦发现任何问题我是否可以改进。

    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);
            }
    

    日志:

           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
    

  • 14天前
    2 #

    使用wEEK_OF_MONTH会更好,没有跨年度问题。

    public static void main(String[] args) throws Exception {
      for (String m : new String[] { "2012-11", "2012-12", "2013-01", "2013-02", "2013-03",
                  "2013-04", "2013-05", "2013-06" }) {
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
          Date date = format.parse(m);
          Calendar c = Calendar.getInstance();
          c.setTime(date);
          int start = c.get(Calendar.WEEK_OF_MONTH);
          c.add(Calendar.MONTH, 1);
          c.add(Calendar.DATE, -1);
          int end = c.get(Calendar.WEEK_OF_MONTH);
          System.out.println(" # of weeks in " + format.format(c.getTime())
                  + ": " + (end - start + 1));
      }
    }
    

    结果:

    # of weeks in 2012-11: 5
    # of weeks in 2012-12: 6
    # of weeks in 2013-01: 5
    # of weeks in 2013-02: 5
    # of weeks in 2013-03: 6
    # of weeks in 2013-04: 5
    # of weeks in 2013-05: 5
    # of weeks in 2013-06: 6
    

  • 14天前
    3 #

    我们可以使用日历类,然后执行类似的操作

    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar.getActualMaximum(Calendar.WEEK_OF_MONTH));
    

    这将返回当月的星期数

  • 14天前
    4 #

    假设我们知道该月的第一天(星期六,星期日等)。

    威兹威兹 威兹威兹 它不是a年,则(更正编辑)

      If 1st day of the month is a 一个月只有4周.否则,在所有情况下,它至少应有5周,最多6周。

      现在,如果您的月份从 Sunday开始 ,您将始终有6周的时间(如果不是2月,那将是5周)

      现在,如果您的月份从 February开始 ,您将有6个月的工作时间,共31天,其余5天。

      对于其他情况,将始终为5周。

      找出月份的第一天并不难,您可以使用 Saturday 或其他日期/时间实用程序库类.但是,我想可能会有更简洁的解决方案来消除极端情况和不必要的复杂性。

  • 14天前
    5 #

    如果您想"手工"完成

    使用此:

    Friday
    

    哪里 Calendar 只是一个整数,用于将您要查找的月份与今天的日期进行比较。

    Date.prototype.getMonthWeek = function(monthAdjustement) { var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay(); var returnMessage = (Math.ceil(this.getDate()/7) + Math.floor(((7-firstDay)/7))); return returnMessage; }

  • dom:Javascript小书签转到URL并执行
  • javascript:为什么jQueryval(value)不调度DOM元素的任何事件?