天天看点

Apache Commons Lang3 常用工具类库pomCommons lang3 常用工具类StringUtils工具类RandomUtils工具类RandomStringUtils 随机字符串ObjectUtils 对象工具类NumberUtils 数值工具类ArrayUtils 通用数组操作工具类DateFormatUtils 时间格式化工具类

Apache Commons Lang3 常用工具类库

  • pom
  • Commons lang3 常用工具类
  • StringUtils工具类
  • RandomUtils工具类
  • RandomStringUtils 随机字符串
  • ObjectUtils 对象工具类
  • NumberUtils 数值工具类
  • ArrayUtils 通用数组操作工具类
  • DateFormatUtils 时间格式化工具类
Java基本对象方法的工具类包 如:StringUtils,ArrayUtils等等.

pom

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

Commons lang3 常用工具类

工具类 描述 示例
org.apache.commons.lang3.ObjectUtils 对象工具类 ObjectUtils.isNotEmpty(new int[]{}) = false
org.apache.commons.lang3.math.NumberUtils 数值工具类 NumberUtils.toInt(null) = 0,NumberUtils.toInt(“1”) = 1
org.apache.commons.lang3.ArrayUtils 数组工具类 ArrayUtils.remove([1, 0], 1) = [1]
org.apache.commons.lang3.BooleanUtils 布尔工具类 BooleanUtils.toInteger(true) = 1,BooleanUtils.toBoolean(1) = Boolean.TRUE
org.apache.commons.lang3.RandomStringUtils 随机字符串工具类 RandomStringUtils.randomAlphabetic(10) = “CtDdCZEldF”,RandomStringUtils.randomGraph(10) = #vdP\[email protected]
org.apache.commons.lang3.RandomUtils 随机数值工具类 RandomUtils.nextBoolean(),RandomUtils.nextInt(100,1000)
org.apache.commons.lang3.SystemUtils 系统工具类 SystemUtils.getUserHome() = “C:\Users\Think”
org.apache.commons.lang3.time.DateFormatUtils 日期格式化工具类,将日期转为指定格式的字符串 DateFormatUtils.format(new Date(),“yyyy-MM-dd HH:mm:ss”) = “2019-11-11 11:11:11”
org.apache.commons.lang3.time.DateUtils 日期工具类,将指定格式的字符串转为日期 DateUtils.parseDate(“1993-09-08 14:30:08”,“yyyy-MM-dd HH:mm:ss”)

Apache Commons Lang 3.9 API 文档:http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html

StringUtils工具类

public static void main(String[] args) {
        //判断目标字符串为空或者为null,空格也当作为空
        StringUtils.isBlank(" ");//true;
        StringUtils.isBlank("zysheep") ;//false;
        StringUtils.isBlank("  zysheep  ");//false;

        //判断目标字符串为空或者为null,空格不当作为空
        StringUtils.isEmpty(null); // true
        StringUtils.isEmpty(""); // true
        StringUtils.isEmpty(" ");// false

        //判断字符串是否是数字,不忽略空格
        StringUtils.isNumeric("ab2c");// false
        StringUtils.isNumeric("12-3");// false
        StringUtils.isNumeric("123");// true

        //判断字符串是否是希腊字母,不忽略空格
        StringUtils.isAlpha("abc");// true
        StringUtils.isAlpha("ab2c");// false

        //判断字符串是否全是小写字母
        StringUtils.isAllLowerCase("abc"); // true
        StringUtils.isAllLowerCase("abC"); // false

        //判断源字符串 seq 是否包含字符
        StringUtils.contains(null,1); // false
        StringUtils.contains("abc", 'a'); // true

        //.........

    }
           

RandomUtils工具类

public static void main(String[] args) {
    //生成 [0, Integer.MAX_VALUE) 之间的随机 int 值
    RandomUtils.nextInt();

    //生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值
    int anInt = RandomUtils.nextInt(1, 10);

    //随机生成一个布尔值,true 或者 false
    RandomUtils.nextBoolean();

    //生成 [0, Long.MAX_VALUE) 之间的 long 值
    RandomUtils.nextFloat();

    System.out.println(anInt);
}
           
Apache Commons Lang3 常用工具类库pomCommons lang3 常用工具类StringUtils工具类RandomUtils工具类RandomStringUtils 随机字符串ObjectUtils 对象工具类NumberUtils 数值工具类ArrayUtils 通用数组操作工具类DateFormatUtils 时间格式化工具类

RandomStringUtils 随机字符串

