添加农历标准化输出

This commit is contained in:
Monica
2024-04-24 17:44:21 +08:00
parent 1be871154f
commit 57aae6e373
3 changed files with 86 additions and 1 deletions

View File

@@ -377,6 +377,33 @@ public class ChineseDate {
return null;
}
/**
*获取标准化农历日期,默认Mix
*
* @return 获取的标准化农历日期
*/
public String getNormalizedDate() {
return getNormalizedDate("Mix");
}
/**
* 获取标准化农历日期
* 支持格式
*<ol
* <li>GSS 干支纪年 数序纪月 数序纪日</li>
* <li>XSS 生肖纪年 数序纪月 数序纪日</li>
* <li>GSG 干支纪年 数序纪月 干支纪日</li>
* <li>Mix 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日</li>
*</ol>
* @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);
}
/**
* 通过农历年月日信息 返回公历信息 提供给构造函数
*

View File

@@ -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 = {"", "", "", "", "", "", "", "", "", "", "", ""};
/**

View File

@@ -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"));
}
}