This commit is contained in:
Looly
2022-03-27 18:47:41 +08:00
parent fbc4662271
commit c2e1bbafc8
9 changed files with 114 additions and 67 deletions

View File

@@ -174,11 +174,12 @@ public enum Month {
* 解析别名为Month对象别名如jan或者JANUARY不区分大小写
*
* @param name 别名值
* @return 月份int值
* @return 月份枚举Month非空
* @throws IllegalArgumentException 如果别名无对应的枚举,抛出此异常
* @since 5.8.0
*/
public static Month of(String name) throws IllegalArgumentException {
Assert.notBlank(name);
Month of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name));
if (null == of) {
of = Month.valueOf(name.toUpperCase());
@@ -186,6 +187,16 @@ public enum Month {
return of;
}
/**
* {@link java.time.Month}转换为Month对象
* @param month {@link java.time.Month}
* @return Month
* @since 5.8.0
*/
public static Month of(java.time.Month month){
return of(month.ordinal());
}
/**
* 获得指定月的最后一天
*

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.date;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import java.time.DayOfWeek;
@@ -165,11 +166,12 @@ public enum Week {
* 解析别名为Week对象别名如sun或者SUNDAY不区分大小写
*
* @param name 别名值
* @return 周int值
* @return 周枚举Week非空
* @throws IllegalArgumentException 如果别名无对应的枚举,抛出此异常
* @since 5.8.0
*/
public static Week of(String name) throws IllegalArgumentException {
Assert.notBlank(name);
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name));
if (null == of) {
of = Week.valueOf(name.toUpperCase());
@@ -192,8 +194,9 @@ public enum Week {
* @since 5.7.14
*/
public static Week of(DayOfWeek dayOfWeek) {
int week = dayOfWeek.ordinal() + 2;
if (week > 7) {
Assert.notNull(dayOfWeek);
int week = dayOfWeek.ordinal() + 1;
if (7 == week) {
week = 1;
}
return of(week);

View File

@@ -51,16 +51,19 @@ public class MonthTest {
@Test
public void ofTest(){
Month jan = Month.of("Jan");
Assert.assertEquals(Month.JANUARY, jan);
Month month = Month.of("Jan");
Assert.assertEquals(Month.JANUARY, month);
jan = Month.of("JAN");
Assert.assertEquals(Month.JANUARY, jan);
month = Month.of("JAN");
Assert.assertEquals(Month.JANUARY, month);
jan = Month.of("FEBRUARY");
Assert.assertEquals(Month.FEBRUARY, jan);
month = Month.of("FEBRUARY");
Assert.assertEquals(Month.FEBRUARY, month);
jan = Month.of("February");
Assert.assertEquals(Month.FEBRUARY, jan);
month = Month.of("February");
Assert.assertEquals(Month.FEBRUARY, month);
month = Month.of(java.time.Month.FEBRUARY);
Assert.assertEquals(Month.FEBRUARY, month);
}
}

View File

@@ -37,6 +37,16 @@ public class WeekTest {
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
}
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));
}
@Test
public void toJdkDayOfWeekTest(){
Assert.assertEquals(DayOfWeek.MONDAY, Week.MONDAY.toJdkDayOfWeek());