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

	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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值