java 字符串占位符动态替换值工具类

2022-06-15 07:56:32
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 占位符
 * @author: **
 * @create: 2021-12-22 14:56
 **/
public class StringFormatUtil {
    private static final Pattern pattern = Pattern.compile("\\{(.*?)\\}");
    private static Matcher matcher;

    public static String stringFormat(String sourStr, Map<String, Object> param) {
        String targetStr = sourStr;
        if (param == null)
            return targetStr;
        try {
            matcher = pattern.matcher(targetStr);
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(1, key.length() - 1).trim();
                Object value = param.get(keyclone);
                if (value != null)
                    targetStr = targetStr.replace(key, value.toString());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return targetStr;
    }

    public static List<String> getKey(String targetStr) {
        List<String> list  = new ArrayList<>();
        try {
            matcher = pattern.matcher(targetStr);
            while (matcher.find()) {
                String key = matcher.group();
                String keyclone = key.substring(1, key.length() - 1).trim();
                list.add(keyclone);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    public static String stringFormat(String sourStr, Object obj) {
        String targetStr = sourStr;
        matcher = pattern.matcher(targetStr);
        if (obj == null)
            return targetStr;

        PropertyDescriptor pd;
        Method getMethod;
        // 匹配{}中间的内容 包括括号
        while (matcher.find()) {
            String key = matcher.group();
            String keyClone = key.substring(1, key.length() - 1).trim();
            try {
                pd = new PropertyDescriptor(keyClone, obj.getClass());
                getMethod = pd.getReadMethod();// 获得get方法
                Object value = getMethod.invoke(obj);
                if (value != null)
                    targetStr = targetStr.replace(key, value.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return targetStr;
    }


    //根据出生年月计算年龄
    public static int getAge(Date birth) {
        try{
            Calendar cal = Calendar.getInstance();
            int thisYear = cal.get(Calendar.YEAR);
            int thisMonth = cal.get(Calendar.MONTH);
            int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
            cal.setTime(birth);
            int birthYear = cal.get(Calendar.YEAR);
            int birthMonth = cal.get(Calendar.MONTH);
            int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
            int age = thisYear - birthYear;
            // 未足月
            if (thisMonth <= birthMonth) {
                // 当月
                if (thisMonth == birthMonth) {
                    // 未足日
                    if (dayOfMonth < birthdayOfMonth) {
                        age--;
                    }
                } else {
                    age--;
                }
            }
            return age;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

}
  • 作者:辛一一
  • 原文链接:https://blog.csdn.net/xc123_java/article/details/124324368
    更新时间:2022-06-15 07:56:32