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);
}
}
}
输出结果: