十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
获取本周一
创新互联公司主要从事成都网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务霍尔果斯,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
public static Date getNowWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况
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为推迟的周数,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);//这是从上周日开始数的到本周五为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);
}