Skip to content

Commit

Permalink
Week.of支持中文名称
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jun 30, 2024
1 parent b8db9b1 commit d14c851
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 🚀Changelog

-------------------------------------------------------------------------------------------------------------
# 5.8.29(2024-06-27)
# 5.8.29(2024-06-30)

### 🐣新特性
* 【core 】 DateUtil增加offsetYear方法
Expand All @@ -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可能的空指针错误
Expand Down
32 changes: 28 additions & 4 deletions hutool-core/src/main/java/cn/hutool/core/date/Week.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -86,9 +87,9 @@ public int getValue() {
* @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;
Expand Down Expand Up @@ -172,6 +173,29 @@ public static Week of(int calendarWeekIntValue) {
*/
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());
Expand All @@ -196,7 +220,7 @@ public static Week of(String name) throws IllegalArgumentException {
public static Week of(DayOfWeek dayOfWeek) {
Assert.notNull(dayOfWeek);
int week = dayOfWeek.getValue() + 1;
if(8 == week){
if (8 == week) {
// 周日
week = 1;
}
Expand Down
96 changes: 61 additions & 35 deletions hutool-core/src/test/java/cn/hutool/core/date/WeekTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,83 @@

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"));

assertEquals(Week.MONDAY, Week.of("Mon"));
assertEquals(Week.MONDAY, Week.of("Monday"));

assertEquals(Week.TUESDAY, Week.of("tue"));
assertEquals(Week.TUESDAY, Week.of("tuesday"));

assertEquals(Week.WEDNESDAY, Week.of("wed"));
assertEquals(Week.WEDNESDAY, Week.of("WEDNESDAY"));

assertEquals(Week.THURSDAY, Week.of("thu"));
assertEquals(Week.THURSDAY, Week.of("THURSDAY"));

assertEquals(Week.FRIDAY, Week.of("fri"));
assertEquals(Week.FRIDAY, Week.of("FRIDAY"));

assertEquals(Week.SATURDAY, Week.of("sat"));
assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
}

@Test
public void ofChineseTest() {
assertEquals(Week.SUNDAY, Week.of("星期日"));
assertEquals(Week.SUNDAY, Week.of("周日"));

Assert.assertEquals(Week.MONDAY, Week.of("Mon"));
Assert.assertEquals(Week.MONDAY, Week.of("Monday"));
assertEquals(Week.MONDAY, Week.of("星期一"));
assertEquals(Week.MONDAY, Week.of("周一"));

Assert.assertEquals(Week.TUESDAY, Week.of("tue"));
Assert.assertEquals(Week.TUESDAY, Week.of("tuesday"));
assertEquals(Week.TUESDAY, Week.of("星期二"));
assertEquals(Week.TUESDAY, Week.of("周二"));

Assert.assertEquals(Week.WEDNESDAY, Week.of("wed"));
Assert.assertEquals(Week.WEDNESDAY, Week.of("WEDNESDAY"));
assertEquals(Week.WEDNESDAY, Week.of("星期三"));
assertEquals(Week.WEDNESDAY, Week.of("周三"));

Assert.assertEquals(Week.THURSDAY, Week.of("thu"));
Assert.assertEquals(Week.THURSDAY, Week.of("THURSDAY"));
assertEquals(Week.THURSDAY, Week.of("星期四"));
assertEquals(Week.THURSDAY, Week.of("周四"));

Assert.assertEquals(Week.FRIDAY, Week.of("fri"));
Assert.assertEquals(Week.FRIDAY, Week.of("FRIDAY"));
assertEquals(Week.FRIDAY, Week.of("星期五"));
assertEquals(Week.FRIDAY, Week.of("周五"));

Assert.assertEquals(Week.SATURDAY, Week.of("sat"));
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
assertEquals(Week.SATURDAY, Week.of("星期六"));
assertEquals(Week.SATURDAY, Week.of("周六"));
}

@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 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(){
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 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());
}
}

0 comments on commit d14c851

Please sign in to comment.