forked from plusone/plusone-commons
添加计算方法。
parent
6d76e9d524
commit
707712b2c0
|
@ -5,6 +5,7 @@ import java.time.MonthDay;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||||
import xyz.zhouxy.plusone.commons.util.Numbers;
|
import xyz.zhouxy.plusone.commons.util.Numbers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,6 +46,8 @@ public enum Quarter {
|
||||||
this.firstMonth = this.lastMonth - 2;
|
this.firstMonth = this.lastMonth - 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StaticFactoryMethods
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据给定的月份值返回对应的季度
|
* 根据给定的月份值返回对应的季度
|
||||||
*
|
*
|
||||||
|
@ -52,6 +55,7 @@ public enum Quarter {
|
||||||
* @return 对应的季度
|
* @return 对应的季度
|
||||||
* @throws IllegalArgumentException 如果月份值不在有效范围内(1到12),将抛出异常
|
* @throws IllegalArgumentException 如果月份值不在有效范围内(1到12),将抛出异常
|
||||||
*/
|
*/
|
||||||
|
@StaticFactoryMethod(Quarter.class)
|
||||||
public static Quarter fromMonth(int monthValue) {
|
public static Quarter fromMonth(int monthValue) {
|
||||||
Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue);
|
Preconditions.checkArgument(Numbers.between(monthValue, 1, 13), "Invalid value for MonthOfYear: " + monthValue);
|
||||||
return of(computeQuarterValueInternal(monthValue));
|
return of(computeQuarterValueInternal(monthValue));
|
||||||
|
@ -63,6 +67,7 @@ public enum Quarter {
|
||||||
* @param month 月份
|
* @param month 月份
|
||||||
* @return 对应的季度
|
* @return 对应的季度
|
||||||
*/
|
*/
|
||||||
|
@StaticFactoryMethod(Quarter.class)
|
||||||
public static Quarter fromMonth(Month month) {
|
public static Quarter fromMonth(Month month) {
|
||||||
final int monthValue = month.getValue();
|
final int monthValue = month.getValue();
|
||||||
return of(computeQuarterValueInternal(monthValue));
|
return of(computeQuarterValueInternal(monthValue));
|
||||||
|
@ -86,6 +91,7 @@ public enum Quarter {
|
||||||
* @return 对应的季度
|
* @return 对应的季度
|
||||||
* @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常
|
* @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常
|
||||||
*/
|
*/
|
||||||
|
@StaticFactoryMethod(Quarter.class)
|
||||||
public static Quarter of(int value) {
|
public static Quarter of(int value) {
|
||||||
if (value < 1 || value > 4) {
|
if (value < 1 || value > 4) {
|
||||||
throw new IllegalArgumentException("Invalid value for Quarter: " + value);
|
throw new IllegalArgumentException("Invalid value for Quarter: " + value);
|
||||||
|
@ -93,6 +99,21 @@ public enum Quarter {
|
||||||
return ENUMS[value - 1];
|
return ENUMS[value - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StaticFactoryMethods end
|
||||||
|
|
||||||
|
// computs
|
||||||
|
|
||||||
|
public Quarter plus(long quarters) { // TODO 单元测试
|
||||||
|
final int amount = (int) ((quarters % 4) + 4);
|
||||||
|
return ENUMS[(ordinal() + amount) % 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
public Quarter minus(long quarters) { // TODO 单元测试
|
||||||
|
return plus(-(quarters % 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
// computs end
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
public int getValue() {
|
public int getValue() {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package xyz.zhouxy.plusone.commons.time;
|
package xyz.zhouxy.plusone.commons.time;
|
||||||
|
|
||||||
|
import static java.time.temporal.ChronoField.YEAR;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
|
@ -29,6 +31,14 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
/** 季度结束日期 */
|
/** 季度结束日期 */
|
||||||
private final LocalDate lastDate;
|
private final LocalDate lastDate;
|
||||||
|
|
||||||
|
private YearQuarter(int year, int quarter) {
|
||||||
|
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
|
||||||
|
this.year = year;
|
||||||
|
this.quarter = Quarter.of(quarter);
|
||||||
|
this.firstDate = this.quarter.firstMonthDay().atYear(year);
|
||||||
|
this.lastDate = this.quarter.lastMonthDay().atYear(year);
|
||||||
|
}
|
||||||
|
|
||||||
private YearQuarter(int year, @Nonnull Quarter quarter) {
|
private YearQuarter(int year, @Nonnull Quarter quarter) {
|
||||||
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
|
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
|
||||||
this.year = year;
|
this.year = year;
|
||||||
|
@ -136,6 +146,37 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||||
|
|
||||||
// Getters end
|
// Getters end
|
||||||
|
|
||||||
|
// computes
|
||||||
|
|
||||||
|
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
|
||||||
|
if (quartersToAdd == 0) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
long quarterCount = this.year * 4L + (this.quarter.getValue() - 1);
|
||||||
|
long calcQuarters = quarterCount + quartersToAdd; // safe overflow
|
||||||
|
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcQuarters, 4));
|
||||||
|
int newQuarter = (int) Math.floorMod(calcQuarters, 4) + 1;
|
||||||
|
return of(newYear, Quarter.of(newQuarter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public YearQuarter minusQuarters(long quartersToAdd) { // TODO 单元测试
|
||||||
|
return plusQuarters(-quartersToAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
|
||||||
|
if (yearsToAdd == 0) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
|
||||||
|
return of(newYear, this.quarter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public YearQuarter minusYears(long yearsToAdd) {
|
||||||
|
return plusYears(-yearsToAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// computes end
|
||||||
|
|
||||||
// hashCode & equals
|
// hashCode & equals
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -203,7 +203,7 @@ class QuarterTests {
|
||||||
@Test
|
@Test
|
||||||
void testFirstDayOfYear() {
|
void testFirstDayOfYear() {
|
||||||
int firstDayOfYear;
|
int firstDayOfYear;
|
||||||
|
|
||||||
firstDayOfYear = Quarter.Q1.firstDayOfYear(true);
|
firstDayOfYear = Quarter.Q1.firstDayOfYear(true);
|
||||||
assertEquals(1, firstDayOfYear);
|
assertEquals(1, firstDayOfYear);
|
||||||
firstDayOfYear = Quarter.Q1.firstDayOfYear(false);
|
firstDayOfYear = Quarter.Q1.firstDayOfYear(false);
|
||||||
|
|
Loading…
Reference in New Issue