mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
Week.of支持中文名称
This commit is contained in:
parent
b8db9b1a8d
commit
d14c8517cd
@ -2,7 +2,7 @@
|
||||
# 🚀Changelog
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.8.29(2024-06-27)
|
||||
# 5.8.29(2024-06-30)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 DateUtil增加offsetYear方法
|
||||
@ -13,6 +13,7 @@
|
||||
* 【core 】 BetweenFormatter支持自定义设置单位(pr#1228@Gitee)
|
||||
* 【cache 】 Cache.put变更策略,对于替换的键值对,不清理队列(issue#3618@Github)
|
||||
* 【core 】 添加 Windows 资源管理器风格字符串比较器(pr#3620@Github)
|
||||
* 【core 】 Week.of支持中文名称(issue#3637@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复AnnotationUtil可能的空指针错误
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.util.Calendar;
|
||||
@ -86,9 +87,9 @@ public enum Week {
|
||||
* @return ISO8601规范的int值
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public int getIso8601Value(){
|
||||
int iso8601IntValue = getValue() -1;
|
||||
if(0 == iso8601IntValue){
|
||||
public int getIso8601Value() {
|
||||
int iso8601IntValue = getValue() - 1;
|
||||
if (0 == iso8601IntValue) {
|
||||
iso8601IntValue = 7;
|
||||
}
|
||||
return iso8601IntValue;
|
||||
@ -172,6 +173,29 @@ public enum Week {
|
||||
*/
|
||||
public static Week of(String name) throws IllegalArgumentException {
|
||||
Assert.notBlank(name);
|
||||
|
||||
// issue#3637
|
||||
if (StrUtil.startWithAny(name, "星期", "周")) {
|
||||
char chineseNumber = name.charAt(name.length() - 1);
|
||||
switch (chineseNumber) {
|
||||
case '一':
|
||||
return MONDAY;
|
||||
case '二':
|
||||
return TUESDAY;
|
||||
case '三':
|
||||
return WEDNESDAY;
|
||||
case '四':
|
||||
return THURSDAY;
|
||||
case '五':
|
||||
return FRIDAY;
|
||||
case '六':
|
||||
return SATURDAY;
|
||||
case '日':
|
||||
return SUNDAY;
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid week name: " + name);
|
||||
}
|
||||
|
||||
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name) + 1);
|
||||
if (null == of) {
|
||||
of = Week.valueOf(name.toUpperCase());
|
||||
@ -196,7 +220,7 @@ public enum Week {
|
||||
public static Week of(DayOfWeek dayOfWeek) {
|
||||
Assert.notNull(dayOfWeek);
|
||||
int week = dayOfWeek.getValue() + 1;
|
||||
if(8 == week){
|
||||
if (8 == week) {
|
||||
// 周日
|
||||
week = 1;
|
||||
}
|
||||
|
@ -5,57 +5,83 @@ import org.junit.Test;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class WeekTest {
|
||||
|
||||
@Test
|
||||
public void ofTest(){
|
||||
public void ofTest() {
|
||||
//测试别名及大小写
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("sun"));
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("SUN"));
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("Sun"));
|
||||
assertEquals(Week.SUNDAY, Week.of("sun"));
|
||||
assertEquals(Week.SUNDAY, Week.of("SUN"));
|
||||
assertEquals(Week.SUNDAY, Week.of("Sun"));
|
||||
//测试全名及大小写
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("sunday"));
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("Sunday"));
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of("SUNDAY"));
|
||||
assertEquals(Week.SUNDAY, Week.of("sunday"));
|
||||
assertEquals(Week.SUNDAY, Week.of("Sunday"));
|
||||
assertEquals(Week.SUNDAY, Week.of("SUNDAY"));
|
||||
|
||||
Assert.assertEquals(Week.MONDAY, Week.of("Mon"));
|
||||
Assert.assertEquals(Week.MONDAY, Week.of("Monday"));
|
||||
assertEquals(Week.MONDAY, Week.of("Mon"));
|
||||
assertEquals(Week.MONDAY, Week.of("Monday"));
|
||||
|
||||
Assert.assertEquals(Week.TUESDAY, Week.of("tue"));
|
||||
Assert.assertEquals(Week.TUESDAY, Week.of("tuesday"));
|
||||
assertEquals(Week.TUESDAY, Week.of("tue"));
|
||||
assertEquals(Week.TUESDAY, Week.of("tuesday"));
|
||||
|
||||
Assert.assertEquals(Week.WEDNESDAY, Week.of("wed"));
|
||||
Assert.assertEquals(Week.WEDNESDAY, Week.of("WEDNESDAY"));
|
||||
assertEquals(Week.WEDNESDAY, Week.of("wed"));
|
||||
assertEquals(Week.WEDNESDAY, Week.of("WEDNESDAY"));
|
||||
|
||||
Assert.assertEquals(Week.THURSDAY, Week.of("thu"));
|
||||
Assert.assertEquals(Week.THURSDAY, Week.of("THURSDAY"));
|
||||
assertEquals(Week.THURSDAY, Week.of("thu"));
|
||||
assertEquals(Week.THURSDAY, Week.of("THURSDAY"));
|
||||
|
||||
Assert.assertEquals(Week.FRIDAY, Week.of("fri"));
|
||||
Assert.assertEquals(Week.FRIDAY, Week.of("FRIDAY"));
|
||||
assertEquals(Week.FRIDAY, Week.of("fri"));
|
||||
assertEquals(Week.FRIDAY, Week.of("FRIDAY"));
|
||||
|
||||
Assert.assertEquals(Week.SATURDAY, Week.of("sat"));
|
||||
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
|
||||
assertEquals(Week.SATURDAY, Week.of("sat"));
|
||||
assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest2(){
|
||||
Assert.assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
|
||||
Assert.assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));
|
||||
Assert.assertEquals(Week.TUESDAY, Week.of(DayOfWeek.TUESDAY));
|
||||
Assert.assertEquals(Week.WEDNESDAY, Week.of(DayOfWeek.WEDNESDAY));
|
||||
Assert.assertEquals(Week.THURSDAY, Week.of(DayOfWeek.THURSDAY));
|
||||
Assert.assertEquals(Week.FRIDAY, Week.of(DayOfWeek.FRIDAY));
|
||||
Assert.assertEquals(Week.SATURDAY, Week.of(DayOfWeek.SATURDAY));
|
||||
public void ofChineseTest() {
|
||||
assertEquals(Week.SUNDAY, Week.of("星期日"));
|
||||
assertEquals(Week.SUNDAY, Week.of("周日"));
|
||||
|
||||
assertEquals(Week.MONDAY, Week.of("星期一"));
|
||||
assertEquals(Week.MONDAY, Week.of("周一"));
|
||||
|
||||
assertEquals(Week.TUESDAY, Week.of("星期二"));
|
||||
assertEquals(Week.TUESDAY, Week.of("周二"));
|
||||
|
||||
assertEquals(Week.WEDNESDAY, Week.of("星期三"));
|
||||
assertEquals(Week.WEDNESDAY, Week.of("周三"));
|
||||
|
||||
assertEquals(Week.THURSDAY, Week.of("星期四"));
|
||||
assertEquals(Week.THURSDAY, Week.of("周四"));
|
||||
|
||||
assertEquals(Week.FRIDAY, Week.of("星期五"));
|
||||
assertEquals(Week.FRIDAY, Week.of("周五"));
|
||||
|
||||
assertEquals(Week.SATURDAY, Week.of("星期六"));
|
||||
assertEquals(Week.SATURDAY, Week.of("周六"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdkDayOfWeekTest(){
|
||||
Assert.assertEquals(DayOfWeek.MONDAY, Week.MONDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.TUESDAY, Week.TUESDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.WEDNESDAY, Week.WEDNESDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.THURSDAY, Week.THURSDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.FRIDAY, Week.FRIDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.SATURDAY, Week.SATURDAY.toJdkDayOfWeek());
|
||||
Assert.assertEquals(DayOfWeek.SUNDAY, Week.SUNDAY.toJdkDayOfWeek());
|
||||
public void ofTest2() {
|
||||
assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
|
||||
assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));
|
||||
assertEquals(Week.TUESDAY, Week.of(DayOfWeek.TUESDAY));
|
||||
assertEquals(Week.WEDNESDAY, Week.of(DayOfWeek.WEDNESDAY));
|
||||
assertEquals(Week.THURSDAY, Week.of(DayOfWeek.THURSDAY));
|
||||
assertEquals(Week.FRIDAY, Week.of(DayOfWeek.FRIDAY));
|
||||
assertEquals(Week.SATURDAY, Week.of(DayOfWeek.SATURDAY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdkDayOfWeekTest() {
|
||||
assertEquals(DayOfWeek.MONDAY, Week.MONDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.TUESDAY, Week.TUESDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.WEDNESDAY, Week.WEDNESDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.THURSDAY, Week.THURSDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.FRIDAY, Week.FRIDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.SATURDAY, Week.SATURDAY.toJdkDayOfWeek());
|
||||
assertEquals(DayOfWeek.SUNDAY, Week.SUNDAY.toJdkDayOfWeek());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user