DateConverterUtil.java 4.88 KB
Newer Older
谢永涛 committed
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
package com.hanyun.hip.mrqc.jms.uitls;

import cn.hutool.core.collection.CollUtil;
import com.hanyun.hip.mrqc.service.engine.rule.QcUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateConverterUtil {
    private static final List<String> COMMON_DATE_FORMATS = Arrays.asList(
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd HH:mm",
            "yyyy-MM-dd HH",
            "yyyy/MM/dd HH:mm:ss",
            "yyyy.MM.dd HH:mm:ss",
            "yyyy-MM-dd'T'HH:mm:ss",
            "yyyy-MM-dd",
            "yyyy年MM月dd日",
            "yyyy年MM月dd日HH:mm",
            "yyyyMMddHHmmss"

    );

    /**
     * 尝试使用正则表达式匹配并转换给定的日期字符串为"yyyy-MM-dd HH:mm:ss"格式。
     *
     * @param strDate  待转换的日期字符串
     * @return 转换后的"yyyy-MM-dd HH:mm:ss"格式的日期字符串,若无法匹配或转换则返回null
     */
    public static String convertToDate(String strDate) {
        for (String format : COMMON_DATE_FORMATS) {
            try {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
                LocalDateTime parsedDate = LocalDateTime.parse(strDate, formatter);
                return parsedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } catch (DateTimeParseException ignored) {
                // 若当前格式不匹配,则忽略异常并尝试下一个格式
            }
        }

        return null; // 若所有格式都无法匹配,返回null
    }

    /**
     * 尝试使用正则表达式匹配并转换给定的日期字符串为"yyyy-MM-dd HH:mm:ss"格式。
     * 这个方法仅作为示例,实际应用中可能效果不佳,建议使用标准日期时间库进行解析。
     *
     * @param strDate  待转换的日期字符串
     * @return 转换后的"yyyy-MM-dd HH:mm:ss"格式的日期字符串,若无法匹配或转换则返回null
     */
    @Deprecated
    public static String convertToDateRegex(String strDate) {
        // 创建一个包含多种常见日期格式的正则表达式
        String regex = "(\\d{4})[-/.](\\d{2})[-/.](\\d{2})[ T]?\\s?(\\d{2}):(\\d{2}):(\\d{2})?"; // 示例正则,可能需要根据实际需求调整

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(strDate);

        if (matcher.matches()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            int day = Integer.parseInt(matcher.group(3));
            int hour = matcher.group(4) != null ? Integer.parseInt(matcher.group(4)) : 0;
            int minute = matcher.group(5) != null ? Integer.parseInt(matcher.group(5)) : 0;
            int second = matcher.group(6) != null ? Integer.parseInt(matcher.group(6)) : 0;

            // 使用SimpleDateFormat将解析出的日期时间组件组合成"yyyy-MM-dd HH:mm:ss"格式
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(year - 1900, month - 1, day, hour, minute, second); // 注意月份需要减1
            return outputFormat.format(date);
        } else {
            return null; // 若无法匹配,返回null
        }
    }

    /**
     * 判断是否含有24小时入院记录
     * @param recordId
     * @return
     */
    public static boolean isHave24HourRyEmrContent(String recordId){
        if(recordId == null || recordId.isEmpty()){
            return false;
        }
        List<Map<String, Object>> mapList = QcUtil.queryForList("select record_id from mrqc_emr_content where record_id = '" + recordId + "' and title like '%24小时入%'");
        return CollUtil.isNotEmpty(mapList);
    }

    /**
     * 判断是否含有入院记录
     * @param recordId
     * @return
     */
    public static boolean isHaveInEmrContent(String recordId){
        if(recordId == null || recordId.isEmpty()){
            return false;
        }
        List<Map<String, Object>> mapList = QcUtil.queryForList("select record_id from mrqc_emr_content where record_id = '" + recordId + "' and title = '入院记录'");
        return CollUtil.isNotEmpty(mapList);
    }

    /**
     * 判断是否含有出院记录
     * @param recordId
     * @return
     */
    public static boolean isHaveOutEmrContent(String recordId){
        if(recordId == null || recordId.isEmpty()){
            return false;
        }
        List<Map<String, Object>> mapList = QcUtil.queryForList("select record_id from mrqc_emr_content where record_id = '" + recordId + "' and title = '出院记录'");
        return CollUtil.isNotEmpty(mapList);
    }
}