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

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

}

输出结果:

相关推荐
fire-flyer27 分钟前
Spring Boot 源码解析之 Logging
java·spring boot·spring·log4j·logging
papership33 分钟前
【入门级-C++程序设计:12、文件及基本读写-文件的基本概念&文本文件的基本操作】
开发语言·c++·青少年编程
SaleCoder2 小时前
用Python构建机器学习模型预测股票趋势:从数据到部署的实战指南
开发语言·python·机器学习·python股票预测·lstm股票模型·机器学习股票趋势
KoiHeng2 小时前
部分排序算法的Java模拟实现(复习向,非0基础)
java·算法·排序算法
cui_hao_nan5 小时前
JVM——如何对java的垃圾回收机制调优?
java·jvm
熟悉的新风景7 小时前
springboot项目或其他项目使用@Test测试项目接口配置-spring-boot-starter-test
java·spring boot·后端
心平愈三千疾7 小时前
学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
java·分布式·学习
玩代码7 小时前
备忘录设计模式
java·开发语言·设计模式·备忘录设计模式
BUTCHER57 小时前
Docker镜像使用
java·docker·容器
岁忧8 小时前
(nice!!!)(LeetCode 面试经典 150 题 ) 30. 串联所有单词的子串 (哈希表+字符串+滑动窗口)
java·c++·leetcode·面试·go·散列表