侧边栏壁纸
博主头像
Stars-one博主等级

所有的创作都是具有价值的
Android Kotlin Java开发者,喜欢折腾搞机,开发小工具
赞赏发电,给予支持

  • 累计撰写 144 篇文章
  • 累计创建 42 个标签
  • 累计收到 12 条评论

目 录CONTENT

文章目录

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

Stars-one
2021-01-21 / 0 评论 / 0 点赞 / 1,344 阅读 / 3,782 字

本周一日期

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

评论区