添加 YearQuarter 和 Quarter 表示季度;修改 DateTimeTools。

This commit is contained in:
2024-08-19 18:31:48 +08:00
parent 71b3b193d1
commit c58e799b1e
4 changed files with 254 additions and 30 deletions

View File

@@ -17,7 +17,8 @@ import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Preconditions;
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
import xyz.zhouxy.plusone.commons.base.Quarter;
import xyz.zhouxy.plusone.commons.base.YearQuarter;
import xyz.zhouxy.plusone.commons.collection.MapWrapper;
public class DateTimeTools {
@@ -158,6 +159,10 @@ public class DateTimeTools {
return ZonedDateTime.ofInstant(dateTime.toInstant(), timeZone.toZoneId());
}
public static ZonedDateTime toZonedDateTime(Calendar calendar) {
return calendar.toInstant().atZone(calendar.getTimeZone().toZoneId());
}
public static ZonedDateTime toZonedDateTime(Calendar calendar, ZoneId zone) {
return calendar.toInstant().atZone(zone);
}
@@ -327,30 +332,20 @@ public class DateTimeTools {
// getQuarter
@SuppressWarnings("deprecation")
public static int getQuarter(Date date) {
return getQuarterInternal(date.getMonth() + 1);
public static YearQuarter getQuarter(Date date) {
return YearQuarter.of(date);
}
public static int getQuarter(Calendar date) {
return getQuarterInternal(date.get(Calendar.MONTH) + 1);
public static YearQuarter getQuarter(Calendar date) {
return YearQuarter.of(date);
}
public static int getQuarter(Month month) {
return getQuarterInternal(month.getValue());
public static Quarter getQuarter(Month month) {
return Quarter.fromMonth(month);
}
public static int getQuarter(LocalDate date) {
return getQuarterInternal(date.getMonthValue());
}
/**
* 获取季度
* @param monthValue 1~12
* @return 季度。1~4
*/
private static int getQuarterInternal(int monthValue) {
return (monthValue - 1) / 3 + 1;
public static YearQuarter getQuarter(LocalDate date) {
return YearQuarter.of(date);
}
private DateTimeTools() {