根据起始时间段,输出期间全部的财周

java 复制代码
import java.time.LocalDate;
import java.time.Month;
import java.util.Set;
import java.util.TreeSet;

public class FiscalWeek {

    private static String getFiscalWeek(LocalDate date) {
        int year = date.getYear();
        Month month = date.getMonth();

        // Determine the fiscal year
        String fiscalYear = String.valueOf(year);
        String fiscalYearShort = fiscalYear.substring(2);

        // Determine the fiscal quarter
        String quarter;
        LocalDate startOfQuarter;
        if (month.getValue() <= 3) {
            quarter = "Q1";
            startOfQuarter = LocalDate.of(year, Month.JANUARY, 1);
        } else if (month.getValue() <= 6) {
            quarter = "Q2";
            startOfQuarter = LocalDate.of(year, Month.APRIL, 1);
        } else if (month.getValue() <= 9) {
            quarter = "Q3";
            startOfQuarter = LocalDate.of(year, Month.JULY, 1);
        } else {
            quarter = "Q4";
            startOfQuarter = LocalDate.of(year, Month.OCTOBER, 1);
        }

        // Calculate the week number within the quarter
        int weekOfQuarter = (int) ((date.toEpochDay() - startOfQuarter.toEpochDay()) / 7) + 1;

        return String.format("FY%s%sW%d", fiscalYearShort, quarter, weekOfQuarter);
    }

    public static Set<String> getFiscalWeeksInRange(LocalDate startDate, LocalDate endDate) {
        Set<String> fiscalWeeks = new TreeSet<>();

        // Traverse through each date in the range
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            fiscalWeeks.add(getFiscalWeek(currentDate));
            currentDate = currentDate.plusWeeks(1);
        }

        return fiscalWeeks;
    }

    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2024, 10, 1);
        LocalDate endDate = LocalDate.of(2024, 10, 30);
        Set<String> fiscalWeeks = getFiscalWeeksInRange(startDate, endDate);

        // Print all unique fiscal weeks
        for (String fiscalWeek : fiscalWeeks) {
            System.out.println(fiscalWeek);
        }
    }

}

输出结果:

相关推荐
Bl_a_ck10 分钟前
开发环境(Development Environment)
开发语言·前端·javascript·typescript·ecmascript
每天一个秃顶小技巧29 分钟前
02.Golang 切片(slice)源码分析(一、定义与基础操作实现)
开发语言·后端·python·golang
purrrew1 小时前
【Java ee初阶】初始网络
java·网络
程序员Bears1 小时前
从零打造个人博客静态页面与TodoList应用:前端开发实战指南
java·javascript·css·html5
Helibo441 小时前
GESPC++六级复习
java·数据结构·算法
serve the people2 小时前
解决osx-arm64平台上conda默认源没有提供 python=3.7 的官方编译版本的问题
开发语言·python·conda
柒七爱吃麻辣烫2 小时前
在Linux中安装JDK并且搭建Java环境
java·linux·开发语言
极小狐2 小时前
极狐GitLab 容器镜像仓库功能介绍
java·前端·数据库·npm·gitlab
极小狐2 小时前
如何构建容器镜像并将其推送到极狐GitLab容器镜像库?
开发语言·数据库·机器学习·gitlab·ruby
努力的搬砖人.3 小时前
如何让rabbitmq保存服务断开重连?保证高可用?
java·分布式·rabbitmq