-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
285 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## 由来 | ||
|
||
文件名操作工具类,主要针对文件名获取主文件名、扩展名等操作,同时针对Windows平台,清理无效字符。 | ||
|
||
此工具类在`5.4.1`之前是`FileUtil`的一部分,后单独剥离为`FileNameUtil`工具。 | ||
|
||
## 使用 | ||
|
||
1. 获取文件名 | ||
|
||
```java | ||
File file = FileUtil.file("/opt/test.txt"); | ||
|
||
// test.txt | ||
String name = FileNameUtil.getName(file); | ||
``` | ||
|
||
2. 获取主文件名和扩展名 | ||
|
||
```java | ||
File file = FileUtil.file("/opt/test.txt"); | ||
|
||
// "test" | ||
String name = FileNameUtil.mainName(file); | ||
|
||
// "txt" | ||
String name = FileNameUtil.extName(file); | ||
``` | ||
|
||
> 注意,此处获取的扩展名不带`.`。 | ||
> `FileNameUtil.mainName`和`FileNameUtil.getPrefix`等价,同理`FileNameUtil.extName`和`FileNameUtil.getSuffix`等价,保留两个方法用于适应不同用户的习惯。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
## 介绍 | ||
|
||
从Hutool的5.4.x开始,Hutool加入了针对JDK8+日期API的封装,此工具类的功能包括`LocalDateTime`和`LocalDate`的解析、格式化、转换等操作。 | ||
|
||
## 使用 | ||
|
||
1. 日期转换 | ||
|
||
```java | ||
String dateStr = "2020-01-23T12:23:56"; | ||
DateTime dt = DateUtil.parse(dateStr); | ||
|
||
// Date对象转换为LocalDateTime | ||
LocalDateTime of = LocalDateTimeUtil.of(dt); | ||
|
||
// 时间戳转换为LocalDateTime | ||
of = LocalDateTimeUtil.ofUTC(dt.getTime()); | ||
``` | ||
|
||
2. 日期字符串解析 | ||
|
||
```java | ||
// 解析ISO时间 | ||
LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56"); | ||
|
||
|
||
// 解析自定义格式时间 | ||
localDateTime = LocalDateTimeUtil.parse("2020-01-23", DatePattern.NORM_DATE_PATTERN); | ||
``` | ||
|
||
解析同样支持`LocalDate`: | ||
|
||
```java | ||
LocalDate localDate = LocalDateTimeUtil.parseDate("2020-01-23"); | ||
|
||
// 解析日期时间为LocalDate,时间部分舍弃 | ||
localDate = LocalDateTimeUtil.parseDate("2020-01-23T12:23:56", DateTimeFormatter.ISO_DATE_TIME); | ||
``` | ||
|
||
3. 日期格式化 | ||
|
||
```java | ||
LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56"); | ||
|
||
// "2020-01-23 12:23:56" | ||
String format = LocalDateTimeUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN); | ||
``` | ||
|
||
4. 日期偏移 | ||
|
||
```java | ||
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56"); | ||
|
||
// 增加一天 | ||
// "2020-01-24T12:23:56" | ||
LocalDateTime offset = LocalDateTimeUtil.offset(localDateTime, 1, ChronoUnit.DAYS); | ||
``` | ||
|
||
如果是减少时间,offset第二个参数传负数即可: | ||
|
||
```java | ||
// "2020-01-22T12:23:56" | ||
offset = LocalDateTimeUtil.offset(localDateTime, -1, ChronoUnit.DAYS); | ||
``` | ||
|
||
5. 计算时间间隔 | ||
|
||
```java | ||
LocalDateTime start = LocalDateTimeUtil.parse("2019-02-02T00:00:00"); | ||
LocalDateTime end = LocalDateTimeUtil.parse("2020-02-02T00:00:00"); | ||
|
||
Duration between = LocalDateTimeUtil.between(start, end); | ||
|
||
// 365 | ||
between.toDays(); | ||
``` | ||
|
||
6. 一天的开始和结束 | ||
|
||
```java | ||
LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56"); | ||
|
||
// "2020-01-23T00:00" | ||
LocalDateTime beginOfDay = LocalDateTimeUtil.beginOfDay(localDateTime); | ||
|
||
// "2020-01-23T23:59:59.999999999" | ||
LocalDateTime endOfDay = LocalDateTimeUtil.endOfDay(localDateTime); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
## 介绍 | ||
|
||
CGLib (Code Generation Library) 是一个强大的,高性能,高质量的Code生成类库,通过此库可以完成动态代理、Bean拷贝等操作。 | ||
|
||
Hutool在`5.4.1`之后加入对Cglib的封装——`CglibUtil`,用于解决Bean拷贝的性能问题。 | ||
|
||
## 使用 | ||
|
||
### 引入Cglib | ||
|
||
```xml | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib</artifactId> | ||
<version>${cglib.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
``` | ||
|
||
### 使用 | ||
|
||
1. Bean拷贝 | ||
|
||
首先我们定义两个Bean: | ||
```java | ||
@Data | ||
public class SampleBean { | ||
private String value; | ||
} | ||
@Data | ||
public class OtherSampleBean { | ||
private String value; | ||
} | ||
``` | ||
|
||
> @Data是Lombok的注解,请自行补充get和set方法,或者引入Lombok依赖 | ||
```java | ||
SampleBean bean = new SampleBean(); | ||
bean.setValue("Hello world"); | ||
|
||
OtherSampleBean otherBean = new OtherSampleBean(); | ||
|
||
CglibUtil.copy(bean, otherBean); | ||
|
||
// 值为"Hello world" | ||
otherBean.getValue(); | ||
``` | ||
|
||
当然,目标对象也可以省略,你可以传入一个class,让Hutool自动帮你实例化它: | ||
|
||
``` | ||
OtherSampleBean otherBean2 = CglibUtil.copy(bean, OtherSampleBean.class); | ||
// 值为"Hello world" | ||
otherBean.getValue(); | ||
``` | ||
|
||
## 关于性能 | ||
|
||
Cglib的性能是目前公认最好的,其时间主要耗费在`BeanCopier`创建上,因此,Hutool根据传入Class不同,缓存了`BeanCopier`对象,使性能达到最好。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
## 介绍 | ||
|
||
拼音工具类在旧版本的Hutool中在core包中,但是发现自己实现相关功能需要庞大的字典,放在core包中便是累赘。 | ||
|
||
于是为了方便,Hutool封装了拼音的门面,用于兼容以下拼音库: | ||
|
||
1. TinyPinyin | ||
2. JPinyin | ||
3. Pinyin4j | ||
|
||
和其它门面模块类似,采用SPI方式识别所用的库。例如你想用Pinyin4j,只需引入jar,Hutool即可自动识别。 | ||
|
||
## 使用 | ||
|
||
### 引入库 | ||
|
||
以下为Hutool支持的拼音库的pom坐标,你可以选择任意一个引入项目中,如果引入多个,Hutool会按照以上顺序选择第一个使用。 | ||
|
||
```xml | ||
<dependency> | ||
<groupId>io.github.biezhi</groupId> | ||
<artifactId>TinyPinyin</artifactId> | ||
<version>2.0.3.RELEASE</version> | ||
</dependency> | ||
``` | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.belerweb</groupId> | ||
<artifactId>pinyin4j</artifactId> | ||
<version>2.5.1</version> | ||
</dependency> | ||
``` | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.github.stuxuhai</groupId> | ||
<artifactId>jpinyin</artifactId> | ||
<version>1.1.8</version> | ||
</dependency> | ||
``` | ||
|
||
### 使用 | ||
|
||
1. 获取拼音 | ||
|
||
```java | ||
// "ni hao" | ||
String pinyin = PinyinUtil.getPinyin("你好", " "); | ||
``` | ||
|
||
这里定义的分隔符为空格,你也可以按照需求自定义分隔符,亦或者使用""无分隔符。 | ||
|
||
2. 获取拼音首字母 | ||
|
||
```java | ||
// "h, s, d, y, g" | ||
String result = PinyinUtil.getFirstLetter("H是第一个", ", "); | ||
``` | ||
|
||
3. 自定义拼音库(拼音引擎) | ||
|
||
```java | ||
Pinyin4jEngine engine = new Pinyin4jEngine(); | ||
|
||
// "ni hao h" | ||
String pinyin = engine.getPinyin("你好h", " "); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters