Java获取本周开始时间、本周结束时间、下周开始时间、下周结束时间

Stars-one 2021年01月21日 89次浏览 本篇字数为3,782字

本文为作者原创,转载请注明出处,谢谢配合
作者:Stars-one
链接:https://stars-one.site/2021/01/21/java-get-week-date


本周一日期

public static Date getCurrentWeekStartDate() {
	Calendar instance = Calendar.getInstance();
	//将时间移动到推到本周一
	int currentDay = instance.get(Calendar.DAY_OF_WEEK);
	int preday;
	if (currentDay == 1) {
		preday = -6;
	} else {
		preday = 2 - currentDay;
	}
	instance.add(Calendar.DAY_OF_WEEK, preday);
	instance.set(Calendar.HOUR_OF_DAY, 0);
	instance.set(Calendar.MINUTE, 0);
	instance.set(Calendar.SECOND, 0);
	instance.set(Calendar.MILLISECOND, 0);
	
	return instance.getTime();
}

本周结束时间

public static Date getCurrentWeekEndDate() {
	Calendar instance = Calendar.getInstance();
	instance.setTime(getCurrentWeekStartDate());
	instance.add(Calendar.DAY_OF_WEEK, 6);
	instance.set(Calendar.HOUR_OF_DAY, 23);
	instance.set(Calendar.MINUTE, 59);
	instance.set(Calendar.SECOND, 59);
	instance.set(Calendar.MILLISECOND, 999);

	return instance.getTime();
}

下周开始时间

public static Date getNextWeekStartDate() {
	Calendar instance = Calendar.getInstance();
	//从0开始
	int currentDay = instance.get(Calendar.DAY_OF_WEEK) - 1;
	int preday;
	if (currentDay == 0) {
		preday = 1;
	} else if (currentDay == 1) {
		preday = 7;
	} else {
		preday = 8 - currentDay;
	}
	//将时间移动到推到下周一
	instance.add(Calendar.DAY_OF_WEEK, preday);
	instance.set(Calendar.HOUR_OF_DAY, 0);
	instance.set(Calendar.MINUTE, 0);
	instance.set(Calendar.SECOND, 0);
	instance.set(Calendar.MILLISECOND, 0);
	return instance.getTime();
}

下周结束时间

public static Date getNextWeekEndDate() {
	Calendar instance = Calendar.getInstance();
	Date startDate = getNextWeekStartDate();
	instance.setTime(startDate);

	instance.add(Calendar.DAY_OF_WEEK, 6);
	instance.set(Calendar.HOUR_OF_DAY, 23);
	instance.set(Calendar.MINUTE, 59);
	instance.set(Calendar.SECOND, 59);
	instance.set(Calendar.MILLISECOND, 999);
	Date endDate = instance.getTime();
	return endDate;
}

全部源码

//本周一日期
public static Date getCurrentWeekStartDate() {
	Calendar instance = Calendar.getInstance();
	//将时间移动到推到本周一
	int currentDay = instance.get(Calendar.DAY_OF_WEEK);
	int preday;
	if (currentDay == 1) {
		preday = -6;
	} else {
		preday = 2 - currentDay;
	}
	instance.add(Calendar.DAY_OF_WEEK, preday);
	instance.set(Calendar.HOUR_OF_DAY, 0);
	instance.set(Calendar.MINUTE, 0);
	instance.set(Calendar.SECOND, 0);
	instance.set(Calendar.MILLISECOND, 0);
	
	return instance.getTime();
}
/**
 * 当前周一的结束日期
 * @return
 */
public static Date getCurrentWeekEndDate() {
	Calendar instance = Calendar.getInstance();
	instance.setTime(getCurrentWeekStartDate());
	instance.add(Calendar.DAY_OF_WEEK, 6);
	instance.set(Calendar.HOUR_OF_DAY, 23);
	instance.set(Calendar.MINUTE, 59);
	instance.set(Calendar.SECOND, 59);
	instance.set(Calendar.MILLISECOND, 999);

	return instance.getTime();
}

/**
 * 获取下周一的开始日期
 * @return
 */
public static Date getNextWeekStartDate() {
	Calendar instance = Calendar.getInstance();
	//从0开始
	int currentDay = instance.get(Calendar.DAY_OF_WEEK) - 1;
	int preday;
	if (currentDay == 0) {
		preday = 1;
	} else if (currentDay == 1) {
		preday = 7;
	} else {
		preday = 8 - currentDay;
	}
	//将时间移动到推到下周一
	instance.add(Calendar.DAY_OF_WEEK, preday);
	instance.set(Calendar.HOUR_OF_DAY, 0);
	instance.set(Calendar.MINUTE, 0);
	instance.set(Calendar.SECOND, 0);
	instance.set(Calendar.MILLISECOND, 0);
	return instance.getTime();
}

/**
 * 获取下周的结束时间
 * @return
 */
public static Date getNextWeekEndDate() {
	Calendar instance = Calendar.getInstance();
	Date startDate = getNextWeekStartDate();
	instance.setTime(startDate);

	instance.add(Calendar.DAY_OF_WEEK, 6);
	instance.set(Calendar.HOUR_OF_DAY, 23);
	instance.set(Calendar.MINUTE, 59);
	instance.set(Calendar.SECOND, 59);
	instance.set(Calendar.MILLISECOND, 999);
	Date endDate = instance.getTime();

	return endDate;
}