字符串工具
@Test
public void stringUtilsTest ( ) {
StringUtils. abbreviate ( "abcdefg" , 6 ) ;
StringUtils. appendIfMissing ( "abc" , "xyz" ) ;
StringUtils. appendIfMissingIgnoreCase ( "abcXYZ" , "xyz" ) ;
StringUtils. capitalize ( "cat" ) ;
StringUtils. uncapitalize ( "Cat" ) ;
StringUtils. center ( "abcd" , 2 ) ;
StringUtils. center ( "ab" , - 1 ) ;
StringUtils. center ( "ab" , 4 ) ;
StringUtils. center ( "a" , 4 , "yz" ) ;
StringUtils. center ( "abc" , 7 , "" ) ;
StringUtils. chomp ( "abc\r\n" ) ;
StringUtils. contains ( "abc" , "z" ) ;
StringUtils. containsIgnoreCase ( "abc" , "A" ) ;
StringUtils. countMatches ( "abba" , "a" ) ;
StringUtils. deleteWhitespace ( " ab c " ) ;
StringUtils. difference ( "abcde" , "abxyz" ) ;
StringUtils. endsWith ( "abcdef" , "def" ) ;
StringUtils. endsWithIgnoreCase ( "ABCDEF" , "def" ) ;
StringUtils. endsWithAny ( "abcxyz" , new String [ ] { null, "xyz" , "abc" } ) ;
StringUtils. startsWith ( "abcdef" , "abc" ) ;
StringUtils. startsWithIgnoreCase ( "ABCDEF" , "abc" ) ;
StringUtils. startsWithAny ( "abcxyz" , new String [ ] { null, "xyz" , "abc" } ) ;
StringUtils. equals ( "abc" , "abc" ) ;
StringUtils. equalsIgnoreCase ( "abc" , "ABC" ) ;
StringUtils. getCommonPrefix ( new String [ ] { "abcde" , "abxyz" } ) ;
StringUtils. indexOf ( "aabaabaa" , "b" ) ;
StringUtils. indexOf ( "aabaabaa" , "b" , 3 ) ;
StringUtils. ordinalIndexOf ( "aabaabaa" , "a" , 3 ) ;
StringUtils. lastIndexOf ( "aabaabaa" , 'b' ) ;
StringUtils. lastIndexOf ( "aabaabaa" , 'b' , 4 ) ;
StringUtils. lastOrdinalIndexOf ( "aabaabaa" , "ab" , 2 ) ;
StringUtils. isAllUpperCase ( "ABC" ) ;
StringUtils. isAllLowerCase ( "abC" ) ;
StringUtils. isBlank ( null) ;
StringUtils. isBlank ( "" ) ;
StringUtils. isBlank ( " " ) ;
StringUtils. isNoneBlank ( " " , "bar" ) ;
StringUtils. isEmpty ( null) ;
StringUtils. isEmpty ( "" ) ;
StringUtils. isEmpty ( " " ) ;
StringUtils. isNoneEmpty ( " " , "bar" ) ;
StringUtils. isNumeric ( "123" ) ;
StringUtils. isNumeric ( "12 3" ) ;
StringUtils. isNumericSpace ( "12 3" ) ;
StringUtils. join ( new int [ ] { 1 , 2 , 3 } , ';' ) ;
StringUtils. upperCase ( "aBc" ) ;
StringUtils. lowerCase ( "aBc" ) ;
StringUtils. swapCase ( "The dog has a BONE" ) ;
StringUtils. replace ( "aba" , "a" , "z" ) ;
StringUtils. replaceOnce ( "aba" , "a" , "z" ) ;
StringUtils. overlay ( "abcdef" , "zz" , 2 , 4 ) ;
StringUtils. replaceEach ( "abcde" , new String [ ] { "ab" , "d" } , new String [ ] { "w" , "t" } ) ;
StringUtils. repeat ( 'e' , 3 ) ;
StringUtils. leftPad ( "test" , 8 , "0" ) ;
StringUtils. rightPad ( "test" , 8 , "0" ) ;
StringUtils. reverse ( "bat" ) ;
StringUtils. remove ( "queued" , 'u' ) ;
StringUtils. split ( "a..b.c" , '.' ) ;
StringUtils. split ( "ab:cd:ef" , ":" , 2 ) ;
StringUtils. splitByWholeSeparator ( "ab-!-cd-!-ef" , "-!-" , 2 ) ;
StringUtils. splitByWholeSeparatorPreserveAllTokens ( "ab::cd:ef" , ":" ) ;
StringUtils. strip ( " ab c " ) ;
StringUtils. stripToNull ( null) ;
StringUtils. stripToEmpty ( null) ;
StringUtils. substring ( "abcd" , 2 ) ;
StringUtils. substring ( "abcdef" , 2 , 4 ) ;
StringUtils. left ( "abc" , 2 ) ;
StringUtils. right ( "abc" , 2 ) ;
StringUtils. mid ( "abcdefg" , 2 , 4 ) ;
StringUtils. substringBefore ( "abcba" , "b" ) ;
StringUtils. substringBeforeLast ( "abcba" , "b" ) ;
StringUtils. substringAfter ( "abcba" , "b" ) ;
StringUtils. substringAfterLast ( "abcba" , "b" ) ;
StringUtils. substringBetween ( "tagabctag" , "tag" ) ;
StringUtils. substringBetween ( "yabczyabcz" , "y" , "z" ) ;
}
数字工具
@Test
public void numberUtilsTest ( ) {
NumberUtils. max ( 1 , 2 , 3 , 4 ) ;
NumberUtils. min ( 1 , 2 , 3 , 4 ) ;
NumberUtils. isDigits ( "153.4" ) ;
NumberUtils. isDigits ( "0123" ) ;
NumberUtils. isCreatable ( "0321.1" ) ;
NumberUtils. isCreatable ( "08" ) ;
NumberUtils. isCreatable ( "09" ) ;
NumberUtils. isCreatable ( "10" ) ;
NumberUtils. toInt ( "123" ) ;
NumberUtils. toInt ( "123" , 1 ) ;
NumberUtils. toScaledBigDecimal ( "2.1" ) ;
NumberUtils. toScaledBigDecimal ( "2.1" , 2 , RoundingMode. HALF_UP) ;
}
数组工具
@Test
public void arrayUtils ( ) {
String[ ] array = ArrayUtils. toArray ( "1" , "2" ) ;
ArrayUtils. contains ( array, "33" ) ;
Map< Object, Object> map = ArrayUtils. toMap ( new String [ ] [ ] { { "RED" , "#FF0000" } , { "GREEN" , "#00FF00" } , { "BLUE" , "#0000FF" } } ) ;
}
日期工具
@Test
public void dateUtils ( ) throws ParseException {
DateFormatUtils. format ( new Date ( ) , "yyyy-MM-dd HH:mm:ss" ) ;
DateUtils. parseDate ( "2020-05-07 22:00:00" , "yyyy-MM-dd HH:mm:ss" ) ;
Date now = new Date ( ) ;
Date addDays = DateUtils. addDays ( now, 1 ) ;
Date addMinutes = DateUtils. addMinutes ( now, 33 ) ;
Date addSeconds = DateUtils. addSeconds ( now, - 233 ) ;
boolean sameDay = DateUtils. isSameDay ( addDays, addMinutes) ;
Date truncate = DateUtils. truncate ( now, Calendar. DATE) ;
}
随机数工具
@Test
public void randomTest ( ) {
RandomStringUtils. random ( 5 ) ;
RandomStringUtils. random ( 15 , true , false ) ;
RandomStringUtils. random ( 15 , "abcdefgABCDEFG123456789" ) ;
RandomStringUtils. random ( 5 , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , '1' , '2' , '3' ) ;
RandomStringUtils. random ( 1009 , 5 , 129 , true , true ) ;
RandomStringUtils. randomAlphabetic ( 15 ) ;
RandomStringUtils. randomAlphabetic ( 2 , 15 ) ;
RandomStringUtils. randomAlphanumeric ( 5 ) ;
RandomStringUtils. randomAlphanumeric ( 5 , 68 ) ;
RandomStringUtils. randomNumeric ( 5 ) ;
RandomStringUtils. randomNumeric ( 15 , 20 ) ;
}
时间计算
@Test
public void time ( ) throws InterruptedException {
StopWatch watch = StopWatch. createStarted ( ) ;
StopWatch watch1 = StopWatch. create ( ) ;
watch1. start ( ) ;
TimeUnit. SECONDS. sleep ( 2 L) ;
System. out. println ( "统计从开始到现在运行时间:" + watch. getTime ( ) + "ms" ) ;
TimeUnit. SECONDS. sleep ( 2 L) ;
watch. split ( ) ;
System. out. println ( "从start到此刻为止的时间:" + watch. getTime ( ) ) ;
System. out. println ( "从开始到第一个切入点运行时间:" + watch. getSplitTime ( ) ) ;
Thread. sleep ( 1000 ) ;
watch. split ( ) ;
System. out. println ( "从开始到第二个切入点运行时间:" + watch. getSplitTime ( ) ) ;
watch. reset ( ) ;
watch. start ( ) ;
Thread. sleep ( 1000 ) ;
System. out. println ( "重新开始后到当前运行时间是:" + watch. getTime ( ) ) ;
watch. suspend ( ) ;
Thread. sleep ( 6000 ) ;
watch. resume ( ) ;
System. out. println ( "恢复后执行的时间是:" + watch. getTime ( ) ) ;
watch. stop ( ) ;
System. out. println ( "花费的时间》》" + watch. getTime ( ) + "ms" ) ;
System. out. println ( "花费的时间》》" + watch. getTime ( TimeUnit. SECONDS) + "s" ) ;
}