使用localdate获取本周、上周、本月、上月、本季度、上季度、本年、去年

该代码片段用于根据给定的类型(如上周、本月、上月、本季度等)动态计算并返回相应的开始和结束日期时间字符串。它使用Java8的日期和时间API,如LocalDate和TemporalAdjusters。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	String startTime = null;
	String endTime = null;
	LocalDate localDate = LocalDate.now();
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	//获取当前季度开始月
	Month minMonth = localDate.getMonth().firstMonthOfQuarter();
	//获取当前季度结束月
	Month maxMonth = minMonth.plus(2);
	switch (type){
		case 2:
			//获取上周
			localDate = today.plusDays(-7);
			LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
			LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
			startTime = monday.format(formatter);
			endTime = sunday.format(formatter);
			break;
		case 3:
			//获取本月
			LocalDateTime nowMonthMin = LocalDateTime.of(localDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
			LocalDateTime nowMonthMax = LocalDateTime.of(localDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
			startTime = nowMonthMin.format(formatter);
			endTime = nowMonthMax.format(formatter);
			break;
		case 4:
			//获取上月
			LocalDate lastDate = localDate.plusMonths(-1);
			LocalDateTime lastMonthMin = LocalDateTime.of(lastDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
			LocalDateTime lastMonthMax = LocalDateTime.of(lastDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
			startTime = lastMonthMin.format(formatter);
			endTime = lastMonthMax.format(formatter);
			break;
		case 5:
			//获取本季度
			LocalDate startDate = LocalDate.of(localDate.getYear(),minMonth,1);
			LocalDate endDate = LocalDate.of(localDate.getYear(),maxMonth,maxMonth.length(localDate.isLeapYear()));
			startTime = startDate.format(formatter);
			endTime = endDate.format(formatter);
			break;
		case 6:
			//获取上季度
			minMonth = minMonth.plus(-3);
			maxMonth = maxMonth.plus(-3);
			startDate = LocalDate.of(localDate.getYear(),minMonth,1);
			endDate = LocalDate.of(localDate.getYear(),maxMonth,maxMonth.length(localDate.isLeapYear()));
			startTime = startDate.format(formatter);
			endTime = endDate.format(formatter);
			break;
		case 7:
			//获取本年
			startDate = localDate.with(TemporalAdjusters.firstDayOfYear());
			endDate = localDate.with(TemporalAdjusters.lastDayOfYear());
			startTime = startDate.format(formatter);
			endTime = endDate.format(formatter);
			break;
		case 8:
			//获取去年
			startDate = localDate.with(TemporalAdjusters.firstDayOfYear()).plusYears(-1);
			endDate = localDate.with(TemporalAdjusters.lastDayOfYear()).plusYears(-1);
			startTime = startDate.format(formatter);
			endTime = endDate.format(formatter);
			break;
		default:
			//获取本周
			LocalDate monday = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
			LocalDate sunday = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
			startTime = monday.format(formatter);
			endTime = sunday.format(formatter);

	}
  1. List item
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂青年宋先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值