首页 文章

如何获得当月的所有周

提问于
浏览
0
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年1月1日星期五等等......

2017年11月30日星期四<<<

2017年11月30日星期四<<<

2017年11月30日星期四<<<

2017年11月30日星期四<<<

But I want to get all weeks(date 7 days of week) of current month Pattern is like

从当月的周五开始..

2017年11月3日星期五

2017年11月4日星期六

2017年11月5日星期日

2017年11月6日星期一

2017年11月7日至11月

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日星期四

End with next month of first week, if current month last week have less then 7 days.

2 回答

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

    My Output Is

    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===========
    
  • 0

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

相关问题