如何获得当月的所有星期
how to get all weeks of current month
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 年 12 月 1 日星期五等等...
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 天,则以第一周的下个月结束。
你必须使用这段代码,我必须修改并添加一些行。它会帮助你
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===========
将您的 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;
}
}
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 年 12 月 1 日星期五等等...
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 天,则以第一周的下个月结束。
你必须使用这段代码,我必须修改并添加一些行。它会帮助你
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===========
将您的 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;
}
}