• 自用工具类整理


    自动生成数据

    uuid&雪花id

    private static Long workerId = 1L;
    private static Long datacenterId = 1L;
    private static Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenterId);
    
    public static String getId(String idType) {
        if (idType.equals("uuid")) {
            UUID uuid = UUID.randomUUID();
            return uuid.toString();
        } else {
            return snowflake.nextIdStr();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    数据类型转换

    主要用以对Map对象的数据转换处理

    BigDecimal

    public static BigDecimal parseBigDecimal(Object decimalString) {
        if (decimalString == null || "".equals(decimalString) || "0".equals(decimalString) || "0.00".equals(decimalString)) {
            return new BigDecimal("0");
        } else {
            try {
                return new BigDecimal(decimalString.toString());
            } catch (Exception e) {
                return (BigDecimal) decimalString;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Date

    主要用于LocalDateTime类型字符串转Date格式

    public static String localDateTimeStringToDateString(String localDateTime) {
        // 2023-09-06T15:15:04.8651735+08:00
        // 2023-08-10T00:00:00
        if (StringUtils.isNotEmpty(localDateTime) && localDateTime.contains("T")) {
            String dateString = "";
            if (localDateTime.contains(".")) {
                dateString = localDateTime.split("T")[0] + " " + localDateTime.split("T")[1].split("\\.")[0];
            } else {
                dateString = localDateTime.split("T")[0] + " " + localDateTime.split("T")[1];
            }
            return dateString;
        }
        return localDateTime;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    字符串转集合

    public class StringToListUtil {
        // 将传入的s001需处理为json的字符串字段数据转化为json
        public static List<Map<String, String>> toJson(String s) {
            // 创建一个map数组用来保存转换好的数据
            List<Map<String, String>> mapList = new ArrayList<>();
            if ("[]".equals(s)) {
                return mapList;
            }
            // 去掉字符串开头的“[{”和结尾的“}]”
            s = s.substring(2, s.length() - 3);
            // 通过“},{”分割出各个对象
            String[] ss = s.split("},\\{");
            // 处理各个对象
            for (String s1 : ss) {
                // 通过“,”分割出各个对象的各个元素键值
                String[] split = s1.split(",");
                Map<String, String> map = new HashMap<>();
                // 处理各个元素键值
                for (int i = 0; i < split.length; i++) {
                    // 去掉开头的“"”,方便之后通过定位“"”来分割属性名和属性值
                    split[i] = split[i].substring(1);
                    // 通过“"”和“:”分割出属性名、属性值
                    try {
                        map.put(split[i].substring(0, split[i].indexOf("\"")), split[i].substring(split[i].indexOf(":") + 1));
                    } catch (Exception e) {
    //                    e.printStackTrace();
                        System.out.println(e.getMessage());
                    }
                }
                // 将当前对象保存到对象数组mapList(所有信息)
                mapList.add(map);
            }
            // 返回
            return mapList;
        }
    
        // 将传入的格式为{"sql":null,"layout":[]}的
        // s001需处理为json的字符串字段数据转化为json
        public static List<Map<String, String>> toJsonHaveSql(String s) {
            s = s.substring(21, s.length() - 1);
            // 创建一个map数组用来保存转换好的数据
            List<Map<String, String>> mapList = new ArrayList<>();
            // 去掉字符串开头的“[{”和结尾的“}]”
            s = s.substring(2, s.length() - 3);
            // 通过“},{”分割出各个对象
            String[] ss = s.split("},\\{");
            // 处理各个对象
            for (String s1 : ss) {
                // 通过“,”分割出各个对象的各个元素键值
                String[] split = s1.split(",");
                Map<String, String> map = new HashMap<>();
                // 处理各个元素键值
                for (int i = 0; i < split.length; i++) {
                    // 去掉开头的“"”,方便之后通过定位“"”来分割属性名和属性值
                    split[i] = split[i].substring(1);
                    // 通过“"”和“:”分割出属性名、属性值
                    map.put(split[i].substring(0, split[i].indexOf("\"")), split[i].substring(split[i].indexOf(":") + 1));
                }
                // 将当前对象保存到对象数组mapList(所有信息)
                mapList.add(map);
            }
            // 返回
            return mapList;
        }
    }
    
    • 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

    .setScale(7, RoundingMode.HALF_UP)
    , 7, BigDecimal.ROUND_HALF_UP

  • 相关阅读:
    C# Array和ArrayList有什么区别
    websocket通信
    2023/9/19 -- C++/QT
    使用 Windows 20 年后我如何切换到 Ubuntu(2022 年指南)
    极狐GitLab CI x Vault,做好企业密钥安全合规管理
    集合深度学习02—List
    CSS接触
    JVM 内存调优总结贴
    美国护肤品公司【Elevai Labs】申请750万美元纳斯达克IPO上市
    用Unity发布APP到Hololens2无坑教程
  • 原文地址:https://blog.csdn.net/weixin_48415369/article/details/134241828