侧边栏壁纸
博主头像
Stars-One的杂货小窝博主等级

所有的创作都是有价值的

  • 累计撰写 276 篇文章
  • 累计创建 46 个标签
  • 累计收到 27 条评论

目 录CONTENT

文章目录

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

Stars-one
2021-01-21 / 0 评论 / 0 点赞 / 1504 阅读 / 5005 字

本周一日期

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;
}
0

评论区