From 43e01e5595407db6af2c67a3a666807276a10d7c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 26 Aug 2024 10:41:26 +0800 Subject: [PATCH] =?UTF-8?q?YearQuarter=20=E5=AE=9E=E7=8E=B0=20Comparable?= =?UTF-8?q?=20=E5=92=8C=20Serializable=20=E6=8E=A5=E5=8F=A3=EF=BC=9B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20isBefore=20=E5=92=8C=20isAfter=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/base/YearQuarter.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java b/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java index 12149ea..e0b0bf5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/YearQuarter.java @@ -1,5 +1,6 @@ package xyz.zhouxy.plusone.commons.base; +import java.io.Serializable; import java.time.LocalDate; import java.time.Month; import java.time.YearMonth; @@ -16,7 +17,8 @@ import com.google.common.base.Preconditions; * * @author zhouxy */ -public final class YearQuarter { +public final class YearQuarter implements Comparable, Serializable { + private static final long serialVersionUID = 3804145964419489753L; /** 年份 */ private final int year; @@ -153,6 +155,25 @@ public final class YearQuarter { return year == other.year && quarter == other.quarter; } + // compareTo + + @Override + public int compareTo(YearQuarter other) { + int cmp = (this.year - other.year); + if (cmp == 0) { + cmp = (this.quarter.compareTo(other.quarter)); + } + return cmp; + } + + public boolean isBefore(YearQuarter other) { + return this.compareTo(other) < 0; + } + + public boolean isAfter(YearQuarter other) { + return this.compareTo(other) > 0; + } + // toString /**