zhizhijie
2026-10-20 c89f5fe744773ed06de8a0834009100e941227f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.trafficaudit.common.util;
 
/**
 * 报表期(期次)工具:期次粒度口径集中在这里,别在各 Service/页面散写。
 *
 * <p>口径:H204 道路货运能耗是<b>季报</b>,报表期 = 季度末月(03/06/09/12);
 * 其余(H203-2/H203-1/城市客运/投资等)为月报;年报待开放后按年。</p>
 */
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) + "」");
    }
}