根據日期查詢這一周的所有日期

2022-09-20 20:27:06 字數 1467 閱讀 8805

static string gewek() catch (parseexception e)

calendar cal = calendar.getinstance();

cal.clear();

cal.settime(date);

// //判斷要計算的日期是否是週日,如果是則減一天計算週六的,否則會出問題,計算到下一周去了

// int dayweek = cal.get(calendar.day_of_week);// 獲得當前日期是乙個星期的第幾天

// if (1 == dayweek)

// // 設定乙個星期的第一天,按中國的習慣乙個星期的第一天是星期一

// cal.setfirstdayofweek(calendar.monday);

// 設定乙個星期的第一天,第一天是星期日

cal.setfirstdayofweek(calendar.sunday);

// 獲得當前日期是乙個星期的第幾天

int day = cal.get(calendar.day_of_week);

system.out.println(day);

// 根據日曆的規則,給當前日期減去星期幾與乙個星期第一天的差值

cal.add(calendar.date, cal.getfirstdayofweek() - day);

//所在周第一天

startdate = sdf.format(cal.gettime());

//所在周第一天+一天

cal.add(calendar.date, 1);

string a1 = sdf.format(cal.gettime());

//所在周第一天+一天+一天

cal.add(calendar.date, 1);

string a2 = sdf.format(cal.gettime());

//所在周第一天+一天+一天+一天....

cal.add(calendar.date, 1);

string a3 = sdf.format(cal.gettime());

cal.add(calendar.date, 1);

string a4 = sdf.format(cal.gettime());

cal.add(calendar.date, 1);

string a5 = sdf.format(cal.gettime());

cal.add(calendar.date, 1);

//所在周最後一天

enddate = sdf.format(cal.gettime());

return startdate + "," + a1 + "," + a2 + "," + a3 + "," + a4 + "," + a5 + "," + enddate;

}

獲取這一周的開始日期和結束日期

獲取這一周的第一天日期 calendar c new gregoriancalendar c.set calendar.year,integer.parseint year c.set calendar.week of year,calendar.get calendar.week of year ...

js根據某天日期計算對應一周日期

記錄乙個js方法,用來計算某個日期對應一周的日期,如輸入20190104,頁面結果如下 var searchtime 20190104 var mon 周一 dealtime 1,searchtime var tue 周二 dealtime 2,searchtime var wed 週三 dealt...

計算每一周的開始日期和結束日期

header content type text html charset utf 8 date date y m d 當前日期 first 1 first 1 表示每週星期一為開始日期 0表示每週日為開始日期 w date w strtotime date 獲取當前周的第幾天 週日是0周一到週六是...