add methods

This commit is contained in:
Looly 2025-03-31 09:50:49 +08:00
parent 3b5aa9c4bc
commit 784041ed35
2 changed files with 142 additions and 0 deletions

View File

@ -16,6 +16,10 @@
package org.dromara.hutool.core.date;
import org.dromara.hutool.core.lang.Assert;
import java.time.temporal.ChronoField;
/**
* 季度枚举
*
@ -84,4 +88,56 @@ public enum Quarter {
return null;
}
}
/**
* 根据给定的月份值返回对应的季度
*
* @param monthValue 月份值取值范围为1到12
* @return 对应的季度
* @throws IllegalArgumentException 如果月份值不在有效范围内1到12将抛出异常
*/
public static Quarter fromMonth(final int monthValue) {
ChronoField.MONTH_OF_YEAR.checkValidValue(monthValue);
return of(computeQuarterValueInternal(monthValue));
}
/**
* 根据给定的月份返回对应的季度
*
* @param month 月份
* @return 对应的季度
*/
public static Quarter fromMonth(final Month month) {
Assert.notNull(month);
final int monthValue = month.getValue();
return of(computeQuarterValueInternal(monthValue));
}
/**
* 该季度的第一个月
*
* @return 结果
*/
public Month firstMonth() {
return Month.of(value * 3 - 3);
}
/**
* 该季度最后一个月
*
* @return 结果
*/
public Month lastMonth() {
return Month.of(value * 3 - 1);
}
/**
* 计算给定月份对应的季度值
*
* @param monthValue 月份值取值范围为1到12
* @return 对应的季度值
*/
private static int computeQuarterValueInternal(final int monthValue) {
return (monthValue - 1) / 3 + 1;
}
}

View File

@ -0,0 +1,86 @@
package org.dromara.hutool.core.date;
import org.junit.jupiter.api.Test;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.*;
public class QuarterTest {
@Test
void testQ1() {
final Quarter quarter = Quarter.of(1);
assertSame(Quarter.Q1, quarter);
assertSame(quarter, Quarter.valueOf("Q1"));
assertEquals(1, Objects.requireNonNull(quarter).getValue());
assertEquals("Q1", quarter.name());
assertNull(Quarter.of(0));
final Month firstMonth = quarter.firstMonth();
assertEquals(Month.JANUARY, firstMonth);
final Month lastMonth = quarter.lastMonth();
assertEquals(Month.MARCH, lastMonth);
}
@Test
void testQ2() {
final Quarter quarter = Quarter.of(2);
assertSame(Quarter.Q2, quarter);
assertSame(quarter, Quarter.valueOf("Q2"));
assertEquals(2, Objects.requireNonNull(quarter).getValue());
assertEquals("Q2", quarter.name());
assertNull(Quarter.of(5));
// ==========
final Month firstMonth = quarter.firstMonth();
assertEquals(Month.APRIL, firstMonth);
// ==========
final Month lastMonth = quarter.lastMonth();
assertEquals(Month.JUNE, lastMonth);
}
@Test
void testQ3() {
final Quarter quarter = Quarter.of(3);
assertSame(Quarter.Q3, quarter);
assertSame(quarter, Quarter.valueOf("Q3"));
assertEquals(3, Objects.requireNonNull(quarter).getValue());
assertEquals("Q3", quarter.name());
assertThrows(IllegalArgumentException.class, () -> {
Quarter.valueOf("Abc");
});
final Month firstMonth = quarter.firstMonth();
assertEquals(Month.JULY, firstMonth);
final Month lastMonth = quarter.lastMonth();
assertEquals(Month.SEPTEMBER, lastMonth);
}
@Test
void testQ4() {
final Quarter quarter = Quarter.of(4);
assertSame(Quarter.Q4, quarter);
assertSame(quarter, Quarter.valueOf("Q4"));
assertEquals(4, quarter.getValue());
assertEquals("Q4", quarter.name());
assertThrows(IllegalArgumentException.class, () -> {
Quarter.valueOf("Q5");
});
final Month firstMonth = quarter.firstMonth();
assertEquals(Month.OCTOBER, firstMonth);
final Month lastMonth = quarter.lastMonth();
assertEquals(Month.DECEMBER, lastMonth);
}
}