public static void main(String[] args) {

        /**
         * random(final int count): 创建长度为指定个数(count)的随机字符串,将从所有字符集中选择字符,不含字母和数字,如 "篊𣶇ࢦ𣿎椗彩𩬉𦿣𦃹뢂垅"
         * random(final int count, final boolean letters, final boolean numbers):生成指定个数(count)的随机字符串,字符将从参数指示的字母或数字字符集中选择.
         *      1、letters: 字母字符集、numbers:数字字符集
         *      2、letters 为 true、numbers 为 true:则随机字符串由字母和数字组成
         *      3、letters 为 true、numbers 为 false:则随机字符串由字母组成
         *      4、letters 为 false、numbers 为 true:则随机字符串由数字组成,如 7253440222803746
         *      5、letters 为 false、numbers 为 false:则等同于 random(final int count)
         * random(final int count, final char... chars):创建长度为指定个数的随机字符串,从指定的字符集(chars)中选择字符. chars 的个数可以小于 count
         * String random(final int count, final String chars): 从指定的字符集中选择字符生成指定个数的随机字符串
         */
        char[] chars = new char[]{'1', '2', '3', '4', '5', 'a', 'b', 'c', 'd', 'e', '中', '国', '秦', '始', '皇'};
        String random = RandomStringUtils.random(16);
        String random1 = RandomStringUtils.random(16, false, true);
        String random2 = RandomStringUtils.random(16, chars);
        String random3 = RandomStringUtils.random(16, "元济不得命乃悉兵四出焚舞阳及叶掠襄城阳翟");

        //如:random=𪧺𩯢譙𫘾𨣴䝏𡧮𠸴㹵膉
        System.out.println("random=" + random);
        //如:random1=6341306804529422
        System.out.println("random1=" + random1);
        //如:random2=553国dc4bd秦国中国秦中e
        System.out.println("random2=" + random2);
        //random3=不不襄阳元四济出阳元不出城不阳济
        System.out.println("random3=" + random3);


        /**
         * randomAlphabetic(final int count): 创建指定长度的随机字符串,字符将从 (a-z, A-Z) 中选择,等同于 random(count, true, false)
         * randomAlphabetic(final int minLengthInclusive, final int maxLengthExclusive):创建长度介于 [minLengthInclusive,maxLengthExclusive) 之间的随机字符串
         * randomAlphanumeric(final int count):创建长度为指定字符数的随机字符串,从拉丁字母字符集(a-z, A-Z)和数字0-9中选择,等同于 random(count, true, true)
         * randomAlphanumeric(final int minLengthInclusive, final int maxLengthExclusive):创建长度介于 [minLengthInclusive,maxLengthExclusive) 之间的随机字符串
         * randomAscii(final int count):随机字符将从 ASCII 码值介于 [32,126] 之间的字符集中选择,等价于:random(count, 32, 127, false, false)
         * randomAscii(final int minLengthInclusive, final int maxLengthExclusive):创建的随机字符串个数介于 [minLengthInclusive,maxLengthExclusive)
         * randomGraph(final int count): 随机字符从所有可见的 ASCII 字符中选择,即除空格和控制字符外的任何内容,等价于:random(count, 33, 126, false, false)
         * randomGraph(final int minLengthInclusive, final int maxLengthExclusive)
         * randomNumeric(final int count): 创建长度为指定字符数的随机字符串,随机字符将从数字字符集中选择。
         * randomNumeric(final int minLengthInclusive, final int maxLengthExclusive)
         * randomPrint(final int count): 随机字符从所有可见的 ASCII 码字符和空格(即除控制字符外的任何内容)中选择。等价于 random(count, 32, 126, false, false)
         */
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(16);
        String randomAlphabetic1 = RandomStringUtils.randomAlphabetic(16, 18);
        String randomAlphanumeric = RandomStringUtils.randomAlphanumeric(16);
        String randomAscii = RandomStringUtils.randomAscii(16);
        String randomGraph = RandomStringUtils.randomGraph(16);
        String randomNumeric = RandomStringUtils.randomNumeric(16);
        String randomPrint = RandomStringUtils.randomPrint(16);

        //randomAlphabetic=kyjTzlDGibsitaUV
        System.out.println("randomAlphabetic=" + randomAlphabetic);
        //randomAlphabetic1=yLfoKuRDnPrToYuoW
        System.out.println("randomAlphabetic1=" + randomAlphabetic1);
        //randomAlphanumeric=lpIXjQ4j5GP5BsHR
        System.out.println("randomAlphanumeric=" + randomAlphanumeric);
        //randomAscii=}~g2\pT/eIkXoa?-
        System.out.println("randomAscii=" + randomAscii);
        //randomGraph=oFsGkQm#oTS7]${2
        System.out.println("randomGraph=" + randomGraph);
        //randomNumeric=7663970486824099
        System.out.println("randomNumeric=" + randomNumeric);
        //randomPrint=dm^}p7ii>n"k%4-+
        System.out.println("randomPrint=" + randomPrint);
    }
           

