From 27913e9c6bb3f5581cca1a9d74e8679332a52c48 Mon Sep 17 00:00:00 2001 From: Monica Date: Tue, 23 Apr 2024 23:54:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=9B=BD=E6=A0=87GB/T=20?= =?UTF-8?q?33661-2017=20=E5=AF=B9=E6=9C=88=E4=BB=BD=E5=92=8C=E4=BC=A0?= =?UTF-8?q?=E7=BB=9F=E8=8A=82=E6=97=A5=E8=BF=9B=E8=A1=8C=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dromara/hutool/core/date/chinese/ChineseMonth.java | 2 +- .../hutool/core/date/chinese/LunarFestival.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java index fce2b9eba..439cb22a5 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java @@ -20,7 +20,7 @@ package org.dromara.hutool.core.date.chinese; */ public class ChineseMonth { - private static final String[] MONTH_NAME = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; + private static final String[] MONTH_NAME = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; private static final String[] MONTH_NAME_TRADITIONAL = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "寒", "冬", "腊"}; /** 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 25478992e..5f0f25086 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 @@ -32,7 +32,7 @@ public class LunarFestival { 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), "羊日"); @@ -49,7 +49,7 @@ public class LunarFestival { // 二月 L_FTV.put(MapUtil.entry(2, 1), "中和节 太阳生日"); - L_FTV.put(MapUtil.entry(2, 2), "龙抬头"); + L_FTV.put(MapUtil.entry(2, 2), "龙头节 龙抬头"); L_FTV.put(MapUtil.entry(2, 12), "花朝节"); L_FTV.put(MapUtil.entry(2, 19), "观世音圣诞"); @@ -62,7 +62,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), "晒衣节 姑姑节"); @@ -70,14 +70,14 @@ public class LunarFestival { L_FTV.put(MapUtil.entry(6, 24), "彝族火把节"); // 七月 - L_FTV.put(MapUtil.entry(7, 7), "七夕"); + L_FTV.put(MapUtil.entry(7, 7), "七夕节"); L_FTV.put(MapUtil.entry(7, 14), "鬼节(南方)"); L_FTV.put(MapUtil.entry(7, 15), "中元节"); L_FTV.put(MapUtil.entry(7, 15), "盂兰盆节 中元节"); 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), "重阳节"); From 57aae6e37301ff2e9acb960ef59cd2e57d6bbe20 Mon Sep 17 00:00:00 2001 From: Monica Date: Wed, 24 Apr 2024 17:44:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=9C=E5=8E=86?= =?UTF-8?q?=E6=A0=87=E5=87=86=E5=8C=96=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 | 65 +++++++++++++++++++ .../core/date/chinese/ChineseMonth.java | 2 +- .../hutool/core/date/ChineseDateTest.java | 20 ++++++ 3 files changed, 86 insertions(+), 1 deletion(-) 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 6f23046fd..5bd8a746e 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 @@ -377,6 +377,33 @@ public class ChineseDate { return null; } + /** + *获取标准化农历日期,默认Mix + * + * @return 获取的标准化农历日期 + */ + public String getNormalizedDate() { + return getNormalizedDate("Mix"); + } + + /** + * 获取标准化农历日期 + * 支持格式 + *
    GSS 干支纪年 数序纪月 数序纪日 + *
  1. XSS 生肖纪年 数序纪月 数序纪日
  2. + *
  3. GSG 干支纪年 数序纪月 干支纪日
  4. + *
  5. Mix 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日
  6. + *
+ * @param format 选择输出的标准格式 + * @return 获取的标准化农历日期 + */ + public String getNormalizedDate(final String format) { + if (gyear >= LunarInfo.BASE_YEAR && gmonthBase1 > 0 && gday > 0) { + return normalized(gyear, gmonthBase1, gday, format); + } + return null; + } /** * 获得节气 @@ -439,6 +466,44 @@ public class ChineseDate { 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 String 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); + } + /** * 通过农历年月日信息 返回公历信息 提供给构造函数 * diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java index fce2b9eba..439cb22a5 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/chinese/ChineseMonth.java @@ -20,7 +20,7 @@ package org.dromara.hutool.core.date.chinese; */ public class ChineseMonth { - private static final String[] MONTH_NAME = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; + private static final String[] MONTH_NAME = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"}; private static final String[] MONTH_NAME_TRADITIONAL = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "寒", "冬", "腊"}; /** 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 29b1c203f..abe1de341 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 @@ -200,4 +200,24 @@ public class ChineseDateTest { Assertions.assertEquals(chineseDate2, chineseDate3); Assertions.assertNotEquals(chineseDate2, chineseDate4); } + @Test + public void getNormalizedDateTest(){ + final Date date = DateUtil.parse("2024-4-24"); + 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("GSS")); + Assertions.assertEquals("农历龙年三月十六", chineseDate.getNormalizedDate("XSS")); + Assertions.assertEquals("农历甲辰年三月戊午日", chineseDate.getNormalizedDate("GSG")); + Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate("Mix")); + + Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.getNormalizedDate()); + Assertions.assertEquals("农历甲辰年三月廿二", chineseDate2.getNormalizedDate("GSS")); + Assertions.assertEquals("农历龙年三月廿二", chineseDate2.getNormalizedDate("XSS")); + Assertions.assertEquals("农历甲辰年三月甲子日", chineseDate2.getNormalizedDate("GSG")); + Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.getNormalizedDate("Mix")); + } } From d7e533be0ec83fc92a583998507a01dc4dc7b18a Mon Sep 17 00:00:00 2001 From: Monica Date: Thu, 25 Apr 2024 12:09:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=86=E5=86=9C=E5=8E=86=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=A0=BC=E5=BC=8F=E8=BD=AC=E4=B8=BA=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/date/chinese/ChineseDate.java | 62 ++++++++++++++++--- .../hutool/core/date/ChineseDateTest.java | 16 ++--- 2 files changed, 63 insertions(+), 15 deletions(-) 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 5bd8a746e..d1122b025 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 @@ -377,13 +377,59 @@ public class ChineseDate { 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("Mix"); + return getNormalizedDate(ChineseDateFormat.Mix); } /** @@ -398,7 +444,7 @@ public class ChineseDate { * @param format 选择输出的标准格式 * @return 获取的标准化农历日期 */ - public String getNormalizedDate(final String format) { + public String getNormalizedDate(final ChineseDateFormat format) { if (gyear >= LunarInfo.BASE_YEAR && gmonthBase1 > 0 && gday > 0) { return normalized(gyear, gmonthBase1, gday, format); } @@ -475,29 +521,31 @@ public class ChineseDate { * @param format 农历输出格式 * @return 标准化农历日期输出 */ - private String normalized(final int year, final int month, final int day, final String format) { + 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": + case Mix: dateTemplate = "公元"+ year +"年农历{}年{}{}"; - case "GSS": + case GSS: normalizedYear = GanZhi.getGanzhiOfYear(this.year); normalizedDay = getChineseDay(); break; - case "XSS" : + case XSS : normalizedYear = getChineseZodiac(); normalizedDay = getChineseDay(); break; - case "GSG": + case GSG: dateTemplate = "农历{}年{}{}日"; normalizedYear = GanZhi.getGanzhiOfYear(this.year); normalizedDay = GanZhi.getGanzhiOfDay(year, month, day); break; } + return StrUtil.format(dateTemplate, normalizedYear, normalizedMonth, 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 abe1de341..2cf0e84bf 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 @@ -209,15 +209,15 @@ public class ChineseDateTest { final ChineseDate chineseDate2 = new ChineseDate(date2); Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate()); - Assertions.assertEquals("农历甲辰年三月十六", chineseDate.getNormalizedDate("GSS")); - Assertions.assertEquals("农历龙年三月十六", chineseDate.getNormalizedDate("XSS")); - Assertions.assertEquals("农历甲辰年三月戊午日", chineseDate.getNormalizedDate("GSG")); - Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate("Mix")); + 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("GSS")); - Assertions.assertEquals("农历龙年三月廿二", chineseDate2.getNormalizedDate("XSS")); - Assertions.assertEquals("农历甲辰年三月甲子日", chineseDate2.getNormalizedDate("GSG")); - Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.getNormalizedDate("Mix")); + 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)); } }