fix: 修复生肖查询结果不正确的问题

- 使用公历年份直接计算生肖和干支
- 修复之前使用农历日期导致年份边界计算错误的问题
This commit is contained in:
H1d3rOne committed 2026-03-08 01:55:20 +08:00
1 parent ac44ea13b4
commit c819b5f746
1 file changed
+14 -4
@@ -111,9 +111,19 @@ public class ToolController {
try {
int year = Integer.parseInt(params.get("year").toString());
Solar solar = Solar.fromYmd(year, 1, 1);
Lunar lunar = solar.getLunar();
String shengXiao = lunar.getYearShengXiao();
String[] shengXiaoList = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
String[] ganList = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
String[] zhiList = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
int shengXiaoIndex = (year - 4) % 12;
if (shengXiaoIndex < 0) shengXiaoIndex += 12;
String shengXiao = shengXiaoList[shengXiaoIndex];
int ganIndex = (year - 4) % 10;
if (ganIndex < 0) ganIndex += 10;
int zhiIndex = (year - 4) % 12;
if (zhiIndex < 0) zhiIndex += 12;
String ganZhi = ganList[ganIndex] + zhiList[zhiIndex];
Map<String, Object> data = new HashMap<>();
data.put("year", year);
@@ -134,7 +144,7 @@ public class ToolController {
shengXiaoInfo.put("猪", "亥猪,五行属水,善良宽厚,乐观豁达");
data.put("info", shengXiaoInfo.getOrDefault(shengXiao, "未知生肖"));
data.put("ganZhi", lunar.getYearInGanZhi());
data.put("ganZhi", ganZhi);
result.put("success", true);
result.put("data", data);