import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
* 周工具类
public class WeekUtils {
* 获取本周的第一天
* @return String
public static String getThisWeekMonday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_MONTH, 0);
cal.set(Calendar.DAY_OF_WEEK, 2);
Date time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
* 获取本周的最后一天
* @return String
public static String getThisWeekSunday() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
cal.add(Calendar.DAY_OF_WEEK, 1);
Date time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
* 获取上周的第一天
* @return
* @throws Exception
public static String getPreviousWeekMonday() {
int week = -1;
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 7 * week);
Date preMonday = currentDate.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(preMonday) + " 00:00:00";
* 获取上周的最后一天
* @return
public static String getPreviousWeekSunday() {
int weeks = -1;
int mondayPlus = getMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + weeks);
Date preSunday = currentDate.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(preSunday) + " 23:59:59";
public static int getMondayPlus() {
Calendar cd = Calendar.getInstance();
int dayOfWeek = cd.get(7) - 1;
if (dayOfWeek == 1) {
return 0;
return (1 - dayOfWeek);
public static void main(String[] args) throws Exception {
System.out.println("获取上周的第一天:" + WeekUtils.getPreviousWeekMonday());
System.out.println("获取上周的最后一天:" + WeekUtils.getPreviousWeekSunday());
System.out.println("获取本周的第一天:" + WeekUtils.getThisWeekMonday());
System.out.println("获取本周的最后一天:" + WeekUtils.getThisWeekSunday());
– 求知若饥,虚心若愚。
请大神指教。1.[代码][Java]代码//获取周第一天public Date getStartDayOfWeek(String date) {LocalDate now = LocalDate.parse(date);return this.getStartDayOfWeek(now);}public Date getStartDayOfWeek(TemporalAccessor date) {...
我整理的一些关于【Java】的项目学习资料(附讲解~~)和大家一起分享、学习一下:https://d.51cto.com/f2PFnNJava中如何获取各周的开始日期和结束日期
在Java中,处理时间和日期非常常见。尤其是在需要获取一周的开始和结束日期时,我们需要一些策略来有效地实现这一功能。本文将详细介绍如何使用Jav...
System.out.println("本月开始时间 = " + firstDayOfMonthStart.format(pattern));System.out.println("本月结束时间 = " + firstDayOfMonthEnd.format(pattern));System.out.println("当天开始时间 = " + todayStart.format(pattern));// 本月最后一天的最后时间。// 本月1号的开始时间。
Date date = new Date();//获得系统时间.
SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
String nowTime = sdf.format(date);
Date time = sdf.parse(nowTime);
2、获取昨天时间(年月日时分秒)
Date dN.
第一种方法public static String getFirstAndLastOfWeek(String dataStr) throws ParseException {
Calendar cal = Calendar.getInstance(); cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dataSt