!1214 [新特性] 新增加了四种农历日期的标准输出

Merge pull request !1214 from eventiming/v6-dev
This commit is contained in:
Looly 2024-04-26 07:35:23 +00:00 committed by Gitee
commit ccc3567841
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 138 additions and 5 deletions

View File

@ -377,6 +377,79 @@ 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(ChineseDateFormat.Mix);
}
/**
* 获取标准化农历日期
* 支持格式
*<ol
* <li>GSS 干支纪年 数序纪月 数序纪日</li>
* <li>XSS 生肖纪年 数序纪月 数序纪日</li>
* <li>GSG 干支纪年 数序纪月 干支纪日</li>
* <li>Mix 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日</li>
*</ol>
* @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;
}
/**
* 获得节气
@ -439,6 +512,46 @@ 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 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);
}
/**
* 通过农历年月日信息 返回公历信息 提供给构造函数
*

View File

@ -33,7 +33,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), "羊日");
@ -50,7 +50,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), "观世音圣诞");
@ -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), "晒衣节 姑姑节");
@ -71,14 +71,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), "重阳节");

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(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));
}
}