ObjectUtils 对象工具类

/**
     * allNotNull(final Object... values) 检查给定数组中的任何元素值是否都不是 null。
     * anyNotNull(final Object... values) 检查给定数组中的元素是否有不是 null 的值。
     * <p>
     * ObjectUtils.allNotNull(*)             = true
     * ObjectUtils.allNotNull(*, *)          = true
     * ObjectUtils.allNotNull(null)          = false
     * ObjectUtils.allNotNull(null, null)    = false
     * ObjectUtils.allNotNull(null, *)       = false
     * ObjectUtils.allNotNull(*, null)       = false
     * ObjectUtils.allNotNull(*, *, null, *) = false
     * <p>
     * ObjectUtils.anyNotNull(*)                = true
     * ObjectUtils.anyNotNull(*, null)          = true
     * ObjectUtils.anyNotNull(null, *)          = true
     * ObjectUtils.anyNotNull(null, null, *, *) = true
     * ObjectUtils.anyNotNull(null)             = false
     * ObjectUtils.anyNotNull(null, null)       = false
     */
    @Test
    public void testAllNotNull() {
        String a = "";
        Float f = null;
        boolean b1 = ObjectUtils.allNotNull(a);
        boolean b2 = ObjectUtils.allNotNull(a, f);
        boolean b3 = ObjectUtils.anyNotNull(a, f);
        //true,false,true
        System.out.println(b1 + "," + b2 + "," + b3);
    }

    /**
     * clone(final T obj) 克隆对象。如果 obj 为  null,则返回 null.
     * cloneIfPossible(final T obj): 先调 clone(final T obj) ,如果返回 null,则返回原来的 obj 对象.
     */
    @Test
    public void testClone() {
        Map<String, Object> dataMap = new HashMap<>(8);
        dataMap.put("code", 200);
        dataMap.put("msg", "success");
        Map<String, Object> clone = ObjectUtils.clone(dataMap);
        Object o = ObjectUtils.cloneIfPossible(null);
        clone.put("code", 1000);
        //{msg=success, code=200}
        System.out.println(dataMap);
        //{msg=success, code=1000}
        System.out.println(clone);
    }

    /**
     * compare(final T c1, final T c2)
     * compare(final T c1, final T c2, final boolean nullGreater)
     * 1、如果 c1 < c2,则返回负数;如果 c1 > c2,则返回正数;如果 c1 = c2,则返回 0;
     * 2、c1、c2 可以为 null。nullGreater 为  false 时,先判断 c1 如果 null ,则返回 -1,再判断 c2 如果为 null,则返回 1.
     * 3、compare(final T c1, final T c2) 底层是 compare(c1, c2, false)
     */
    @Test
    public void testCompare() {
        System.out.println(ObjectUtils.compare(2, 3));
        System.out.println(ObjectUtils.compare(2, -3));
        System.out.println(ObjectUtils.compare(2, null));
        System.out.println(ObjectUtils.compare(2, null, true));
        System.out.println(ObjectUtils.compare(null, 3));
        System.out.println(ObjectUtils.compare(null, 3, true));
        System.out.println(ObjectUtils.compare(3, 3));
        System.out.println(ObjectUtils.compare(9.89f, 3.45f));
        System.out.println(ObjectUtils.compare("秦", "汉"));
        System.out.println(ObjectUtils.compare("元", "汉"));
    }

    /**
     * 如果传递的对象是 null,则返回默认值
     * ObjectUtils.defaultIfNull(null, null)      = null
     * ObjectUtils.defaultIfNull(null, "")        = ""
     * ObjectUtils.defaultIfNull(null, "zz")      = "zz"
     * ObjectUtils.defaultIfNull("abc", *)        = "abc"
     * ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
     * <p>
     * 返回数组中不是 null 的第一个值
     * ObjectUtils.firstNonNull(null, null)      = null
     * ObjectUtils.firstNonNull(null, "")        = ""
     * ObjectUtils.firstNonNull(null, null, "")  = ""
     * ObjectUtils.firstNonNull(null, "zz")      = "zz"
     * ObjectUtils.firstNonNull("abc", *)        = "abc"
     * ObjectUtils.firstNonNull(null, "xyz", *)  = "xyz"
     * ObjectUtils.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE
     * ObjectUtils.firstNonNull()                = null
     */
    @Test
    public void testDefaultIfNull() {
        String defaultIfNull1 = ObjectUtils.defaultIfNull(null, "[]");
        String defaultIfNull2 = ObjectUtils.defaultIfNull("ha", "");
        System.out.println(defaultIfNull1 + "," + defaultIfNull2);//[],ha

        System.out.println(ObjectUtils.firstNonNull(null, null, "*"));//*
        System.out.println(ObjectUtils.firstNonNull(null, "xyz", "*"));//xyz
    }

    /**
     * 检查对象是否为空,支持:CharSequence、Array、Collection、Map
     * <p>
     * ObjectUtils.isEmpty(null)             = true
     * ObjectUtils.isEmpty("")               = true
     * ObjectUtils.isEmpty(" ")              = false
     * ObjectUtils.isEmpty("ab")             = false
     * ObjectUtils.isEmpty(new int[]{})      = true
     * ObjectUtils.isEmpty(new int[]{1,2,3}) = false
     * ObjectUtils.isEmpty(1234)             = false
     * <p>
     * ObjectUtils.isNotEmpty(null)             = false
     * ObjectUtils.isNotEmpty("")               = false
     * ObjectUtils.isNotEmpty("ab")             = true
     * ObjectUtils.isNotEmpty(new int[]{})      = false
     * ObjectUtils.isNotEmpty(new int[]{1,2,3}) = true
     * ObjectUtils.isNotEmpty(1234)             = true
     */

    @Test
    public void testIsEmpty() {
        System.out.println(ObjectUtils.isEmpty(null));//true
        System.out.println(ObjectUtils.isEmpty(""));//true
        System.out.println(ObjectUtils.isEmpty(" "));//false
        System.out.println(ObjectUtils.isEmpty(new int[]{}));//true
    }

    /**
     * max(final T... values):获取其中最大的值
     * min(final T... values): 获取其中最小的值
     * median(final T... items):获取其中的中位数
     */
    @Test
    public void testMax() {
        Integer[] ints = new Integer[]{6, 3, 5, 99, 45, 34, 55, 79, 44, 88, 31};
        Integer max = ObjectUtils.max(ints);
        Integer min = ObjectUtils.min(ints);
        Integer median = ObjectUtils.median(ints);
        System.out.println(max + "," + min + "," + median);
    }

           

