【问题标题】:how to get all weeks of current month如何获得当月的所有周
【发布时间】:2017-11-30 06:39:46
【问题描述】:
public static void getWeeksOfMonth(int month, int year) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
    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);
    System.out.println(ndays + "<<<ff");
    int inc = 1;
    for (int i = 1; i <= ndays; i++) {
        String day = sdf.format(cal.getTime());
        System.out.println(day + "<<<");
        Log.e("quest", day + "<<<");
        inc++;
        if (i % 7 == 0) {
            Log.e("question", "=======week days===========");
            inc = 0;
        }
        if (inc >= 1 && i == ndays) {

            //cal.set(Calendar.MONTH, month+1);

            Log.e("endval", " " + day);
            for (int ii = inc; ii <= 6; ii++) {
                String dayi = sdf.format(cal.getTime());
                System.out.println(dayi + "<<<");
                Log.e("quest1", dayi + "<<<");
                inc++;
                if (ii % 7 == 0) {
                }
            }

        }
        cal.add(Calendar.DATE, 1);
    }

}

输出

2017 年 11 月 1 日星期三

2017 年 11 月 2 日星期四

2017 年 11 月 3 日星期五

2017 年 11 月 4 日星期六

2017 年 11 月 5 日星期日

2017 年 11 月 6 日星期一

2017 年 11 月 7 日星期二

=======工作日===========

2017 年 11 月 8 日星期三

2017 年 11 月 9 日星期四

2017 年 11 月 10 日星期五

2017 年 11 月 11 日星期六

2017 年 11 月 12 日星期日

2017 年 11 月 13 日星期一

2017 年 11 月 14 日星期二

=======工作日===========

2017 年 11 月 15 日星期三

2017 年 11 月 16 日星期四

2017 年 11 月 17 日星期五

2017 年 11 月 18 日星期六

2017 年 11 月 19 日星期日

2017 年 11 月 20 日星期一

2017 年 11 月 21 日星期二

=======工作日===========

2017 年 11 月 22 日星期三

2017 年 11 月 23 日星期四

2017 年 11 月 24 日星期五

2017 年 11 月 25 日星期六

2017 年 11 月 26 日星期日

2017 年 11 月 27 日星期一

2017 年 11 月 28 日星期二

=======工作日===========

2017 年 11 月 29 日星期三

2017 年 11 月 30 日星期四

2017 年 11 月 30 日星期四

2017 年 11 月 30 日星期四

2017 年 11 月 30 日星期四

2017 年 11 月 30 日星期四

2017 年 11 月 30 日星期四

但我想获取当前月份的所有周(日期为一周中的第 7 天) 模式就像

从当月的星期五开始..

2017 年 11 月 3 日星期五

2017 年 11 月 4 日星期六

2017 年 11 月 5 日星期日

2017 年 11 月 6 日星期一

2017 年 11 月 7 日星期二

2017 年 11 月 8 日星期三

2017 年 11 月 9 日星期四

星期四结束..

=======工作日===========

2017 年 11 月 10 日星期五

2017 年 11 月 11 日星期六

2017 年 11 月 12 日星期日

2017 年 11 月 13 日星期一

2017 年 11 月 14 日星期二

2017 年 11 月 15 日星期三

2017 年 11 月 16 日星期四

=======工作日===========

2017 年 11 月 17 日星期五

2017 年 11 月 18 日星期六

2017 年 11 月 19 日星期日

2017 年 11 月 20 日星期一

2017 年 11 月 21 日星期二

2017 年 11 月 22 日星期三

2017 年 11 月 23 日星期四

=======工作日==========

2017 年 11 月 24 日星期五

2017 年 11 月 25 日星期六

2017 年 11 月 26 日星期日

2017 年 11 月 27 日星期一

2017 年 11 月 28 日星期二

2017 年 11 月 29 日星期三

2017 年 11 月 30 日星期四

=======工作日===========

2017 年 12 月 1 日星期五

2017 年 12 月 2 日星期六

