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

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);
        }
    }

}

输出结果:

相关推荐
草莓base7 分钟前
【手写一个spring】spring源码的简单实现--容器启动
java·后端·spring
Allen Bright20 分钟前
maven概述
java·maven
qystca22 分钟前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法
编程重生之路22 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
薯条不要番茄酱23 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
今天吃饺子28 分钟前
2024年SCI一区最新改进优化算法——四参数自适应生长优化器,MATLAB代码免费获取...
开发语言·算法·matlab
努力进修32 分钟前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
politeboy32 分钟前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
Daniel 大东1 小时前
BugJson因为json格式问题OOM怎么办
java·安全
Ajiang28247353042 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++