From b86bb6c15b35a2224bcb5f329d53286651383b87 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Jun 2024 16:07:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=AD=A3=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/DateTimeTools.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java index 8dd3cbf..f3032b5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java @@ -4,6 +4,7 @@ import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -325,6 +326,34 @@ public class DateTimeTools { return org.joda.time.DateTimeZone.forID(zone.getId()); } + // getQuarter + + @SuppressWarnings("deprecation") + public static int getQuarter(Date date) { + return getQuarterInternal(date.getMonth() + 1); + } + + public static int getQuarter(Calendar date) { + return getQuarterInternal(date.get(Calendar.MONTH) + 1); + } + + public static int getQuarter(Month month) { + return getQuarterInternal(month.getValue()); + } + + 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; + } + private DateTimeTools() { throw new IllegalStateException("Utility class"); }