java实现:取指定时间区间的周频、月频、季频最后时间点

该文章提供了三个Java代码示例,分别演示如何获取从指定开始到结束日期之间的每周周末、每月月末和每个季度季末的日期。通过使用LocalDate、DateTimeFormatter和TemporalAdjusters等类,实现了对日期的精确调整和处理。
摘要由CSDN通过智能技术生成

在我们的开发过程中,我们可能会需要对时间进行处理,通常对时间的处理都会比较复杂,这篇文章准备了获取指定时间区间的周频、月频、和季频的java代码,供我们程序开发者参考。

1、java实现:取出开始和结束的时间区间的每周周末时间

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
​
public class Main {
​
    public static void main(String[] args) {

        //初始化两个时间
        String startDateString = "20200201";
        String endDateString = "20220201";

        //将String格式的时间转换为LocalDate对象格式
        LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
​
        List<LocalDate> result = new ArrayList<>();

        //取出当前周中的最后一天作为当前时间
        LocalDate currentWeekEnd = startDate.with(DayOfWeek.SUNDAY);
​

        //while进行循环,当前时间小于结束时间时进入代码逻辑
        while (currentWeekEnd.isBefore(endDate)) {

            result.add(currentWeekEnd);

            //当前周数加一,在取出周末时间,并加入集合
            currentWeekEnd = currentWeekEnd.plusWeeks(1).with(DayOfWeek.SUNDAY);
        }
​
        System.out.println(result);
    }
}
 

2、java实现:取出开始和结束的时间区间的每个月月末时间

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
​
public class Main {
​
    public static void main(String[] args) {
        
        //初始化两个时间
        String startDateString = "20200201";
        String endDateString = "20220201";
        
        //将string格式的时间转换为LocalDate对象格式
        LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
​
        List<LocalDate> result = new ArrayList<>();

        //取出当前月份中的最后一天作为当前时间,并加入到集合中
        LocalDate currentDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
        result.add(currentDate);
​
        //while循环,当前时间小于结束时间进入代码逻辑
        while (currentDate.isBefore(endDate)) {

            //当前时间月份加一,在进行月份的最后一天取值,并加入集合
            currentDate = currentDate.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
            result.add(currentDate);
        }
​
        System.out.println(result);
    }
}
​

3、java实现:取出开始和结束的时间区间的每个季度季末时间

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
​
public class Main {
​
    public static void main(String[] args) {

        //初始化两个时间
        String startDateString = "20200201";
        String endDateString = "20220201";

        //将String格式的时间转换为LocalDate对象格式
        LocalDate startDate = LocalDate.parse(startDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endDate = LocalDate.parse(endDateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
​
        List<LocalDate> result = new ArrayList<>();

        //取出当前时间的月份的最后一天
        LocalDate currentQuarterEnd = startDate.with(TemporalAdjusters.lastDayOfMonth());
​
        //while循环,当前月份小于结束时间时进行循环
        while (currentQuarterEnd.isBefore(endDate)) {

            //if语句进行判断,如果当前月份为3、6、9、12月,那么将获取到的当前月份的最后一天对象加入list集合中
            if (currentQuarterEnd.getMonth() == Month.FEBRUARY || currentQuarterEnd.getMonth() == Month.MAY ||
                    currentQuarterEnd.getMonth() == Month.AUGUST || currentQuarterEnd.getMonth() == Month.NOVEMBER) {
                result.add(currentQuarterEnd);
            }

            //然后当前时间的月份数进行加一操作
            currentQuarterEnd = currentQuarterEnd.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth());

            //if语句进行判断,如果当前月份加一的月份为3、6、9、12月,那么将获取到的当前月份的最后一天对象加入list集合中
            if (currentQuarterEnd.getMonth() == Month.MARCH || currentQuarterEnd.getMonth() == Month.JUNE ||
                    currentQuarterEnd.getMonth() == Month.SEPTEMBER || currentQuarterEnd.getMonth() == Month.DECEMBER) {
                result.add(currentQuarterEnd);
            }
        }
​
        System.out.println(result);
    }
}
​

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值