NumberUtils 数值工具类

/**
     * NumberUtils 提供了数字类型的常量,-1、0、1,有 Long、Integer、Short、Byte、Double、Float 类型
     */
    @Test
    public void test1() {
        Integer integerZero = NumberUtils.INTEGER_ZERO;
        Float floatOne = NumberUtils.FLOAT_ONE;
        Double doubleMinusOne = NumberUtils.DOUBLE_MINUS_ONE;
        //0、1.0、-1.0
        System.out.printf("%s、%s、%s", integerZero, floatOne, doubleMinusOne);
    }

    /**
     * int compare(final byte x, final byte y):比较 x、y 的大小,源码 return x - y;
     * int compare(final int x, final int y): 比较 x、y 的大小,源码:x == y ? 0 : x < y ? -1 : 1
     * int compare(final long x, final long y): 比较 x、y 的大小,源码:x == y ? 0 : x < y ? -1 : 1
     * int compare(final short x, final short y) : 比较 x、y 的大小,源码:x == y ? 0 : x < y ? -1 : 1
     */
    @Test
    public void compare() {
        //-2、-1、-1、-1
        System.out.printf("%d、", NumberUtils.compare(Byte.parseByte("16"), Byte.parseByte("18")));
        System.out.printf("%d、", NumberUtils.compare(Integer.parseInt("16"), Integer.parseInt("18")));
        System.out.printf("%d、", NumberUtils.compare(Long.parseLong("16"), Long.parseLong("18")));
        System.out.printf("%d%n", NumberUtils.compare(Short.parseShort("16"), Short.parseShort("18")));
    }

    /**
     * BigInteger createBigInteger(final String str)
     * 1、将字符串转成 BigInteger,支持 10进制,十六进制(以0X或者#开头)、8进制(以0开头)
     * 2、如果 str 为 null,则返回 null。如果转换错误,则抛出 NumberFormatException
     */
    @Test
    public void test3() {
        BigInteger bigInteger1 = NumberUtils.createBigInteger("200");
        BigInteger bigInteger2 = NumberUtils.createBigInteger("0XC8");
        BigInteger bigInteger3 = NumberUtils.createBigInteger("0310");
        BigInteger bigInteger4 = NumberUtils.createBigInteger(null);
        //200、200、200、null
        System.out.printf("%s、%s、%s、%s", bigInteger1, bigInteger2, bigInteger3, bigInteger4);
    }

    /**
     * BigDecimal createBigDecimal(final String str)
     * 1、将字符串转成 BigDecimal,只支持 10 进制,底层就是使用 new BigDecimal(str)
     * 2、str 为null时,返回 null,str 为空时会抛出 NumberFormatException 异常
     */
    @Test
    public void test4() {
        BigDecimal bigDecimal1 = NumberUtils.createBigDecimal("200");
        BigDecimal bigDecimal2 = NumberUtils.createBigDecimal("300.677");
        BigDecimal bigDecimal3 = NumberUtils.createBigDecimal("1988876565677.99889");
        BigDecimal bigDecimal4 = NumberUtils.createBigDecimal(null);
        //200、300.677、1988876565677.99889、null
        System.out.printf("%s、%s、%s、%s", bigDecimal1, bigDecimal2, bigDecimal3, bigDecimal4);
    }

    /**
     * Double createDouble(final String str):字符串转 Double 类型,源码: str == null ? null : Double.valueOf(str);
     * Float createFloat(final String str):字符串转 Float 类型,源码: str == null ? null : Float.valueOf(str);
     * Integer createInteger(final String str):
     * 1、字符串转 Integer 类型,支持十六进制(0xhhhh)、8进制(0dddd),源码: str == null ? null : Integer.decode(str);
     * Long createLong(final String str):
     * 1、字符串转 Long 类型,支持十六进制(0xhhhh)、8进制(0dddd),源码: str == null ? null : Long.decode(str);
     */
    @Test
    public void test5() {
        //889888888.898767、8.766889E8、80000、8000、8766888819
        System.out.printf("%f、", NumberUtils.createDouble("889888888.8987667"));
        System.out.printf("%s、", NumberUtils.createFloat("876688881.898766181"));
        System.out.printf("%d、", NumberUtils.createInteger("80000"));
        System.out.printf("%d、", NumberUtils.createInteger("0X1F40"));
        System.out.printf("%d", NumberUtils.createLong("8766888819"));
    }

    /**
     * boolean isCreatable(final String str)
     * 1、判断字符串是否为有效的 java 数字,支持16进制、8进制、10进制、正负数、小数、科学计数法(如8.788006e+05)、类型限定符(110L、3.14f)
     * 2、0X 开头当做 16 进制处理,如 0X89F9;以0开头的非十六进制字符串作为八进制值处理,如 076、-076等
     * 3、注意例如 098 不是八进制,因为8进制是0-7,没有8、9,所以会当做10进制处理,而此时不是数字,所以为false.
     * 4、str 为空或者为 null,都返回 false
     */
    @Test
    public void testICreatable() {
        //100=true、-110L=true、100.98=true、-110.88F=true、0X89F9=true
        System.out.printf("100=%b、", NumberUtils.isCreatable("100"));
        System.out.printf("110L=%b、", NumberUtils.isCreatable("110L"));
        System.out.printf("100.98=%b、", NumberUtils.isCreatable("100.98"));
        System.out.printf("-110.88F=%b、", NumberUtils.isCreatable("-110.88F"));
        System.out.printf("0X89F9=%b%n", NumberUtils.isCreatable("0X89F9"));

        //-076=true、098=false、8.788006e+05=true、1001.=true、1001.=false
        System.out.printf("-076=%b、", NumberUtils.isCreatable("-076"));
        System.out.printf("098=%b、", NumberUtils.isCreatable("098"));
        System.out.printf("8.788006e+05=%b、", NumberUtils.isCreatable("8.788006e+05"));
        System.out.printf("1001.=%b、", NumberUtils.isCreatable("1001."));
        System.out.printf("1001.=%b", NumberUtils.isCreatable("1001.x"));
    }

    /**
     * boolean isDigits(final String str)
     * 1、检查字符串中是否只含有数字,负数和小数都会返回 false
     * 2、str 为 null 或者为空 返回 false
     * 3、底层调用 {@link StringUtils#isNumeric(java.lang.CharSequence)}
     */
    @Test
    public void testIsDigits() {
        //w100=false、100=true、-100=false、100.0=false、0=true、null=false、0088787878544534300=true
        System.out.printf("w100=%b、", NumberUtils.isDigits("w100"));
        System.out.printf("100=%b、", NumberUtils.isDigits("100"));
        System.out.printf("-100=%b、", NumberUtils.isDigits("-100"));
        System.out.printf("100.0=%b、", NumberUtils.isDigits("100.0"));
        System.out.printf("0=%b、", NumberUtils.isDigits("0"));
        System.out.printf("null=%b", NumberUtils.isDigits(null));
        System.out.printf("、0088787878544534300=%b", NumberUtils.isDigits("0088787878544534300"));
    }

    /**
     * boolean isParsable(final String str)
     * 1、检查字符串是否可以解析为数字,即 {@link Integer#parseInt(String)},{@link Long#parseLong(String)}, {@link Float#parseFloat(String),{@link Double#parseDouble(String)}.
     * 2、这个方法可以防止调用上面的方法时出现  {@link java.text.ParseException}
     * 3、注意只支持 10 进制,支持正负数,支持小数,不支持 8进制、16进制、不支持科学计数法,也不支持类型限定符(如 3000L,3.14F)
     */
    @Test
    @SuppressWarnings("all")
    public void test8() {
        System.out.printf("100=%b、", NumberUtils.isParsable("100"));//100=true
        System.out.printf("110L=%b、", NumberUtils.isParsable("110L"));//110L=false
        System.out.printf("100.98=%b、", NumberUtils.isParsable("100.98"));//100.98=true
        System.out.printf("-110.88F=%b、", NumberUtils.isParsable("-110.88F"));//-110.88F=false
        System.out.printf("110.88F=%b、", NumberUtils.isParsable("110.88F"));//110.88F=false
        System.out.printf("0X89F9=%b%n", NumberUtils.isParsable("0X89F9"));//0X89F9=false

        System.out.printf("-076=%b、", NumberUtils.isParsable("-076"));//-076=true
        System.out.printf("098=%b、", NumberUtils.isParsable("098"));//098=true
        System.out.printf("8.788006e+05=%b、", NumberUtils.isParsable("8.788006e+05"));//8.788006e+05=false
        System.out.printf("1001.=%b、", NumberUtils.isParsable("1001."));//1001.=false
        System.out.printf("1001.x=%b", NumberUtils.isParsable("1001.x"));//1001.x=false
    }

    /**
     * 求最大值
     * byte max(byte a, final byte b, final byte c)
     * byte max(final byte... array)
     * double max(final double a, final double b, final double c)
     * double max(final double... array)
     * float max(final float a, final float b, final float c)
     * float max(final float... array)
     * int max(int a, final int b, final int c)
     * int max(final int... array)
     * long max(long a, final long b, final long c)
     * long max(final long... array)
     * short max(short a, final short b, final short c)
     * short max(final short... array)
     * <p>
     * 求最小值
     * byte min(byte a, final byte b, final byte c)
     * byte min(final byte... array)
     * double min(final double a, final double b, final double c)
     * double min(final double... array)
     * float min(final float a, final float b, final float c)
     * float min(final float... array)
     * int min(int a, final int b, final int c)
     * int min(final int... array)
     * long min(long a, final long b, final long c)
     * long min(final long... array)
     * short min(short a, final short b, final short c)
     * short min(final short... array)
     */
    @Test
    public void test9() {
        byte[] bytes = new byte[]{10, 22, 24, 32, 22, 52, 39, 32};
        float[] floats = new float[]{10.4f, 202.3f, 33.4f, 20.0f, 33f};
        long[] longs = new long[]{100L, 200L, 300L, 20L, 250L, 290L};

        //[10, 22, 24, 32, 22, 52, 39, 32],max=52、min=10
        System.out.printf("%s,max=%s、min=%s%n", Arrays.toString(bytes), NumberUtils.max(bytes), NumberUtils.min(bytes));
        //[10.4, 202.3, 33.4, 20.0, 33.0],max=202.3、min=10.4
        System.out.printf("%s,max=%s、min=%s%n", Arrays.toString(floats), NumberUtils.max(floats), NumberUtils.min(floats));
        //[100, 200, 300, 20, 250, 290],max=300、min=20
        System.out.printf("%s,max=%s、min=%s", Arrays.toString(longs), NumberUtils.max(longs), NumberUtils.min(longs));
    }

    /**
     * byte toByte(final String str):字符串转 byte,如果转换失败,异常捕获后不会抛出,直接返回默认值0;如果 str 为 null,则返回默认值 0
     * byte toByte(final String str, final byte defaultValue):字符串转 byte,同时设置默认值时,其余和上面同理
     * <p>
     * 其它数值类型也是同理(转换失败不会抛出异常,而是返回默认值)
     * double toDouble(final BigDecimal value):
     * double toDouble(final BigDecimal value, final double defaultValue)
     * double toDouble(final String str)
     * double toDouble(final String str, final double defaultValue)
     * float toFloat(final String str)
     * float toFloat(final String str, final float defaultValue)
     * int toInt(final String str):
     * long toLong(final String str):
     * toInt(final String str, final int defaultValue)
     * long toLong(final String str, final long defaultValue)
     * short toShort(final String str)
     * toShort(final String str, final short defaultValue)
     */
    @Test
    public void test10() {
        //29、0、-30
        System.out.printf("%d、%d、%d%n", NumberUtils.toByte("29"), NumberUtils.toByte("x9"), NumberUtils.toByte("-30"));
        //39、45
        System.out.printf("%d、%d", NumberUtils.toByte("S30", (byte) 39), NumberUtils.toByte(null, (byte) 45));
    }

           

