package com.trafficaudit.common.util; /** * 报表期(期次)工具:期次粒度口径集中在这里,别在各 Service/页面散写。 * *
口径:H204 道路货运能耗是季报,报表期 = 季度末月(03/06/09/12); * 其余(H203-2/H203-1/城市客运/投资等)为月报;年报待开放后按年。
*/ public final class PeriodUtil { private PeriodUtil() { } /** 是否季度末月(03/06/09/12) */ public static boolean isQuarterEndMonth(String period) { if (period == null) return false; String p = period.trim(); if (p.length() < 7) return false; try { int m = Integer.parseInt(p.substring(5, 7)); return m == 3 || m == 6 || m == 9 || m == 12; } catch (Exception e) { return false; } } /** 该月所属季度的末月:2026-10 → 2026-12;2026-03 → 2026-03;非法值原样返回 */ public static String quarterEndOf(String period) { if (period == null) return null; String p = period.trim(); if (p.length() < 7) return period; try { int y = Integer.parseInt(p.substring(0, 4)); int m = Integer.parseInt(p.substring(5, 7)); int end = ((m - 1) / 3 + 1) * 3; return String.format("%04d-%02d", y, end); } catch (Exception e) { return period; } } /** 校验报表期必须是季度末月,否则抛出可直接展示给用户的异常 */ public static void requireQuarterEndMonth(String period, String label) { if (isQuarterEndMonth(period)) return; throw new RuntimeException(label + "是季报,报表期须为季度末月(3/6/9/12 月),当前为「" + (period == null ? "" : period) + "」,应为「" + quarterEndOf(period) + "」"); } }