From cabfb74aa5b426f5d71019fdd4f95df6a608d638 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 26 Apr 2024 20:25:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=9B=9B?= =?UTF-8?q?=E7=A7=8D=E5=86=9C=E5=8E=86=E6=97=A5=E6=9C=9F=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E5=87=86=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/date/chinese/ChineseDate.java | 210 ++++++------------ .../core/date/chinese/ChineseDateFormat.java | 69 ++++++ .../hutool/core/date/chinese/GanZhi.java | 10 +- .../core/date/chinese/LunarFestival.java | 8 +- .../hutool/core/date/ChineseDateTest.java | 65 +++--- .../dromara/hutool/core/date/GanzhiTest.java | 17 +- .../core/date/chinese/IssueI97WU6Test.java | 2 +- 7 files changed, 190 insertions(+), 191 deletions(-) create mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDateFormat.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDate.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDate.java index d1122b025..4561d2a69 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDate.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDate.java @@ -12,13 +12,9 @@ package org.dromara.hutool.core.date.chinese; -import org.dromara.hutool.core.math.ChineseNumberFormatter; -import org.dromara.hutool.core.date.CalendarUtil; -import org.dromara.hutool.core.date.DateTime; -import org.dromara.hutool.core.date.DateUtil; -import org.dromara.hutool.core.date.TimeUtil; -import org.dromara.hutool.core.date.Zodiac; +import org.dromara.hutool.core.date.*; import org.dromara.hutool.core.lang.Assert; +import org.dromara.hutool.core.math.ChineseNumberFormatter; import org.dromara.hutool.core.text.StrUtil; import java.time.LocalDate; @@ -34,6 +30,8 @@ import java.util.Objects; *
  • 通过公历日期构造获取对应农历
  • *
  • 通过农历日期直接构造
  • * + *
    + * 规范参考:GB/T 33661-2017 * * @author zjw, looly * @since 5.1.1 @@ -150,7 +148,7 @@ public class ChineseDate { * @since 5.7.18 */ public ChineseDate(final int chineseYear, final int chineseMonth, final int chineseDay, boolean isLeapMonth) { - if(chineseMonth != LunarInfo.leapMonth(chineseYear)){ + if (chineseMonth != LunarInfo.leapMonth(chineseYear)) { // issue#I5YB1A,用户传入的月份可能非闰月,此时此参数无效。 isLeapMonth = false; } @@ -268,7 +266,7 @@ public class ChineseDate { */ public String getChineseMonth(final boolean isTraditional) { return ChineseMonth.getChineseMonthName(isLeapMonth(), - isLeapMonth() ? this.month - 1 : this.month, isTraditional); + isLeapMonth() ? this.month - 1 : this.month, isTraditional); } /** @@ -365,92 +363,6 @@ public class ChineseDate { return GanZhi.getGanzhiOfYear(this.year); } - /** - * 干支纪年信息 - * - * @return 获得天干地支的年月日信息 - */ - public String getCyclicalYMD() { - if (gyear >= LunarInfo.BASE_YEAR && gmonthBase1 > 0 && gday > 0) { - return cyclicalm(gyear, gmonthBase1, gday); - } - return null; - } - - /** - * 农历标准化输出格式枚举 - */ - public enum ChineseDateFormat{ - - /** - * 干支纪年 数序纪月 数序纪日 - */ - GSS("干支纪年 数序纪月 数序纪日"), - /** - * 生肖纪年 数序纪月 数序纪日 - */ - XSS("生肖纪年 数序纪月 数序纪日"), - /** - * 干支纪年 数序纪月 干支纪日 - */ - GSG("干支纪年 数序纪月 干支纪日"), - /** - * 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日 - */ - Mix("农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日"); - - /** - * 农历标准化输出格式信息 - */ - private final String info; - - /** - * 构造 - * - * @param info 输出格式信息 - */ - ChineseDateFormat(final String info) { - this.info = info; - } - - /** - * 获取农历日期输出格式相关描述 - * - * @return 输出格式信息 - */ - public String getName() { - return this.info; - } - } - - /** - *获取标准化农历日期,默认Mix - * - * @return 获取的标准化农历日期 - */ - public String getNormalizedDate() { - return getNormalizedDate(ChineseDateFormat.Mix); - } - - /** - * 获取标准化农历日期 - * 支持格式 - *
      GSS 干支纪年 数序纪月 数序纪日 - *
    1. XSS 生肖纪年 数序纪月 数序纪日
    2. - *
    3. GSG 干支纪年 数序纪月 干支纪日
    4. - *
    5. Mix 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日
    6. - *
    - * @param format 选择输出的标准格式 - * @return 获取的标准化农历日期 - */ - public String getNormalizedDate(final ChineseDateFormat format) { - if (gyear >= LunarInfo.BASE_YEAR && gmonthBase1 > 0 && gday > 0) { - return normalized(gyear, gmonthBase1, gday, format); - } - return null; - } - /** * 获得节气 * @@ -470,12 +382,70 @@ public class ChineseDate { */ public String toStringNormal() { return String.format("%04d-%02d-%02d", this.year, - isLeapMonth() ? this.month - 1 : this.month, this.day); + isLeapMonth() ? this.month - 1 : this.month, this.day); } @Override public String toString() { - return String.format("%s%s年 %s%s", getCyclical(), getChineseZodiac(), getChineseMonthName(), getChineseDay()); + return toString(ChineseDateFormat.GXSS); + } + + /** + * 获取标准化农历日期 + * 支持格式 + *
      + *
    1. {@link ChineseDateFormat#GSS} 干支纪年 数序纪月 数序纪日
    2. + *
    3. {@link ChineseDateFormat#XSS} 生肖纪年 数序纪月 数序纪日
    4. + *
    5. {@link ChineseDateFormat#GXSS} 干支生肖纪年 数序纪月(传统表示) 数序纪日日
    6. + *
    7. {@link ChineseDateFormat#GSG} 干支纪年 数序纪月 干支纪日
    8. + *
    9. {@link ChineseDateFormat#GGG} 干支纪年 干支纪月 干支纪日
    10. + *
    11. {@link ChineseDateFormat#MIX} 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日
    12. + *
    + * + * @param format 选择输出的标准格式 + * @return 获取的标准化农历日期 + * @since 6.0.0 + */ + public String toString(ChineseDateFormat format) { + if(null == format){ + format = ChineseDateFormat.MIX; + } + + final int year = this.year; + CharSequence dateTemplate = "农历{}年{}{}"; + String normalizedYear = GanZhi.getGanzhiOfYear(year); + String normalizedMonth = getChineseMonth(); + String normalizedDay = getChineseDay(); + switch (format){ + case GXSS: + dateTemplate = "农历{}" + getChineseZodiac() + "年{}{}"; + normalizedMonth = getChineseMonthName(); + break; + case XSS : + normalizedYear = getChineseZodiac(); + break; + case GSG: + dateTemplate = "农历{}年{}{}日"; + normalizedDay = GanZhi.getGanzhiOfDay(this.gyear, this.gmonthBase1, this.gday); + break; + case GGG: + dateTemplate = "农历{}年{}月{}日"; + normalizedMonth = GanZhi.getGanzhiOfMonth(this.gyear, this.gmonthBase1, this.gday); + normalizedDay = GanZhi.getGanzhiOfDay(this.gyear, this.gmonthBase1, this.gday); + break; + case MIX: + //根据选择的格式返回不同标准化日期输出,默认为Mix + dateTemplate = "公元"+ this.year +"年农历{}年{}{}"; + case GSS: + break; + default: + throw new IllegalArgumentException("Unsupported format: " + format); + } + + return StrUtil.format(dateTemplate, + normalizedYear, + normalizedMonth, + normalizedDay); } @Override @@ -507,49 +477,9 @@ public class ChineseDate { */ private String cyclicalm(final int year, final int month, final int day) { return StrUtil.format("{}年{}月{}日", - GanZhi.getGanzhiOfYear(this.year), - GanZhi.getGanzhiOfMonth(year, month, day), - GanZhi.getGanzhiOfDay(year, month, day)); - } - - /** - * 获取不同格式的标准化农历日期输出 - * - * @param year 公历年 - * @param month 公历月 - * @param day 公历日 - * @param format 农历输出格式 - * @return 标准化农历日期输出 - */ - private String normalized(final int year, final int month, final int day, final ChineseDateFormat format) { - //根据选择的格式返回不同标准化日期输出,默认为Mix - String normalizedYear = ""; - String normalizedMonth = getChineseMonth(); - String normalizedDay = ""; - CharSequence dateTemplate = "农历{}年{}{}"; - - switch (format){ - case Mix: - dateTemplate = "公元"+ year +"年农历{}年{}{}"; - case GSS: - normalizedYear = GanZhi.getGanzhiOfYear(this.year); - normalizedDay = getChineseDay(); - break; - case XSS : - normalizedYear = getChineseZodiac(); - normalizedDay = getChineseDay(); - break; - case GSG: - dateTemplate = "农历{}年{}{}日"; - normalizedYear = GanZhi.getGanzhiOfYear(this.year); - normalizedDay = GanZhi.getGanzhiOfDay(year, month, day); - break; - } - - return StrUtil.format(dateTemplate, - normalizedYear, - normalizedMonth, - normalizedDay); + GanZhi.getGanzhiOfYear(this.year), + GanZhi.getGanzhiOfMonth(year, month, day), + GanZhi.getGanzhiOfDay(year, month, day)); } /** @@ -564,7 +494,7 @@ public class ChineseDate { private DateTime lunar2solar(final int chineseYear, final int chineseMonth, final int chineseDay, final boolean isLeapMonth) { //超出了最大极限值 if ((chineseYear == 2100 && chineseMonth == 12 && chineseDay > 1) || - (chineseYear == LunarInfo.BASE_YEAR && chineseMonth == 1 && chineseDay < 31)) { + (chineseYear == LunarInfo.BASE_YEAR && chineseMonth == 1 && chineseDay < 31)) { return null; } final int day = LunarInfo.monthDays(chineseYear, chineseMonth); diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDateFormat.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDateFormat.java new file mode 100644 index 000000000..b021e4ff9 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseDateFormat.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.core.date.chinese; + +/** + * 农历标准化输出格式枚举 + * + * @author eventiming + */ +public enum ChineseDateFormat { + + /** + * 干支纪年 数序纪月 数序纪日 + */ + GSS("干支纪年 数序纪月 数序纪日"), + /** + * 生肖纪年 数序纪月 数序纪日 + */ + XSS("生肖纪年 数序纪月 数序纪日"), + /** + * 干支生肖纪年 数序纪月(传统表示) 数序纪日 + */ + GXSS("干支生肖纪年 数序纪月(传统表示) 数序纪日"), + /** + * 干支纪年 数序纪月 干支纪日 + */ + GSG("干支纪年 数序纪月 干支纪日"), + /** + * 干支纪年 干支纪月 干支纪日 + */ + GGG("干支纪年 干支纪月 干支纪日"), + /** + * 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日 + */ + MIX("农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日"); + + /** + * 农历标准化输出格式信息 + */ + private final String info; + + /** + * 构造 + * + * @param info 输出格式信息 + */ + ChineseDateFormat(final String info) { + this.info = info; + } + + /** + * 获取农历日期输出格式相关描述 + * + * @return 输出格式信息 + */ + public String getName() { + return this.info; + } +} diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/GanZhi.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/GanZhi.java index 0f4c9c7d6..944a4763e 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/GanZhi.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/GanZhi.java @@ -78,15 +78,15 @@ public class GanZhi { /** * 获取干支日 * - * @param year 公历年 - * @param month 公历月,从1开始 - * @param day 公历日 + * @param year 公历年 + * @param monthBase1 公历月,从1开始 + * @param day 公历日 * @return 干支 * @since 5.4.7 */ - public static String getGanzhiOfDay(final int year, final int month, final int day) { + public static String getGanzhiOfDay(final int year, final int monthBase1, final int day) { // 与1970-01-01相差天数,不包括当天 - final long days = LocalDate.of(year, month, day).toEpochDay() - 1; + final long days = LocalDate.of(year, monthBase1, day).toEpochDay() - 1; //1899-12-21是农历1899年腊月甲子日 41:相差1900-01-31有41天 return cyclicalm((int) (days - LunarInfo.BASE_DAY + 41)); } diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/LunarFestival.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/LunarFestival.java index 0b5234793..0add41332 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/LunarFestival.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/LunarFestival.java @@ -27,13 +27,13 @@ import java.util.Map; */ public class LunarFestival { - //农历节日 *表示放假日 + //农历节日 // 来自:https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E4%BC%A0%E7%BB%9F%E8%8A%82%E6%97%A5/396100 private static final TableMap, String> L_FTV = new TableMap<>(16); static { // 节日 - L_FTV.put(MapUtil.entry(1, 1), "春节*"); + L_FTV.put(MapUtil.entry(1, 1), "春节"); L_FTV.put(MapUtil.entry(1, 2), "犬日"); L_FTV.put(MapUtil.entry(1, 3), "猪日"); L_FTV.put(MapUtil.entry(1, 4), "羊日"); @@ -63,7 +63,7 @@ public class LunarFestival { L_FTV.put(MapUtil.entry(4, 8), "佛诞节"); // 五月 - L_FTV.put(MapUtil.entry(5, 5), "端午节 端阳节*"); + L_FTV.put(MapUtil.entry(5, 5), "端午节 端阳节"); // 六月 L_FTV.put(MapUtil.entry(6, 6), "晒衣节 姑姑节"); @@ -78,7 +78,7 @@ public class LunarFestival { L_FTV.put(MapUtil.entry(7, 30), "地藏节"); // 八月 - L_FTV.put(MapUtil.entry(8, 15), "中秋节*"); + L_FTV.put(MapUtil.entry(8, 15), "中秋节"); // 九月 L_FTV.put(MapUtil.entry(9, 9), "重阳节"); diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/ChineseDateTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/ChineseDateTest.java index 2cf0e84bf..d74d878bc 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/date/ChineseDateTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/ChineseDateTest.java @@ -13,6 +13,7 @@ package org.dromara.hutool.core.date; import org.dromara.hutool.core.date.chinese.ChineseDate; +import org.dromara.hutool.core.date.chinese.ChineseDateFormat; import org.dromara.hutool.core.text.StrUtil; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,7 +31,7 @@ public class ChineseDateTest { Assertions.assertEquals(2020, date.getChineseYear()); Assertions.assertEquals(1, date.getMonth()); - Assertions.assertEquals("一月", date.getChineseMonth()); + Assertions.assertEquals("正月", date.getChineseMonth()); Assertions.assertEquals("正月", date.getChineseMonthName()); @@ -40,12 +41,12 @@ public class ChineseDateTest { Assertions.assertEquals("庚子", date.getCyclical()); Assertions.assertEquals("鼠", date.getChineseZodiac()); Assertions.assertEquals("春节", date.getFestivals()); - Assertions.assertEquals("庚子鼠年 正月初一", date.toString()); + Assertions.assertEquals("农历庚子鼠年正月初一", date.toString()); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-01-14"))); - Assertions.assertEquals("己亥猪年 腊月二十", date.toString()); + Assertions.assertEquals("农历己亥猪年腊月二十", date.toString()); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-01-24"))); - Assertions.assertEquals("己亥猪年 腊月三十", date.toString()); + Assertions.assertEquals("农历己亥猪年腊月三十", date.toString()); Assertions.assertEquals("2019-12-30", date.toStringNormal()); } @@ -59,10 +60,10 @@ public class ChineseDateTest { @Test public void parseTest(){ ChineseDate date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1996-07-14"))); - Assertions.assertEquals("丙子鼠年 五月廿九", date.toString()); + Assertions.assertEquals("农历丙子鼠年五月廿九", date.toString()); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1996-07-15"))); - Assertions.assertEquals("丙子鼠年 五月三十", date.toString()); + Assertions.assertEquals("农历丙子鼠年五月三十", date.toString()); } @Test @@ -92,23 +93,23 @@ public class ChineseDateTest { // 修复这两个日期不正确的问题 // 问题出在计算与1900-01-31相差天数的问题上了,相差天数非整天 ChineseDate date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1991-09-14"))); - Assertions.assertEquals("辛未羊年 八月初七", date.toString()); + Assertions.assertEquals("农历辛未羊年八月初七", date.toString()); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1991-09-15"))); - Assertions.assertEquals("辛未羊年 八月初八", date.toString()); + Assertions.assertEquals("农历辛未羊年八月初八", date.toString()); } @Test public void dateTest2(){ //noinspection ConstantConditions final ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19")); - Assertions.assertEquals("庚子鼠年 九月初三", date.toString()); + Assertions.assertEquals("农历庚子鼠年九月初三", date.toString()); } @Test public void dateTest2_2(){ //noinspection ConstantConditions final ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20")); - Assertions.assertEquals("庚子鼠年 五月三十", date.toString()); + Assertions.assertEquals("农历庚子鼠年五月三十", date.toString()); } @Test @@ -116,7 +117,7 @@ public class ChineseDateTest { // 初一,offset为0测试 //noinspection ConstantConditions final ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22")); - Assertions.assertEquals("己未羊年 闰二月初一", date.toString()); + Assertions.assertEquals("农历己未羊年闰二月初一", date.toString()); } @Test @@ -126,8 +127,8 @@ public class ChineseDateTest { //noinspection ConstantConditions final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27")); - Assertions.assertEquals("戊申猴年 五月初五", c1.toString()); - Assertions.assertEquals("戊申猴年 闰五月初五", c2.toString()); + Assertions.assertEquals("农历戊申猴年五月初五", c1.toString()); + Assertions.assertEquals("农历戊申猴年闰五月初五", c2.toString()); } @Test @@ -135,7 +136,7 @@ public class ChineseDateTest { //https://github.com/dromara/hutool/issues/2112 final ChineseDate springFestival = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2022-02-01"))); final String chineseMonth = springFestival.getChineseMonth(); - Assertions.assertEquals("一月", chineseMonth); + Assertions.assertEquals("正月", chineseMonth); } @Test @@ -144,17 +145,17 @@ public class ChineseDateTest { Date date = DateUtil.parse("1970-01-01"); //noinspection ConstantConditions ChineseDate chineseDate = new ChineseDate(date); - Assertions.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString()); + Assertions.assertEquals("农历己酉鸡年冬月廿四", chineseDate.toString()); date = DateUtil.parse("1970-01-02"); //noinspection ConstantConditions chineseDate = new ChineseDate(date); - Assertions.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString()); + Assertions.assertEquals("农历己酉鸡年冬月廿五", chineseDate.toString()); date = DateUtil.parse("1970-01-03"); //noinspection ConstantConditions chineseDate = new ChineseDate(date); - Assertions.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString()); + Assertions.assertEquals("农历己酉鸡年冬月廿六", chineseDate.toString()); } @Test @@ -163,7 +164,7 @@ public class ChineseDateTest { final Date date = DateUtil.parse("1900-01-31"); //noinspection ConstantConditions final ChineseDate chineseDate = new ChineseDate(date); - Assertions.assertEquals("庚子鼠年 正月初一", chineseDate.toString()); + Assertions.assertEquals("农历庚子鼠年正月初一", chineseDate.toString()); } @Test @@ -200,24 +201,22 @@ public class ChineseDateTest { Assertions.assertEquals(chineseDate2, chineseDate3); Assertions.assertNotEquals(chineseDate2, chineseDate4); } + @Test public void getNormalizedDateTest(){ final Date date = DateUtil.parse("2024-4-24"); + final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(date)); + + Assertions.assertEquals("农历甲辰年三月十六", chineseDate.toString(ChineseDateFormat.GSS)); + Assertions.assertEquals("农历龙年三月十六", chineseDate.toString(ChineseDateFormat.XSS)); + Assertions.assertEquals("农历甲辰年三月戊午日", chineseDate.toString(ChineseDateFormat.GSG)); + Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.toString(ChineseDateFormat.MIX)); + final Date date2 = DateUtil.parse("2024-4-30"); - - final ChineseDate chineseDate = new ChineseDate(date); - final ChineseDate chineseDate2 = new ChineseDate(date2); - - Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate()); - Assertions.assertEquals("农历甲辰年三月十六", chineseDate.getNormalizedDate(ChineseDate.ChineseDateFormat.GSS)); - Assertions.assertEquals("农历龙年三月十六", chineseDate.getNormalizedDate(ChineseDate.ChineseDateFormat.XSS)); - Assertions.assertEquals("农历甲辰年三月戊午日", chineseDate.getNormalizedDate(ChineseDate.ChineseDateFormat.GSG)); - Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate(ChineseDate.ChineseDateFormat.Mix)); - - Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.getNormalizedDate()); - Assertions.assertEquals("农历甲辰年三月廿二", chineseDate2.getNormalizedDate(ChineseDate.ChineseDateFormat.GSS)); - Assertions.assertEquals("农历龙年三月廿二", chineseDate2.getNormalizedDate(ChineseDate.ChineseDateFormat.XSS)); - Assertions.assertEquals("农历甲辰年三月甲子日", chineseDate2.getNormalizedDate(ChineseDate.ChineseDateFormat.GSG)); - Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.getNormalizedDate(ChineseDate.ChineseDateFormat.Mix)); + final ChineseDate chineseDate2 = new ChineseDate(Objects.requireNonNull(date2)); + Assertions.assertEquals("农历甲辰年三月廿二", chineseDate2.toString(ChineseDateFormat.GSS)); + Assertions.assertEquals("农历龙年三月廿二", chineseDate2.toString(ChineseDateFormat.XSS)); + Assertions.assertEquals("农历甲辰年三月甲子日", chineseDate2.toString(ChineseDateFormat.GSG)); + Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.toString(ChineseDateFormat.MIX)); } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/GanzhiTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/GanzhiTest.java index d9fea10a2..ac8bdf884 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/date/GanzhiTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/GanzhiTest.java @@ -13,6 +13,7 @@ package org.dromara.hutool.core.date; import org.dromara.hutool.core.date.chinese.ChineseDate; +import org.dromara.hutool.core.date.chinese.ChineseDateFormat; import org.dromara.hutool.core.date.chinese.GanZhi; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,31 +31,31 @@ public class GanzhiTest { public void getCyclicalYMDTest(){ //通过公历构建 final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1993-01-06"))); - final String cyclicalYMD = chineseDate.getCyclicalYMD(); - Assertions.assertEquals("壬申年癸丑月丁亥日",cyclicalYMD); + final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG); + Assertions.assertEquals("农历壬申年癸丑月丁亥日",cyclicalYMD); } @Test public void getCyclicalYMDTest2(){ //通过农历构建 final ChineseDate chineseDate = new ChineseDate(1992,12,14); - final String cyclicalYMD = chineseDate.getCyclicalYMD(); - Assertions.assertEquals("壬申年癸丑月丁亥日",cyclicalYMD); + final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG); + Assertions.assertEquals("农历壬申年癸丑月丁亥日",cyclicalYMD); } @Test public void getCyclicalYMDTest3(){ //通过公历构建 final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-08-28"))); - final String cyclicalYMD = chineseDate.getCyclicalYMD(); - Assertions.assertEquals("庚子年甲申月癸卯日",cyclicalYMD); + final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG); + Assertions.assertEquals("农历庚子年甲申月癸卯日",cyclicalYMD); } @Test public void getCyclicalYMDTest4(){ //通过公历构建 final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1905-08-28"))); - final String cyclicalYMD = chineseDate.getCyclicalYMD(); - Assertions.assertEquals("乙巳年甲申月己亥日",cyclicalYMD); + final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG); + Assertions.assertEquals("农历乙巳年甲申月己亥日",cyclicalYMD); } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/IssueI97WU6Test.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/IssueI97WU6Test.java index 3c1c5fc65..a04728a70 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/IssueI97WU6Test.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/chinese/IssueI97WU6Test.java @@ -8,7 +8,7 @@ public class IssueI97WU6Test { public void getTermTest() { // 润十月没有三十,十月有三十 final ChineseDate chineseDate = new ChineseDate(1984, 10, 30, false); - Assertions.assertEquals("甲子鼠年 寒月三十", chineseDate.toString()); + Assertions.assertEquals("农历甲子鼠年寒月三十", chineseDate.toString()); Assertions.assertEquals("小雪", chineseDate.getTerm()); } }