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

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

}

输出结果:

相关推荐
一个天蝎座 白勺 程序猿2 分钟前
Python练习(1)Python基础类型操作语法实战:20道实战题解与案例分析(上)
开发语言·python·学习
lightqjx12 分钟前
【数据结构】顺序表(sequential list)
c语言·开发语言·数据结构·算法
巨人张22 分钟前
信息素养Python编程题
开发语言·python
东阳马生架构24 分钟前
订单初版—5.售后退货链路中的技术问题说明文档
java
小小寂寞的城29 分钟前
JAVA策略模式demo【设计模式系列】
java·设计模式·策略模式
阿猿收手吧!1 小时前
【计算机网络】HTTP1.0 HTTP1.1 HTTP2.0 QUIC HTTP3 究极总结
开发语言·计算机网络
JAVA学习通1 小时前
图书管理系统(完结版)
java·开发语言
abigalexy1 小时前
深入Java锁机制
java
paishishaba1 小时前
处理Web请求路径参数
java·开发语言·后端