2017 年 12 月 3 日星期日

2017 年 12 月 4 日星期一

2017 年 12 月 5 日,星期二

2017 年 12 月 6 日星期三

2017 年 12 月 7 日星期四

如果上周当月少于 7 天,则以第一周的下个月结束。

【问题讨论】:

  • 你能告诉我们你的代码吗
  • @JohnJoe 是的,我更新了我的问题
  • This 可能会有所帮助。
  • @ADM 阅读我的问题我不想要周数我想要日期与周
  • 我已经读过了。这就是为什么我发送链接以获得至少一些帮助。

标签: android android-layout fullcalendar google-calendar-api android-calendar


【解决方案1】:

您必须使用此代码,我必须修改并添加一些行。它会帮助你

 public static void getWeeksOfMonth(int month, int year) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd-MMM-yyyy");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(DAY_OF_MONTH, 1);
        int ndays = cal.getActualMaximum(DAY_OF_MONTH);
        System.out.println(ndays + "<<<ff");
        while (cal.get(DAY_OF_WEEK) != FRIDAY) {
            cal.add(DAY_OF_MONTH, 1);
            ndays--;
        }
        int remainingDays = ndays%7;
        if(remainingDays==0)
            ndays += 7;
        else
            ndays = ndays + 7 - remainingDays;

        int inc = 1;
        for (int i = 1; i <= ndays; i++) {
            String day = sdf.format(cal.getTime());
            System.out.println(day + "<<<");
            inc++;
            if (i % 7 == 0) {
                Log.e("question", "=======week days===========");
                inc = 0;
            }
            if (inc >= 1 && i == ndays) {
                for (int ii = inc; ii <= 6; ii++) {
                    String dayi = sdf.format(cal.getTime());
                    System.out.println(dayi + "<<<");
                    Log.e("quest1", dayi + "<<<");
                    inc++;
                }
            }
            cal.add(Calendar.DATE, 1);
        }

    }

我的输出是

Friday 03-Feb-2017<<<
Saturday 04-Feb-2017<<<
Sunday 05-Feb-2017<<<
Monday 06-Feb-2017<<<
Tuesday 07-Feb-2017<<<
Wednesday 08-Feb-2017<<<
Thursday 09-Feb-2017<<<
=====week days===========
Friday 10-Feb-2017<<<
Saturday 11-Feb-2017<<<
Sunday 12-Feb-2017<<<
Monday 13-Feb-2017<<<
Tuesday 14-Feb-2017<<<
Wednesday 15-Feb-2017<<<
Thursday 16-Feb-2017<<<
=====week days===========
Friday 17-Feb-2017<<<
Saturday 18-Feb-2017<<<
Sunday 19-Feb-2017<<<
Monday 20-Feb-2017<<<
Tuesday 21-Feb-2017<<<
Wednesday 22-Feb-2017<<<
Thursday 23-Feb-2017<<<
=====week days===========
Friday 24-Feb-2017<<<
Saturday 25-Feb-2017<<<
Sunday 26-Feb-2017<<<
Monday 27-Feb-2017<<<
Tuesday 28-Feb-2017<<<
Wednesday 01-Mar-2017<<<
Thursday 02-Mar-2017<<<
=====week days===========

【讨论】:

  • 干得好,但只剩下一个问题我想从星期五开始一天,到星期四结束
  • 这意味着你被认为是第一个星期五的整个月份都是开始日?
【解决方案2】:

将你的 for 循环修改为这个逻辑:

for(int i=1; i<=ndays;i+=7){

        String day = sdf.format(cal.getTime());
        System.out.println(day + "<<<");
        increment++;

        cal.add(Calendar.DATE, 1);
        if(increment >= 1){
            for(int j = 2;j <= 7;j++){
                String day1 = sdf.format(cal.getTime());
                System.out.println(day1 + "<<<");
                cal.add(Calendar.DATE, 1);
            }
            System.out.println("<<<END");
            increment = 0;
        }
    }

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2022-11-13
    • 2020-01-22
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多