这篇博客介绍了如何使用JDK7的Calendar和JDK8的LocalDate来获取日期的周、月、季度和年的开始及结束时间。通过示例代码展示了两种实现方式,指出JDK8的LocalDate使用更简洁易懂。
摘要由CSDN通过智能技术生成
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
* 根据给定时间获取周、月度、季度、年度开始结束时间
* @author wsp
public class DateCalendarUtil {
* @Description: 获取月份第一天
* @Author: wsp
public static Calendar getMonthStart(Date date) { // 月份开始
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(date);
//set方法将给定日历字段设置为给定值
//下面代码作用是将日历设置为给定日期月份天数的第一天
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
return startCalendar;
* @Description: 获取月份最后一天
* @Author: wsp
public static Calendar getMonthEnd(Date date) { // 月份结束
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(date);
//getActualMaximum方法用来返回给定日历字段属性的最大值
//endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) 代码作用为返回日历该月份的最大值
//set方法,将该日历的给定月份设置为最大天数
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return endCalendar;
* @Description: 获取季度第一天
* 1.根据给定日期计算当前季度的第一个月份
* 2.设置日历的月份为当前季度的第一个月份
* 3.最后设置日历月份天数为第一天即可
* @Author: wsp
public static Calendar getQuarterStart(Date date) {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(date);
//get方法:获取给定日历属性的值,如 endCalendar.get(Calendar.MONTH) 获取日历的月份
//计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第一个月份只需 月份 / 3 * 3
startCalendar.set(Calendar.MONTH, (((int) startCalendar.get(Calendar.MONTH)) / 3) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
return startCalendar;
* @Description: 获取季度最后一天
* @Author: wsp
public static Calendar getQuarterEnd(Date date) { // 季度结束
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(date);
//计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第三个月份只需 月份 / 3 * 3 + 2
endCalendar.set(Calendar.MONTH, (((int) endCalendar.get(Calendar.MONTH)) / 3) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return endCalendar;
* @Description: 获取年份第一天
* @Author: wsp
public static Calendar getYearStart(Date date) { // 年份开始
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(date);
startCalendar.set(Calendar.DAY_OF_YEAR, 1);
return startCalendar;
* @Description: 获取年份最后一天
* @Author: wsp
public static Calendar getYearEnd(Date date) { // 年份结束
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(date);
endCalendar.set(Calendar.DAY_OF_YEAR, endCalendar.getActualMaximum(Calendar.DAY_OF_YEAR));
return endCalendar;
* @Description: 获取当前周第一天(周一)
* @Author: wsp
public static Date getWeekStart(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//获取传入日期属于星期几,根据星期几进行不同处理
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//周日:需要减去6天为周一。(当传入日期为周日时,若我们直接设置日历天数为周一,则日期会变为下一周的周一,而非当前周)
if (dayOfWeek == 1) {
calendar.add(Calendar.DAY_OF_MONTH, -6);
} else {
//周二 至 周六:直接获取周一即可
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar.getTime();
* @Description: 获取当前周最后一天(周日)
* @Author: wsp
public static Date getWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getWeekStart(date));
calendar.add(Calendar.DAY_OF_MONTH, 6);
return calendar.getTime();
public static void main(String[] args) throws ParseException {
String dateStr = "2021-10-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dateStr);
String startMonthDay = sdf.format(getMonthStart(date).getTime());
String endMonthDay = sdf.format(getMonthEnd(date).getTime());
System.out.printf("月份的第一天:%s, 月份的最后一天:%s%n", startMonthDay, endMonthDay);
String startQuarterDay = sdf.format(getQuarterStart(date).getTime());
String endQuarterDay = sdf.format(getQuarterEnd(date).getTime());
System.out.printf("季度第一天:%s, 季度最后一天:%s%n", startQuarterDay, endQuarterDay);
String startYearDay = sdf.format(getYearStart(date).getTime());
String endYearDay = sdf.format(getYearEnd(date).getTime());
System.out.printf("年份第一天:%s, 年份最后一天:%s%n", startYearDay, endYearDay);
String startWeekDay = sdf.format(getWeekStart(date));
String endWeekDay = sdf.format(getWeekEnd(date));
System.out.printf("当前周周一:%s, 当前周周日:%s%n", startWeekDay, endWeekDay);
月份的第一天:2021-10-01, 月份的最后一天:2021-10-31
季度第一天:2021-10-01, 季度最后一天:2021-12-31
年份第一天:2021-01-01, 年份最后一天:2021-12-31
当前周周一:2021-10-11, 当前周周日:2021-10-17
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
public class DateUtil {
* @Description: 获取季度第一天
* @Author: wsp
public static LocalDate getQuarterStart(LocalDate localDate) {
//设置日期天数为第一天
localDate = localDate.withDayOfMonth(1);
long month = localDate.getMonth().getValue();
if (1 <= month && month <= 3) {
localDate = localDate.withMonth(1);
} else if (4 <= month && month <= 6) {
localDate = localDate.withMonth(4);
} else if (7 <= month && month <= 9) {
localDate = localDate.withMonth(7);
} else {
localDate = localDate.withMonth(10);
return localDate;
* @Description: 获取季度最后一天
* @Author: wsp
private static LocalDate getQuarterEnd(LocalDate localDate) {
//设置日期天数为最后一天
localDate = localDate.withDayOfMonth(localDate.lengthOfMonth());
long month = localDate.getMonth().getValue();
if (1 <= month && month <= 3) {
localDate = localDate.withMonth(3);
} else if (4 <= month && month <= 6) {
localDate = localDate.withMonth(6);
} else if (7 <= month && month <= 9) {
localDate = localDate.withMonth(9);
} else {
localDate = localDate.withMonth(12);
return localDate;
public static void main(String[] args) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.of(2021, 10, 17);
LocalDate startWeekDay = localDate.with(ChronoField.DAY_OF_WEEK, 1);
LocalDate endWeekDay = localDate.with(ChronoField.DAY_OF_WEEK, 7);
System.out.printf("当前周周一:%s, 当前周周日:%s%n", format.format(startWeekDay), format.format(endWeekDay));
LocalDate startMonthDay = localDate.with(ChronoField.DAY_OF_MONTH, 1);
//LocalDate endMonthDay = localDate.with(ChronoField.DAY_OF_MONTH, localDate.lengthOfMonth());
LocalDate endMonthDay = localDate.withDayOfMonth(localDate.lengthOfMonth());
System.out.printf("月份的第一天:%s, 月份的最后一天:%s%n", format.format(startMonthDay), format.format(endMonthDay));
LocalDate startQuarterDay = getQuarterStart(localDate);
LocalDate endQuarterDay = getQuarterEnd(localDate);
System.out.printf("季度第一天:%s, 季度最后一天:%s%n", format.format(startQuarterDay), format.format(endQuarterDay));
LocalDate startYearDay = localDate.with(ChronoField.DAY_OF_YEAR, 1);
LocalDate endYearDay = localDate.with(ChronoField.DAY_OF_YEAR, localDate.lengthOfYear());
System.out.printf("年份第一天:%s, 年份最后一天:%s%n", format.format(startYearDay), format.format(endYearDay));
当前周周一:2021-10-11, 当前周周日:2021-10-17
月份的第一天:2021-10-01, 月份的最后一天:2021-10-31
季度第一天:2021-10-01, 季度最后一天:2021-12-31
年份第一天:2021-01-01, 年份最后一天:2021-12-31
可以看到使用jdk8的LocalDate类简单很多,也更容易理解。
public static void main(String[] args) {
LocalDate nowDate = LocalDate.now();
欢迎使用Markdown编辑器
你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
我整理的一些关于【Java】的项目学习资料(附讲解~~)和大家一起分享、学习一下:https://d.51cto.com/f2PFnNJava中如何获取各周的开始日期和结束日期
在Java中,处理时间和日期非常常见。尤其是在需要获取一周的开始和结束日期时,我们需要一些策略来有效地实现这一功能。本文将详细介绍如何使用Jav...
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
* @author by zhoushupeng
* @date 2019/07/05
public ...
//获取本季度的第一天
Console.WriteLine(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01"));
//获取本季度的最后一天
Console.WriteLine(DateTime.Parse(DateTime.Now.AddMonths(3 - ((Date
在Java中,你可以使用`java.time`包下的`MonthDay`类以及`Days`类来获取给定月份的最后一天。这里是一个简单的示例:
```java
import java.time.LocalDate;
import java.time.MonthDay;
public class Main {
public static void main(String[] args) {
int year = 2023; // 输入年份
int month = 12; // 输入月份 (注意:Java中的月份从1开始计数)
// 创建LocalDate对象表示特定的年月
LocalDate date = LocalDate.of(year, month, 1);
// 使用MonthDay类获取指定月份的最后一天,并加1天(因为LocalDate不包含下个月)
MonthDay lastDayOfMonth = MonthDay.from(date.with(TemporalAdjusters.lastDayOfMonth()));
LocalDate lastDay = lastDayOfMonth.atEndOfMonth(); // 转换回LocalDate
System.out.println("给定年月 " + year + "-" + month + " 的最后一天是: " + lastDay);
在这个例子中,我们首先创建了一个`LocalDate`对象代表给定的年月的第一天,然后使用`TemporalAdjusters.lastDayOfMonth()`调整日期到该月的最后一天,最后再转换回`LocalDate`形式。