大写时间日期格式化

背景:朋友项目中需要需要对OCR识别营业执照上面的日期进行格式化,并做一些容错处理。如:“二〇一五年五月十二日” 转换成 “2015-5-12”

直接上代码 ↓↓↓

初始化字典
private static final char TEN = '十';
Map<Character, Integer> digitMap = new HashMap<>();
// 初始化字典
private void setupMap() {
    digitMap.put('一', 1);
    digitMap.put('二', 2);
    digitMap.put('三', 3);
    digitMap.put('四', 4);
    digitMap.put('五', 5);
    digitMap.put('六', 6);
    digitMap.put('七', 7);
    digitMap.put('八', 8);
    digitMap.put('九', 9);
    digitMap.put(TEN, 1);
	// 兼容一些奇怪的识别结果
    digitMap.put('0', 0);
    digitMap.put('O', 0);
    digitMap.put('o', 0);
    digitMap.put('〇', 0);
    digitMap.put('I', 1);
    digitMap.put('l', 1);
    digitMap.put('i', 1);
}
核心方法
// source: 需要转换的年、月、日字符串,如:二O一五
private String convert(String source) {
    StringBuilder sb = new StringBuilder();
    int index = 0;
    int length = source.length();
    for (char c: source.toCharArray()) {
        // 对 “十” 做特殊处理
        if (length == 1 && c == TEN) {
            digitMap.put(TEN, 10);
        } else if (index != 0 && c == TEN) {
            digitMap.put(TEN, 0);
        } else {
            digitMap.put(TEN, 1);
        }
        if (index != 0 && index != length-1 && c == TEN) {
            continue;
        }
        Integer digit = digitMap.get(c);
        if (digit == null) {
            System.err.println(MessageFormat.format("{0} is invalid.", c));
            return null;
        }
        sb.append(digit.toString());
        index++;
    }
    return sb.toString();
}
测试调用
@Test
public void test() {
    setupMap();

    String source = "二〇一五年五月十二日";
    source = "二O一五年十一月三十一日";
    // 判断是否包含"年"、"月"、"日"
    int beginIndex = 0;
    int yearIndex = source.indexOf("年");
    int monthIndex = source.indexOf("月");
    int dayIndex = source.indexOf("日");
    if (-1 == yearIndex || -1 == monthIndex || -1 == dayIndex) {
        System.err.println(MessageFormat.format("{0} is invalid.", source));
        return;
    }
    String year = source.substring(beginIndex, yearIndex);
    String month = source.substring(yearIndex + 1, monthIndex);
    String day = source.substring(monthIndex + 1, dayIndex);

    String result = MessageFormat.format("{0}-{1}-{2}", convert(year), convert(month), convert(day));
    System.out.println(MessageFormat.format("result: {0}", result));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值