zoukankan      html  css  js  c++  java
  • java 常用工具类

    所需maven 依赖

            <!--apache commons-->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>

    字符串填充工具类

        public static void main(String[] args) {
            //字符串填充
            String s = "1";
            System.out.println("原始字符串:" + s);
            //左填充
            String s1 = StringUtils.leftPad(s, 4, "0");
            System.out.println("左填充字符串:" + s1);
            //右填充
            String s2 = StringUtils.rightPad(s, 4, "0");
            System.out.println("右填充字符串:" + s2);
        }
    
    控制台输出:
    
    原始字符串:1
    左填充字符串:0001
    右填充字符串:1000

    字符串拼接工具类

    public class Test002 {
        public static void main(String[] args) {
            //方法一
            String[] array = new String[]{"test", "1234", "5678", ""};
            System.out.println("--------- 方法二 --------");
            StringBuffer sb = new StringBuffer();
            for (String s : array) {
                //如果拼接字符为空则跳过,不进行拼接处理
                if (StringUtils.isEmpty(s)) {
                    break;
                }
                sb.append(s).append(",");
            }
            System.out.println("截取前:" + sb.toString());
            //截取最后拼接的一个","符号
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
            }
            System.out.println("截取后:" + sb.toString());
    
            System.out.println("--------- 方法二 --------");
            //StringUtils.join方法
            String join = StringUtils.join(array, ",");
            System.out.println("StringUtils.join 方法拼接处理前:" + join);
            if (join.length() > 0) {
                join = join.substring(0, join.length() - 1);
            }
            System.out.println("StringUtils.join 方法拼接处理后:" + join);
        }
    }
    
    控制台输出:
    --------- 方法一  --------
    截取前:test,1234,5678,
    截取后:test,1234,5678
    --------- 方法二 --------
    StringUtils.join 方法拼接处理前:test,1234,5678,
    StringUtils.join 方法拼接处理后:test,1234,5678

    日期格式转化工具类

    public class Test003 {
        public static void main(String[] args) throws ParseException {
            //时间工具类 线程不安全
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //时间类型转化为字符串
            String format = simpleDateFormat.format(new Date());
            System.out.println("时间类型转化为字符串格式:" + format);
            Date parse = simpleDateFormat.parse("2020-06-18 10:07:02");
            System.out.println("字符串转化为时间类型格式:" + parse);
    
            System.out.println("-------- 线程安全 --------");
    
            //google 线程安全
            String format1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
            System.out.println("时间类型转化为字符串格式:" + format1);
            Date date = DateUtils.parseDate("2020-06-18 10:10:43", "yyyy-MM-dd HH:mm:ss");
            System.out.println("字符串转化为时间类型格式:" + date);
    
            Date now = new Date();
            System.out.println("now:" + dateToString(now));
            // Date 加 1 天
            Date addDays = DateUtils.addDays(now, 1);
            System.out.println("addDays:" + dateToString(addDays));
            // Date 加 23 分钟
            Date addMinutes = DateUtils.addMinutes(now, 23);
            System.out.println("addMinutes:" + dateToString(addMinutes));
            // Date 减去 123 秒
            Date addSeconds = DateUtils.addSeconds(now, -123);
            System.out.println("addSeconds:" + dateToString(addSeconds));
            // 判断是否 Wie 同一天
            boolean sameDay = DateUtils.isSameDay(addDays, addMinutes);
            System.out.println("sameDay 是否是同一天:" + sameDay);
            // 过滤时分秒,若 now 为 2020-06-18 10:23:46 调用 truncate 方法以后
            // 返回时间为 2020-06-18 00:00:00
            Date truncate = DateUtils.truncate(now, Calendar.DATE);
            System.out.println(dateToString(truncate));
    
            System.out.println("------------ java8 ------------");
    
            // java 8 提供时间工具类
            LocalDateTime localDateTime = LocalDateTime.parse("2020-06-18 10:23:46", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            String format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime);
            System.out.println("java 8 时间类:" + format2);
            LocalDateTime now1 = LocalDateTime.now();
            System.out.println("now:" + localDateTimeToString(now1));
            //加一天
            LocalDateTime plusDays = now1.plusDays(1);
            System.out.println("plusDays:" + localDateTimeToString(plusDays));
            //加一小时
            LocalDateTime plusHours = now1.plusHours(1);
            System.out.println("plusHours:" + localDateTimeToString(plusHours));
            //加30分钟
            LocalDateTime plusMinutes = now1.plusMinutes(30);
            System.out.println("plusMinutes:" + localDateTimeToString(plusMinutes));
            //加30秒
            LocalDateTime plusSeconds = now1.plusSeconds(30);
            System.out.println("plusSeconds:" + localDateTimeToString(plusSeconds));
    
    
        }
    
        /**
         * 时间类型转化
         *
         * @param date
         * @return
         */
        public static String dateToString(Date date) {
            return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
        }
    
        /**
         * java 8 localDateTimeToString 工具类
         *
         * @param localDateTime
         * @return
         */
        public static String localDateTimeToString(LocalDateTime localDateTime) {
            return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime);
        }
    }

     集合框架maven依赖

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.0</version>
            </dependency>

    集合常用工具类

    public class Test004 {
        public static void main(String[] args) {
            List<String> stringList = new ArrayList<>();
            stringList.add("a");
            stringList.add("b");
            stringList.add("c");
    
            List<String> stringList1 = new ArrayList<>();
            stringList1.add("aa");
            stringList1.add("bb");
            stringList1.add("cc");
            stringList1.add("a");
            //判断集合为空
            if (CollectionUtils.isEmpty(stringList)) {
                System.out.println("集合为空");
            }
            System.out.println("stringList" + stringList);
            System.out.println("integerList" + stringList1);
            // 集合合并
            CollectionUtils.addAll(stringList, stringList1);
            System.out.println("stringList" + stringList);
    
            Map<String, String> concurrentMap = Maps.newConcurrentMap();
            //判断map集合为空
            if (MapUtils.isEmpty(concurrentMap)) {
                System.out.println("map 集合为空");
            }
        }
    }

    计时工具类

    public class Test005 {
        public static void main(String[] args) throws InterruptedException {
            // 创建之后立刻计时,若想主动开始计时
            Stopwatch stopwatch = Stopwatch.createStarted();
            // 创建计时器,但是需要主动调用 start 方法开始计时
            // Stopwatch stopwatch = Stopwatch.createUnstarted();
            // stopWatch.start();
            // 模拟其他代码耗时
            TimeUnit.SECONDS.sleep(2L);
    
            // 当前已经消耗的时间
            System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));
    
            TimeUnit.SECONDS.sleep(2L);
    
            // 停止计时 未开始的计时器调用 stop 将会抛错 IllegalStateException
            stopwatch.stop();
            // 再次统计总耗时
            System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));
    
            // 重新开始,将会在原来时间基础计算,若想重新从 0开始计算,需要调用 stopwatch.reset()
            stopwatch.start();
            TimeUnit.SECONDS.sleep(2L);
            System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));
    
        }
    }
  • 相关阅读:
    自定义tabbar
    数据存储: sqlite,coredata plist 归档
    分享(微信,微博,人人)
    OAuth协议与第三方登录:(QQ,百度,微信,微博)
    修改后台来测试APP的方法
    mysql 数据库优化之路
    tcp协议在定位中的应用(2)
    tcp协议在定位中的应用
    计算机术语中一些歧义
    网络常见问题背后的原因
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/13156280.html
Copyright © 2011-2022 走看看