概览

  • commons-lang3
  • commons-collections
  • common-beanutils
  • commons-io

commons-lang3

Maven

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

使用

判断是否为数字

1
2
org.apache.commons.lang3.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric ("aaa123456789");

首字母转成大写

1
2
3
String str = "yideng";
String capitalize = StringUtils.capitalize(str);
System.out.println(capitalize); // 输出Yideng

重复拼接字符串

1
2
String str = StringUtils.repeat("ab", 2);
System.out.println(str); // 输出abab

格式化日期

1
2
3
4
5
6
7
8
9
// Date 转 String
String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
System.out.println(date); // 输出 2021-05-01 01:01:01

// String 转 Date
Date date = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");

// 计算一个小时后的日期
Date date = DateUtils.addHours(new Date(), 1);

包装临时对象

1
2
3
4
5
6
7
// 返回两个字段
ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng");
System.out.println(pair.getLeft() + "," + pair.getRight()); // 输出 1,yideng

// 返回三个字段
ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "yideng", new Date());
System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight()); // 输出 1,yideng,Wed Apr 07 23:30:00 CST 2021

commons-collections

Maven

1
2
3
4
5
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

使用

集合判空

1
2
3
//判断空或非空
CollectionUtils.isEmpty(list)
CollectionUtils.isNotEmpty(list)

集合交、并、差集

1
2
3
4
5
6
// 两个集合取交集
Collection<String> collection = CollectionUtils.retainAll(listA, listB);
// 两个集合取并集
Collection<String> collection = CollectionUtils.union(listA, listB);
// 两个集合取差集
Collection<String> collection = CollectionUtils.subtract(listA, listB);

common-beanutils

操作对象

Maven

1
2
3
4
5
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>

使用

对象属性赋值

1
2
3
4
5
6
7
8
9
10
public class User {
private Integer id;
private String name;
}

User user = new User();
BeanUtils.setProperty(user, "id", 1);
BeanUtils.setProperty(user, "name", "yideng");
System.out.println(BeanUtils.getProperty(user, "name")); // 输出 yideng
System.out.println(user); // 输出 {"id":1,"name":"yideng"}

对象和 map 互转

1
2
3
4
5
6
7
8
// 对象转map
Map<String, String> map = BeanUtils.describe(user);
System.out.println(map); // 输出 {"id":"1","name":"yideng"}

// map 转对象
User newUser = new User();
BeanUtils.populate(newUser, map);
System.out.println(newUser); // 输出 {"id":1,"name":"yideng"}

commons-io

文件流处理

Maven

1
2
3
4
5
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>

使用

文件操作:读、写、复制

1
2
3
4
5
6
7
File file = new File("demo1.txt");
// 读取文件
List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());
// 写入文件
FileUtils.writeLines(new File("demo2.txt"), lines);
// 复制文件
FileUtils.copyFile(srcFile, destFile);