优化 YearQuarter 的代码

dev
ZhouXY108 2024-09-02 17:09:36 +08:00
parent 1a9960dbf1
commit 322ecf4db8
1 changed files with 13 additions and 10 deletions

View File

@ -31,14 +31,6 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
/** 季度结束日期 */
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) {
Preconditions.checkNotNull(quarter, "Quarter can not be null.");
this.year = year;
@ -47,6 +39,17 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
this.lastDate = quarter.lastMonthDay().atYear(year);
}
/**
* {@link YearQuarter}
*
* @param year
* @param quarter
* @return {@link YearQuarter}
*/
public static YearQuarter of(int year, int quarter) {
return of(year, Quarter.of(quarter));
}
/**
* {@link YearQuarter}
*
@ -65,7 +68,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
* @return {@link YearQuarter}
*/
public static YearQuarter of(@Nonnull LocalDate date) {
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
return of(date.getYear(), Quarter.fromMonth(date.getMonth()));
}
/**
@ -202,7 +205,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
public int compareTo(YearQuarter other) {
int cmp = (this.year - other.year);
if (cmp == 0) {
cmp = (this.quarter.compareTo(other.quarter));
cmp = this.quarter.compareTo(other.quarter);
}
return cmp;
}