• 常用封装工具类



    前言

    一、数字相关工具类

    1. 保留指定小数位

    2. 数字转汉字

    import org.apache.commons.lang3.StringUtils;
    import java.math.BigDecimal;
    
    public class NumberUtils {
    
        /**
         * 保留指定小数位数
         *
         * @param value 原始double值
         * @param scale 小数位数
         * @return 保留指定小数位数后的字符串形式
         */
        public static String round(double value, int scale) {
            if (scale < 0) {
                throw new IllegalArgumentException("小数位数不能为负数");
            }
            BigDecimal bd = BigDecimal.valueOf(value);
            bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
          //转字符串后1.00 去除后面的00
            String zeros = ".";
            for (int i = 0; i < scale; i++) {
                zeros += "0";
            }
            if (bd.toString().endsWith(zeros)) {
                return bd.toString().substring(0, StringUtils.indexOf(bd.toString(), zeros));
            }
            return bd.toString();
        }
    
        /**
         * 数字转汉字
         *
         * @param number
         * @return
         */
        public static String convertChinese(int number) {
            //数字对应的汉字
            String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
            //单位
            String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
            //将输入数字转换为字符串
            String result = String.valueOf(number);
            //将该字符串分割为数组存放
            char[] ch = result.toCharArray();
            //结果 字符串
            String str = "";
            int length = ch.length;
            for (int i = 0; i < length; i++) {
                int c = (int) ch[i] - 48;
                if (c != 0) {
                    str += num[c - 1] + unit[length - i - 1];
                }
            }
            return str;
        }
    }
    
    • 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

    二、获取bean

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @Order(98)
    public class SpringUtil implements ApplicationContextAware {
    
        private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
        private static ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if (SpringUtil.applicationContext == null) {
                SpringUtil.applicationContext = applicationContext;
            }
            logger.info("ApplicationContext配置成功,applicationContext对象:" + SpringUtil.applicationContext);
        }
    
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
        public static Object getBean(String name) {
            return getApplicationContext().getBean(name);
        }
    
        public static <T> T getBean(Class<T> clazz) {
            return getApplicationContext().getBean(clazz);
        }
    
        public static <T> T getBean(String name, Class<T> clazz) {
            return getApplicationContext().getBean(name, clazz);
        }
    }
    
    
    • 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

    三、分页相关工具类

    1. 假分页,包含一些自定义类,可根据项目具体封装结构修改

    2. 分页序号

    import com.gsafety.bg.gsdss.common.page.PageResult;
    import org.springframework.data.domain.Pageable;
    
    import java.util.List;
    public class PageStaticUtils<T> {
    
        /**
         * 后端计算分页
         *
         * @param list list
         * @param page page
         * @return PageResult
         */
        public PageResult<T> pageData(List<T> list, Pageable page) {
            int total = list.size();
            int pages = (total / page.getPageSize()) + (total % page.getPageSize() > 0 ? 1 : 0);
            int start = page.getPageSize() * page.getPageNumber();
            int end = Math.min((start + page.getPageSize()), total);
            PageResult<T> pageResult = new PageResult<T>();
            pageResult.setNowPage(page.getPageNumber() + 1);
            pageResult.setPages(pages);
            pageResult.setPageSize(page.getPageSize());
            pageResult.setTotal(total);
            pageResult.setList(list.subList(start, end));
            return pageResult;
        }
        
        /**
         * 获取序号
         *
         * @param list
         * @param data
         * @param pageSize
         * @param nowPage
         * @return
         */
        public Integer getPageNum(List<T> list, T data, Integer pageSize, Integer nowPage) {
            return (list.indexOf(data) + 1) + pageSize * nowPage;
        }
    }
    
    • 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

    四、时间工具类

    1. 星期获取

    2. 时间段耗时计算 [ x时x分 ]

    import java.time.LocalDate;
    
    public class DayOfWeekUtils {
    
        /**
         * 星期几
         * @param day
         * @return
         */
        public static String week(LocalDate day) {
            String[][] strArray = {{"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};
            String k = String.valueOf(day.getDayOfWeek());
            //获取行数
            for (int i = 0; i < strArray.length; i++) {
                if (k.equals(strArray[i][0])) {
                    k = strArray[i][1];
                    break;
                }
            }
            return "星期" + k;
        }
    
    
        /**
         * 计算两个时间段耗时
         *
         * @param start
         * @param end
         * @return
         */
        public static String printSplitTime(LocalDateTime start, LocalDateTime end) {
            // 获得两个时间之间的相差值
            Duration dur = Duration.between(start, end);
            //两个时间差的分钟数
            long minute = dur.toMinutes() - dur.toHours() * 60;
            long hour = dur.toHours();
            return hour + "时" + minute + "分";
        }
    }
    
    • 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

    五、经纬度距离计算

    import lombok.experimental.UtilityClass;
    
    @UtilityClass
    public class DistanceUtil {
    
        /**
         * 地球平均半径
         */
        private static final double EARTH_RADIUS = 6378137;
    
    
        private static double rad(double d) {
            return d * Math.PI / 180.0;
        }
    
        /**
         * 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
         *
         * @param centerLng 初始点经度
         * @param centerLat 初始点纬度
         * @param targetLng 目标点经度
         * @param targetLat 目标点纬度
         */
        public static double getDistance(double centerLng, double centerLat, double targetLng, double targetLat) {
            double radLat1 = rad(centerLat);
            double radLat2 = rad(targetLat);
            double a = radLat1 - radLat2;
            double b = rad(centerLng) - rad(targetLng);
            double s = 2 * Math.asin(
                    Math.sqrt(
                            Math.pow(Math.sin(a / 2), 2)
                                    + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                    )
            );
            s = s * EARTH_RADIUS;
            s = Math.round(s * 10000) / 10000;
            return s;
        }
    }
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    等保2.0对云计算有哪些特定的安全要求?
    vue3插槽的使用
    使用大语言模型 LLM 做文本分析
    NLP冻手之路(4)——pipeline管道函数的使用
    【论文阅读 07】Anomaly region detection and localization in metal surface inspection
    SpotBugs代码检查:在整数上进行没有起任何实际作用的位操作(INT_VACUOUS_BIT_OPERATION)
    ubuntu20编译安装pkg-config
    管控软件开发进度 4大关键项需要重视
    java计算机毕业设计高校多媒体设备报修管理系统MyBatis+系统+LW文档+源码+调试部署
    ChatGPT改写:论文写作新时代
  • 原文地址:https://blog.csdn.net/zhanghuaiyu_35/article/details/134014503