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

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

}

输出结果:

相关推荐
方圆想当图灵10 分钟前
缓存之美:万文详解 Caffeine 实现原理(下)
java·redis·缓存
fmdpenny24 分钟前
Vue3初学之商品的增,删,改功能
开发语言·javascript·vue.js
栗豆包25 分钟前
w175基于springboot的图书管理系统的设计与实现
java·spring boot·后端·spring·tomcat
涛ing38 分钟前
21. C语言 `typedef`:类型重命名
linux·c语言·开发语言·c++·vscode·算法·visual studio
等一场春雨1 小时前
Java设计模式 十四 行为型模式 (Behavioral Patterns)
java·开发语言·设计模式
黄金小码农1 小时前
C语言二级 2025/1/20 周一
c语言·开发语言·算法
萧若岚1 小时前
Elixir语言的Web开发
开发语言·后端·golang
wave_sky1 小时前
解决使用code命令时的bash: code: command not found问题
开发语言·bash
水银嘻嘻2 小时前
【Mac】Python相关知识经验
开发语言·python·macos
ac-er88882 小时前
Yii框架中的多语言支持:如何实现国际化
android·开发语言·php