package com.template.common.utils; import java.time.LocalDate; import java.time.Year; public class ClassYear { public static void main(String[] args) { Integer stage = 9; // 替换为你想要获取入学年份的学段 int currentYear = Year.now().getValue(); // 获取当前年份 LocalDate currentDate = LocalDate.now(); // 获取当前日期 int admissionYear = getAdmissionYear(stage, currentYear, currentDate); if (admissionYear != -1) { System.out.println("入学年份:" + admissionYear); } else { System.out.println("无法获取入学年份"); } } public static int getAdmissionYear(Integer stage, int currentYear, LocalDate currentDate) { int admissionYear = -1; switch (stage) { //初一 case 7: if(currentDate.getMonthValue() < 9){ admissionYear = currentYear - 1; }else{ admissionYear = currentYear; } break; //初二 case 8: if(currentDate.getMonthValue() < 9){ admissionYear = currentYear - 2; }else{ admissionYear = currentYear; } break; //初三 case 9: if (currentDate.getMonthValue() < 9) { admissionYear = currentYear - 3; } else { admissionYear = currentYear; } break; default: break; } return admissionYear; } }