2024-10-12 00:57:35 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2024 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-08-26 17:36:36 +08:00
|
|
|
|
package xyz.zhouxy.plusone.commons.time;
|
2024-08-19 18:31:48 +08:00
|
|
|
|
|
|
|
|
|
import java.time.Month;
|
|
|
|
|
import java.time.MonthDay;
|
2024-11-19 20:29:33 +08:00
|
|
|
|
import java.time.temporal.ChronoField;
|
2024-08-19 18:31:48 +08:00
|
|
|
|
|
2024-11-19 20:29:33 +08:00
|
|
|
|
import com.google.common.collect.Range;
|
2024-08-19 18:31:48 +08:00
|
|
|
|
|
2024-08-28 17:20:54 +08:00
|
|
|
|
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
2024-08-19 18:31:48 +08:00
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 季度
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
2024-08-20 00:42:11 +08:00
|
|
|
|
*/
|
2024-08-19 18:31:48 +08:00
|
|
|
|
public enum Quarter {
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/** 第一季度 */
|
2024-08-26 10:24:07 +08:00
|
|
|
|
Q1(1),
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/** 第二季度 */
|
2024-08-26 10:24:07 +08:00
|
|
|
|
Q2(2),
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/** 第三季度 */
|
2024-08-26 10:24:07 +08:00
|
|
|
|
Q3(3),
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/** 第四季度 */
|
2024-08-26 10:24:07 +08:00
|
|
|
|
Q4(4),
|
2024-08-19 18:31:48 +08:00
|
|
|
|
;
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/** 季度值 (1/2/3/4) */
|
2024-08-19 18:31:48 +08:00
|
|
|
|
private final int value;
|
|
|
|
|
|
2024-11-19 20:29:33 +08:00
|
|
|
|
private final Range<Integer> monthRange;
|
2024-08-19 18:31:48 +08:00
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
/** 常量值 */
|
|
|
|
|
private static final Quarter[] ENUMS = Quarter.values();
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param value 季度值 (1/2/3/4)
|
|
|
|
|
*/
|
2024-08-26 10:24:07 +08:00
|
|
|
|
Quarter(int value) {
|
2024-08-19 18:31:48 +08:00
|
|
|
|
this.value = value;
|
|
|
|
|
|
2024-11-19 20:29:33 +08:00
|
|
|
|
final int lastMonth = value * 3;
|
|
|
|
|
final int firstMonth = lastMonth - 2;
|
|
|
|
|
|
|
|
|
|
this.monthRange = Range.closed(firstMonth, lastMonth);
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:20:54 +08:00
|
|
|
|
// StaticFactoryMethods
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据给定的月份值返回对应的季度
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-08-20 00:42:11 +08:00
|
|
|
|
* @param monthValue 月份值,取值范围为1到12
|
|
|
|
|
* @return 对应的季度
|
|
|
|
|
* @throws IllegalArgumentException 如果月份值不在有效范围内(1到12),将抛出异常
|
|
|
|
|
*/
|
2024-08-28 17:20:54 +08:00
|
|
|
|
@StaticFactoryMethod(Quarter.class)
|
2024-08-19 18:31:48 +08:00
|
|
|
|
public static Quarter fromMonth(int monthValue) {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
ChronoField.MONTH_OF_YEAR.checkValidValue(monthValue);
|
2024-08-19 18:31:48 +08:00
|
|
|
|
return of(computeQuarterValueInternal(monthValue));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据给定的月份返回对应的季度
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-08-20 00:42:11 +08:00
|
|
|
|
* @param month 月份
|
|
|
|
|
* @return 对应的季度
|
|
|
|
|
*/
|
2024-08-28 17:20:54 +08:00
|
|
|
|
@StaticFactoryMethod(Quarter.class)
|
2024-08-19 18:31:48 +08:00
|
|
|
|
public static Quarter fromMonth(Month month) {
|
|
|
|
|
final int monthValue = month.getValue();
|
|
|
|
|
return of(computeQuarterValueInternal(monthValue));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据指定的年份,获取一个新的 YearQuarter 实例
|
|
|
|
|
* 此方法允许在保持当前季度信息不变的情况下,更改年份
|
|
|
|
|
*
|
|
|
|
|
* @param year 指定的年份
|
|
|
|
|
* @return 返回一个新的 YearQuarter 实例,年份更新为指定的年份
|
|
|
|
|
*/
|
2024-08-19 18:31:48 +08:00
|
|
|
|
public final YearQuarter atYear(int year) {
|
|
|
|
|
return YearQuarter.of(year, this);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据给定的季度值返回对应的季度
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-08-20 00:42:11 +08:00
|
|
|
|
* @param value 季度值 (1/2/3/4)
|
|
|
|
|
* @return 对应的季度
|
|
|
|
|
* @throws IllegalArgumentException 如果季度值不在有效范围内(1到4),将抛出异常
|
|
|
|
|
*/
|
2024-08-28 17:20:54 +08:00
|
|
|
|
@StaticFactoryMethod(Quarter.class)
|
2024-08-19 18:31:48 +08:00
|
|
|
|
public static Quarter of(int value) {
|
2024-08-26 10:24:07 +08:00
|
|
|
|
if (value < 1 || value > 4) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid value for Quarter: " + value);
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
2024-08-26 10:24:07 +08:00
|
|
|
|
return ENUMS[value - 1];
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 17:20:54 +08:00
|
|
|
|
// 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
|
|
|
|
|
|
2024-08-19 18:31:48 +08:00
|
|
|
|
// Getters
|
|
|
|
|
|
|
|
|
|
public int getValue() {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public Month firstMonth() {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return Month.of(firstMonthValue());
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public int firstMonthValue() {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return this.monthRange.lowerEndpoint();
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public Month lastMonth() {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return Month.of(lastMonthValue());
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public int lastMonthValue() {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return this.monthRange.upperEndpoint();
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public MonthDay firstMonthDay() {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return MonthDay.of(firstMonth(), 1);
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public MonthDay lastMonthDay() {
|
2024-08-26 17:36:36 +08:00
|
|
|
|
// 季度的最后一个月不可能是 2 月
|
|
|
|
|
final Month month = lastMonth();
|
|
|
|
|
return MonthDay.of(month, month.maxLength());
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 10:24:07 +08:00
|
|
|
|
public int firstDayOfYear(boolean leapYear) {
|
2024-11-19 20:29:33 +08:00
|
|
|
|
return firstMonth().firstDayOfYear(leapYear);
|
2024-08-19 18:31:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Getters end
|
|
|
|
|
|
2024-08-20 00:42:11 +08:00
|
|
|
|
// Internal
|
|
|
|
|
|
2024-08-19 18:31:48 +08:00
|
|
|
|
/**
|
2024-08-20 00:42:11 +08:00
|
|
|
|
* 计算给定月份对应的季度值
|
2024-10-21 23:17:52 +08:00
|
|
|
|
*
|
2024-08-20 00:42:11 +08:00
|
|
|
|
* @param monthValue 月份值,取值范围为1到12
|
|
|
|
|
* @return 对应的季度值
|
2024-08-19 18:31:48 +08:00
|
|
|
|
*/
|
|
|
|
|
private static int computeQuarterValueInternal(int monthValue) {
|
|
|
|
|
return (monthValue - 1) / 3 + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|