獲取本周一
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)霍爾果斯,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
public?static?Date?getNowWeekMonday(Date?date)?{????
Calendar?cal?=?Calendar.getInstance();????
cal.setTime(date);????
cal.add(Calendar.DAY_OF_MONTH,?-1);?//解決周日會出現(xiàn)?并到下一周的情況????
cal.set(Calendar.DAY_OF_WEEK,?Calendar.MONDAY);????
303????????????return?cal.getTime();????
}
獲取上周一
public?static?Date?getLastWeekMonday(Date?date)?{????
Date?a?=?DateUtils.addDays(date,?-1);????
Calendar?cal?=?Calendar.getInstance();????
cal.setTime(a);????
cal.add(Calendar.WEEK_OF_YEAR,?-1);//?一周????
cal.set(Calendar.DAY_OF_WEEK,?Calendar.MONDAY);????
return?cal.getTime();????
}
獲取上周日
public?static?Date??getLastWeekSunday(Date?date)?{????
Date?a?=?DateUtils.addDays(date,?-1);????
Calendar?cal?=?Calendar.getInstance();????
cal.setTime(a);????
cal.set(Calendar.DAY_OF_WEEK,?1);????
return?cal.getTime();????
}
代碼里面有用到?lapache.common-ang包 你需要下載下 就可以使用
mport java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test{
public static void main(String args[]){
Calendar cal = Calendar.getInstance();
//n為推遲的周數(shù),1本周,-1向前推遲一周,2下周,依次類推
int n = 2;
String monday;
cal.add(Calendar.DATE, n*7);
//想周幾,這里就傳幾Calendar.MONDAY(TUESDAY...)
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
System.out.println(monday);
}
}
/**
* 獲取上周五時間
*/
public Date lastFirday() {
//作用防止周日得到本周日期
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 7 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 9);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);//這是從上周日開始數(shù)的到本周五為6
}
/**
* 獲取上周一時間
*/
public Date lastMonday() {
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);
}