一、字符串相关
String 类在平常的java开发过程中用的最多的一个类,经常需要围绕String做一些处理,JDK自身也提供了很多String API,但是功能都比较基础。通常需要结合String 多个方法才能完成一个业务功能。
首先介绍的是Apache提供的StringUtils工具类
需要引入的pom依赖:
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.10</version></dependency>
:::info 说明:commons-lang3是一直在维护的新版本,建议使用这个,而不是commons-lang :::
1、判断字符串是否为空
String str = "hello world";if (null == str || str.isEmpty()) {}
上面的写法想必大家应该都写过,这段代码虽然简单,但是一不小心很容易造成空指针异常
使用StringUtils写法如下:
if(StringUtils.isEmpty(str)){}
上面的代码判断为字符串为空是不是更简单呢,不过需要注意的是下面这种情况
public class UtilsTest {public static void main(String[] args) {String str1 = "";String str2 = " ";System.out.println(StringUtils.isEmpty(str1));System.out.println(StringUtils.isEmpty(str2));}}
2、字符串固定长度
String result = StringUtils.leftPad("test",8,"0");
上面leftPad方法表示返回字符串固定长度8,如果不足,在左边补0
3、关键字替换
StringUtils.replace("abc","a","A");StringUtils.replaceOnce("aba","a","A");StringUtils.replacePattern("ABCabc123","[^A-Z0-9]+","");
4、字符串拼接
String[] array = new String[]{"abc","123","456"};StringBuilder stringBuilder = new StringBuilder();for(String s:array){stringBuilder.append(s).append(";");}System.out.println(stringBuilder.toString());
StringUtils类使用
String[] array = new String[]{"abc","123","456"};StringUtils.join(array,";")
5、字符串拆分
StringUtils.split("a..b.c",",")
二、日期相关
DateUtils和DateFormatUtils
在JDK8之前,Java只提供了一个Date类,当需要将Date按照一定格式转换成字符串的时候,这个时候需要使用到SimpleDateFormat类。
1、日期转指定格式的字符串
public class UtilsTest {public static void main(String[] args) {Date date = new Date();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dateString=simpleDateFormat.format(date);System.out.println(dateString);}}
看起来好像也很简单,但是这里面用到的SimpleDateFormat不是线程安全的,这就导致在多线程环境下可能出现线程安全的问题,因此,可以使用common-lang3下的时间工具类DataUtils/DateFormateUtils,从而解决Date与字符串的转换问题。
方法很简单,上面代码的转换等价于如下代码:
String dateString = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");System.out.println(dateString);
2、字符串转日期
Date date = DateUtils.parseDate("2020-10-15 22:00:00","yyyy-MM-dd HH:mm:ss");System.out.println(date);
输出结果:
Thu Oct 15 22:00:00 CST 2020
3、DateUtils时间计算
除开上面的日期转换之外,DateUtils还提供了很方便的时间计算的功能函数
直接贴代码:
public class UtilsTest {public static void main(String[] args){Date now = new Date();//Date加1天Date addDays = DateUtils.addDays(now, 1);//Date加33分钟Date addMinutes = DateUtils.addMinutes(now, 33);//Date减去60秒Date addSeconds = DateUtils.addSeconds(now, -60);//过滤时分秒Date date = Dateutils.truncate(now, Calendar.DATE);System.out.println(date);System.out.println(addDays);System.out.println(addMinutes);System.out.println(addSeconds);}}
输出结果:
Thu 0ct 15 00:00:00 CST 2020Fri 0ct 16 22:50:46 CST 2020Thu 0ct 15 23:23:46 CST 2020Thu 0ct 15 22:49:46 CST 2020
三、集合数组相关
1、判断是否为空
以前这样判断
List<String> list = new ArrayList<String>();if(null==list||list.isEmpty()){}
上面写起来其实也不难,但是也比较容易抛出空指针异常,现在可以通过使用commons-collections类来判断是否为空
pom依赖:
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version></dependency>
使用CollectionUtils/MapUtils/ArrayUtils进行判空判断
List<String> list = new ArrayList<String>();Map map = new HashMap();String[] strings = new String[];map.put("name", "zxb");if (CollectionUtils.isEmpty(list)) {}if (MapUtils.isEmpty(map)){}if(ArrayUtils.isEmpty(strings)){}
:::tips 其中要注意的是ArrayUtils是commons-lang3包下的 :::
2、将数组快速加入到list中
直接上代码:
public class UtilsTest {public static void main(String[] args){List<String> list = new ArrayList<String>();list.add("A");list.add("B");list.add("C");String[] arrays = new String[]{ "D", "E", "H"};CollectionUtils.addAll(list, arrays);System.out.print1n(list);}}
输出:
[A, B, C,D, E, H]
其他的方法这里不做过多补充,Google的Guava工具类也有很多对于集合的操作增强类
四、计时相关
以前计时:
long start = System.currentTimeMillis();long end = System.currentTimeMillis();System.out.println("运行时间:"+(end-start)+"ms");
上面想必大家都用过,确实很简单,但是这种计时是非常不灵活的,默认情况下只能取ms为单位,如果需要分钟呢?那就需要另外计算来转换,这里给大家介绍Guava stopwatch计时工具类来统计程序执行时间
pom依赖:
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>19.0</version></dependency>
使用Stopwatch工具类统计程序执行时间:
public class UtilsTest{public static void main(String[] args) throws InterruptedException {Stopwatch stopwatch = Stopwatch.createStarted();//创建计时器,但是不是立即执行,需要主动调用start方法开始计时//Stopwatch stopwatch = Stopwatch.createUnstacted();//stopwatch.start();//这里当做其他代码花费时间TimeUnit.SECONDS.sleep( timeout: 2l);//当前消耗的时间System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));TimeUnit.SECONDS.sleep( timeout: 2l);stopwatch.stop();System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));//重新开始,从原来时间基础上计算,如果想要重新从0开始计算,需要调用stopwatch.reset()stopwatch.start();TimeUnit.SECONDS.sleep( timeout: 2l);system.out.println(stopwatch.elapsed(TimeUnit.SECONDS));}}
输出结果:
246
