java8 localDate 计算循环每个月份或每个年份的固定日

假设起止日期为 起: 2023-11-08  止: 2025-11-08 

获取月份:

第一种情况: 不包含起始时间, 循环获取日期范围内每个月固定号日期

例子: 获取2023-12-08 2024-01-08 2024-02-08  选择几号可以自行改变;

    /**  
     * 获取指定日期范围内的每个月的指定号日期  
     * @param startDate 开始日期  
     * @param endDate 结束日期  
     * @return
     */  
    public static List<LocalDate> getDatesInRange(LocalDate startDate, LocalDate endDate, int day) {  
        List<LocalDate> dates = new ArrayList<>();  
        LocalDate currentDate = startDate;  
  
        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {  
            // 如果当前日期是固定号,则添加到列表中  
            if (currentDate.getDayOfMonth() == day) {  
                dates.add(currentDate);  
            }  
            currentDate = currentDate.plusDays(1);  
        }  
        return dates;  
    }  

第二种情况: 包含起始时间, 循环获取日期范围内每个月固定号日期

例子: 获取2023-11-08 2023-12-08 2024-01-08

/**
     * 获取指定日期范围内的每个月的固定号日期
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 包含每个月固定号日期的列表
     */
    public static List<LocalDate> getDatesInRange(LocalDate startDate, LocalDate endDate, int day) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate currentDate = startDate;

        // 首先添加起始年份的固定号日期
        dates.add(currentDate);

        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {
            // 如果当前日期是固定号,则添加到列表中
            if (currentDate.getDayOfMonth() == day) {
                dates.add(currentDate);
            }
            currentDate = currentDate.plusDays(1);
        }
        return dates;
    }

获取年份:

获取指定日期范围内每个年份的固定号日期

/** 测试 其中payRecord为实体类, 该业务是计算用户需缴纳维保年费,并设置提前提醒天数*/
    public static void main(String[] args) {
        int num = Integer.parseInt(payRecord.getRemindDate());//提前提醒天数
        LocalDate startDate = DateUtils.toLocalDate(payRecord.getStartDate());
        LocalDate endDate = DateUtils.toLocalDate(payRecord.getEndDate());
        List<LocalDate> range = DateUtils.getYearDatesInRange(startDate, startDate.minusDays(num),
                endDate, endDate.minusDays(num).getDayOfMonth());
    }


/**
     * 获取指定日期范围内每个年份的固定号日期
     * @param startDate 开始日期
     * @param startMinusDate 开始日期计算后提前日期
     * @param endDate 结束日期
     * @return 包含每个年份固定号日期的列表
     */
    public static List<LocalDate> getYearDatesInRange(LocalDate startDate, LocalDate startMinusDate, LocalDate endDate, int day) {
        List<LocalDate> dates = new ArrayList<>();
        LocalDate currentDate = startDate;

        // 首先添加起始年份的固定号日期
        dates.add(startMinusDate);

        while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {

            // 如果当前日期是固定号,则添加到列表中
            if (currentDate.getDayOfMonth() == day) {
                dates.add(currentDate);
            }
            // 移动到下一个年份的固定号日期
            currentDate = currentDate.plusYears(1).withDayOfMonth(day);
        }

        return dates;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 java.time 包中的 LocalDate 类来获取当前年份月份,然后使用 SimpleDateFormat 类来格式化期为指定的格式。 例如: ``` import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class Main { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM", Locale.CHINA); String formattedDate = currentDate.format(formatter); System.out.println(formattedDate); } } ``` 运行上面的代码会输出当前的年份月份,格式为 "年份-月份"。 如果要获取每个月的月份,可以使用一个循环,每次循环时将月份加 1,然后使用相同的方法来格式化期。 例如: ``` import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Locale; public class Main { public static void main(String[] args) { int year = LocalDate.now().getYear(); int month = 1; while (month <= 12) { LocalDate date = LocalDate.of(year, month, 1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM", Locale.CHINA); String formattedDate = date.format(formatter); System.out.println(formattedDate); month++; } } } ``` 运行上面的代码会输出今年的每个月的月份,格式为 "年份-月份"。 ### 回答2: 可以使用Java期时间类库来实现这个功能,我这里以Java 8的java.time包为例。 首先,你需要导入`java.time.LocalDate`类和`java.time.format.DateTimeFormatter`类: ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; ``` 然后,可以使用`LocalDate`类的`now`方法获取当下的期,并通过`DateTimeFormatter`类的`ofPattern`方法定义期的输出格式: ```java LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); ``` 接下来,你可以使用一个循环来输出今年的每个月份,从1月到12月。在循环中,你可以使用`currentDate.format`方法将期格式化为指定的格式: ```java for (int month = 1; month <= 12; month++) { String formattedDate = currentDate.withMonth(month).format(formatter); System.out.println(formattedDate); } ``` 完整的代码如下所示: ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); for (int month = 1; month <= 12; month++) { String formattedDate = currentDate.withMonth(month).format(formatter); System.out.println(formattedDate); } } } ``` 运行该程序,即可输出今年每个月的月份,按照年份-月份格式。 ### 回答3: 可以使用Java中的Date和SimpleDateFormat类来实现。 1. 首先,导入需要的类: import java.util.Date; import java.text.SimpleDateFormat; 2. 创建一个Date对象,并获取当前的年份月份Date date = new Date(); int year = date.getYear() + 1900; int month = date.getMonth() + 1; 3. 使用SimpleDateFormat类将年份月份格式化为"年份-月份"的字符串: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String formattedDate = sdf.format(date); 4. 如果需要输出今年每个月的月份,可以使用循环来遍历月份: for (int i = 1; i <= 12; i++) { // 根据年份月份生成期 String monthString = String.format("%02d", i); // 将月份转换为2位数的字符串 String formattedDate = year + "-" + monthString; // 输出结果 System.out.println(formattedDate); } 以上代码将输出今年每个月的月份,按照"年份-月份"的格式显示。输出结果类似于: 2022-01 2022-02 2022-03 ... 2022-12 注意:在Java中,Date的getYear()方法返回的是从1900年开始到指定年份的年数,getMonth()方法返回的是月份0到11,因此在获取年份月份后,需要进行对应的修正。另外,使用SimpleDateFormat类时,需要指定期的格式,其中"yyyy"表示年份,"MM"表示月份。另外,为了输出结果的对齐,使用String.format()方法将月份转换成2位数的字符串。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值