ArrayUtils 通用数组操作工具类

/**
     * boolean[] add(final boolean[] array, final boolean element)
     * byte[] add(final byte[] array, final byte element)
     * char[] add(final char[] array, final char element)
     * double[] add(final double[] array, final double element)
     * float[] add(final float[] array, final float element)
     * int[] add(final int[] array, final int element)
     * long[] add(final long[] array, final long element)
     * short[] add(final short[] array, final short element)
     * T[] add(final T[] array, final T element)
     * 复制给定数组并在新数组的末尾添加给定元素。返回新数组,原数组不变。(数组也能像List一样操作)
     * 如果输入数组是{@code null},则返回一个新的单元素数组,其组件类型与元素相同。
     */
    @Test
    public void add() {
        Integer[] integers = {};
        Integer[] add = ArrayUtils.add(integers, 100);
        add = ArrayUtils.add(add, 100);

        System.out.println(ArrayUtils.toString(integers));//{}
        System.out.println(ArrayUtils.toString(add));//{100,100}
    }

    /**
     * boolean[] addAll(final boolean[] array1, final boolean... array2)
     * byte[] addAll(final byte[] array1, final byte... array2)
     * char[] addAll(final char[] array1, final char... array2)
     * double[] addAll(final double[] array1, final double... array2)
     * float[] addAll(final float[] array1, final float... array2)
     * int[] addAll(final int[] array1, final int... array2)
     * long[] addAll(final long[] array1, final long... array2)
     * short[] addAll(final short[] array1, final short... array2)
     * T[] addAll(final T[] array1, final T... array2)
     * 将给定数组的所有元素添加到新数组中,新数组包含array1的所有元素,以及array2的所有元素。返回新数组,原数组不变。
     * ArrayUtils.addAll(array1, null)   = cloned copy of array1
     * ArrayUtils.addAll(null, array2)   = cloned copy of array2
     * ArrayUtils.addAll([], [])         = []
     */
    @Test
    public void addAll() {
        Integer[] integers1 = {100, 200, 300};
        Integer[] integers2 = {1, 2, 3, 4, 5};

        Integer[] addAll = ArrayUtils.addAll(integers1, integers2);

        System.out.println(ArrayUtils.toString(integers1));//{100,200,300}
        System.out.println(ArrayUtils.toString(addAll));//{100,200,300,1,2,3,4,5}
    }

    /**
     * boolean[] clone(final boolean[] array)
     * 支持操作8种基本数据类型
     * T[] clone(final T[] array)
     * 克隆数组,如果参数是 null,则返回 null。
     */
    @Test
    public void clone1() {
        Integer[] integers1 = {100, 200, 300};
        Integer[] clone = ArrayUtils.clone(integers1);
        Integer[] add = ArrayUtils.add(clone, 2000);

        System.out.println(ArrayUtils.toString(integers1));//{100,200,300}
        System.out.println(ArrayUtils.toString(add));//{100,200,300,2000}
    }

    @Test
    public void hashCode1() {
        String[] strings = {"100", "100X", "300P"};
        System.out.println(ArrayUtils.getLength(strings));//3
        System.out.println(ArrayUtils.hashCode(strings));//124771894
    }

    /**
     * boolean isSameType(final Object array1, final Object array2)
     * 检查两个数组的元素是否为同一类型。
     */
    @Test
    public void isSameType() {
        String[] strings1 = {"100", "100X", "300P"};
        String[] strings2 = {"20", "10"};
        Integer[] integers = {1, 2, 3, 4, 5};

        boolean sameType1 = ArrayUtils.isSameType(strings1, integers);
        boolean sameType2 = ArrayUtils.isSameType(strings1, strings2);
        System.out.println(sameType1);//false
        System.out.println(sameType2);//true
    }

    /**
     * boolean isSorted(final boolean[] array)
     * 支持操作8种基本数据类型
     * boolean isSorted(final T[] array)
     * 检查提供的数组是否按自然顺序排序。如果 array 为null或者长度小于2,则恒返回 true.
     */
    @Test
    public void isSorted() {
        String[] strings1 = {"aa", "bb", "c"};
        Integer[] integers = {1, 2, 13, 4, 5};
        System.out.println(ArrayUtils.isSorted(strings1));//true
        System.out.println(ArrayUtils.isSorted(integers));//false
    }

    /**
     * int[] removeAllOccurences(final int[] array, final int element)
     * 支持操作8种基本数据类型
     * T[] removeAllOccurences(final T[] array, final T element)
     * 从指定数组中移除指定的所有元素。所有后续元素都向左移动(从索引中减去一个)。
     * 如果数组不包含这样的元素,则不会从数组中移除任何元素。
     * 如果输入数组是<code>null</code>,则返回<code>null</code>
     */
    @Test
    public void removeAllOccurences() {
        String[] strings1 = {"aa", "bb", "c", "aa"};
        String[] strings = ArrayUtils.removeAllOccurences(strings1, "aa");
        System.out.println(ArrayUtils.toString(strings1));//{aa,bb,c,aa}
        System.out.println(ArrayUtils.toString(strings));//{bb,c}
    }

    /**
     * reverse(final double[] array)
     * reverse(final double[] array, final int startIndexInclusive, final int endIndexExclusive)
     * 支持操作8种基本数据类型
     * reverse(final Object[] array)
     * 反转给定数组的顺序,当 array 是null,则不会有任何效果。
     * 在给定范围内[startIndexInclusive,endIndexExclusive]反转给定数组的顺序。
     */
    @Test
    public void reverse() {
        Integer[] integers = {1, 2, 13, 4, 5};
        ArrayUtils.reverse(integers);
        System.out.println(ArrayUtils.toString(integers));//{5,4,13,2,1}
    }

    /**
     * shift(final boolean[] array, final int offset)-改变给定数组的顺序。
     * 支持操作8种基本数据类型
     * shift(final Object[] array, int startIndexInclusive, int endIndexExclusive, int offset)
     */
    @Test
    public void shift() {
        Integer[] integers = {1, 2, 13, 4, 5};
        ArrayUtils.shift(integers, 2);
        System.out.println(ArrayUtils.toString(integers));//{4,5,1,2,13}
        ArrayUtils.shift(integers, 4);
        System.out.println(ArrayUtils.toString(integers));//{5,1,2,13,4}
    }

    /**
     * shuffle(final byte[] array)
     * 使用Fisher算法随机指定数组的元素顺序。
     * <p>
     * 随机改数组的元素顺序
     * T[] toArray(final T... items)
     * 创建类型安全的泛型数组。这个方法只有在提供相同类型的参数时才有意义,这样编译器才能推断出数组本身的类型。
     */
    @Test
    public void shuffle() {
        Integer[] integers = {1, 2, 13, 4, 5};
        ArrayUtils.shuffle(integers);
        System.out.println(ArrayUtils.toString(integers));

        Integer[] toArray = ArrayUtils.toArray(1, 2, 3, 4, 5);
    }

    /**
     * double[] toPrimitive(final Double[] array)
     * double[] toPrimitive(final Double[] array, final double valueForNull)
     * 将包装类型转为基本类型,valueForNull 是当元素为 null 时的默认值,
     * 否则如果存在 null 元素,而又没有设置默认值,则会出现空指针异常。
     */
    @Test
    public void toPrimitive() {
        Integer[] integers = {1, 2, 13, null, 5};
        int[] ints = ArrayUtils.toPrimitive(integers, -1);
        System.out.println(ArrayUtils.toString(ints));//{1,2,13,-1,5}
    }

    /**
     * boolean contains(final Object[] array, final Object objectToFind): 检查 objectToFind 是否在给定数组 array 中
     */
    @Test
    public void test() {
        String[] arrs = {"1", "22", "33", null};
        System.out.println(ArrayUtils.contains(arrs, "1"));//true
        System.out.println(ArrayUtils.contains(arrs, null));//true
        System.out.println(ArrayUtils.contains(arrs, "rt"));//false
        System.out.println(ArrayUtils.contains(null, null));//false
    }
           

DateFormatUtils 时间格式化工具类

@Test
public void testFormat() {
    //将日期转为指定格式的字符串
    String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

    // 内置常量
    FastDateFormat format1 = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT;
    String format2 = format1.format(new Date());

    System.out.printf("%s  \n %s",format,format2);
}