Skip to content

Commit d4f53cc

Browse files
HuTool工具类
1 parent 28b5f72 commit d4f53cc

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
</properties>
1616

1717
<dependencies>
18+
<dependency>
19+
<groupId>cn.hutool</groupId>
20+
<artifactId>hutool-all</artifactId>
21+
<version>5.8.6</version>
22+
</dependency>
23+
1824
<dependency>
1925
<groupId>junit</groupId>
2026
<artifactId>junit</artifactId>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.collection.CollUtil;
4+
import cn.hutool.core.util.StrUtil;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class CollUtilsDemo {
10+
11+
public static void main(String[] args) {
12+
new CollUtilsDemo().pageDemo();
13+
}
14+
15+
/**
16+
* pageNo 页码,从0开始计数,0表示第一页
17+
* pageSize 每页的条目数
18+
*
19+
*/
20+
public void pageDemo() {
21+
List<String> list = Arrays.asList("1","2","3","4");
22+
List<String> pageList = CollUtil.page(0, 2, list);
23+
pageList.forEach(System.out::println);
24+
}
25+
26+
public void findOneDemo() {
27+
List<String> list = Arrays.asList("2","3");
28+
String str = CollUtil.findOne(list, num -> StrUtil.equals("2", num));
29+
System.out.println(str);
30+
}
31+
32+
public void getDemo() {
33+
List<String> list = Arrays.asList("1","2","3","4");
34+
String str = CollUtil.get(list, 0);
35+
System.out.println(str);
36+
}
37+
38+
public void addAllDemo() {
39+
List<String> list = Arrays.asList("1","2","3","4");
40+
List<String> list2 = null;
41+
//自带null的判断,不需要再重复判断
42+
CollUtil.addAll(list, list2);
43+
list.forEach(System.out::println);
44+
}
45+
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.convert.Convert;
4+
import com.alibaba.fastjson.JSON;
5+
6+
import java.util.List;
7+
8+
public class ConvertDemo {
9+
10+
public static void main(String[] args) {
11+
new ConvertDemo().convertToListDemo();
12+
}
13+
14+
/**
15+
* 转换为指定类型数组
16+
*/
17+
public void convertArrayDemo() {
18+
String[] b = { "1", "2", "3", "4" };
19+
Integer[] intArray = Convert.toIntArray(b);
20+
System.out.println(JSON.toJSONString(intArray));
21+
}
22+
23+
24+
/**
25+
* 转换为list
26+
*/
27+
public void convertToListDemo() {
28+
String[] strArr = {"a", "b", "c", "d"};
29+
List<String> strList = Convert.toList(String.class, strArr);
30+
System.out.println(strList);
31+
}
32+
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.date.DateField;
4+
import cn.hutool.core.date.DateTime;
5+
import cn.hutool.core.date.DateUtil;
6+
7+
import java.util.Date;
8+
9+
public class DateUtilDemo {
10+
11+
public static void main(String[] args) {
12+
new DateUtilDemo().format();
13+
}
14+
15+
/**
16+
* 字符串转日期
17+
*/
18+
public void parse() {
19+
String dateStr = "2017-03-01";
20+
Date date = DateUtil.parse(dateStr);
21+
System.out.println(date);
22+
}
23+
24+
/**
25+
* 字符串转日期。按格式
26+
*/
27+
public void parsePattern() {
28+
String dateStr = "2017-03-01 01:21:32";
29+
Date date = DateUtil.parse(dateStr, "yyyy-MM-dd hh:mm:ss");
30+
System.out.println(date);
31+
}
32+
33+
/**
34+
* 日期转字符串。按格式
35+
*/
36+
public void format() {
37+
Date date = new Date();
38+
String dateStr = DateUtil.format(date, "yyyy-MM-dd hh:mm:ss");
39+
System.out.println(dateStr);
40+
}
41+
42+
43+
44+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.lang.Snowflake;
4+
import cn.hutool.core.util.IdUtil;
5+
6+
public class IdUtilsDemo {
7+
8+
9+
public static void main(String[] args) {
10+
new IdUtilsDemo().snowFlake();
11+
}
12+
13+
/**
14+
* 获取随机UUID
15+
*/
16+
public void uuidDemo() {
17+
String uuid = IdUtil.randomUUID();
18+
//格式:cbb021c7-cb48-44cd-b8ba-814620ee4340
19+
System.out.println(uuid);
20+
}
21+
22+
/**
23+
* 简化的UUID,去掉了横线
24+
*/
25+
public void simpleUUIDDemo() {
26+
String uuid = IdUtil.simpleUUID();
27+
//格式:a7e0edfb17ac4120a03842f938f88d34
28+
System.out.println(uuid);
29+
}
30+
31+
/**
32+
* 雪花算法。获取唯一id。
33+
*/
34+
public void getNextIdDemo() {
35+
long id = IdUtil.getSnowflakeNextId();
36+
//格式:1648328806748430336
37+
System.out.println(id);
38+
39+
String idStr = IdUtil.getSnowflakeNextIdStr();
40+
//格式:"1648328806752624640"
41+
System.out.println(idStr);
42+
}
43+
44+
45+
/**
46+
* 雪花算法。配合终端ID和数据中心ID。
47+
*/
48+
public void snowFlake() {
49+
//workerId 终端ID
50+
// datacenterId 数据中心ID
51+
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
52+
long id = snowflake.nextId();
53+
//格式:1648328965712121856
54+
System.out.println(id);
55+
56+
String idStr = snowflake.nextIdStr();
57+
//格式:1648328965716316160
58+
System.out.println(idStr);
59+
60+
}
61+
62+
63+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.collection.CollUtil;
4+
import cn.hutool.core.collection.ListUtil;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class ListUtilsDemo {
10+
11+
public void page() {
12+
List<String> list = Arrays.asList("1","2","3","4");
13+
List<String> pageList = ListUtil.page(0, 2, list);
14+
pageList.forEach(System.out::println);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hutool;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
5+
public class StrUtilsDemo {
6+
7+
public void equalDemo() {
8+
String num = "2";
9+
if (StrUtil.equals("2", num)) {
10+
System.out.println(num);
11+
}
12+
}
13+
14+
15+
16+
}

0 commit comments

Comments
 (0)