新增加了四种农历日期的标准输出

This commit is contained in:
Looly
2024-04-26 20:25:18 +08:00
parent ccc3567841
commit cabfb74aa5
7 changed files with 190 additions and 191 deletions

View File

@@ -12,13 +12,9 @@
package org.dromara.hutool.core.date.chinese; package org.dromara.hutool.core.date.chinese;
import org.dromara.hutool.core.math.ChineseNumberFormatter; import org.dromara.hutool.core.date.*;
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.lang.Assert; import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.math.ChineseNumberFormatter;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import java.time.LocalDate; import java.time.LocalDate;
@@ -34,6 +30,8 @@ import java.util.Objects;
* <li>通过公历日期构造获取对应农历</li> * <li>通过公历日期构造获取对应农历</li>
* <li>通过农历日期直接构造</li> * <li>通过农历日期直接构造</li>
* </ul> * </ul>
* <br>
* 规范参考:<a href="https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=E107EA4DE9725EDF819F33C60A44B296">GB/T 33661-2017</a>
* *
* @author zjw, looly * @author zjw, looly
* @since 5.1.1 * @since 5.1.1
@@ -150,7 +148,7 @@ public class ChineseDate {
* @since 5.7.18 * @since 5.7.18
*/ */
public ChineseDate(final int chineseYear, final int chineseMonth, final int chineseDay, boolean isLeapMonth) { 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用户传入的月份可能非闰月此时此参数无效。 // issue#I5YB1A用户传入的月份可能非闰月此时此参数无效。
isLeapMonth = false; isLeapMonth = false;
} }
@@ -268,7 +266,7 @@ public class ChineseDate {
*/ */
public String getChineseMonth(final boolean isTraditional) { public String getChineseMonth(final boolean isTraditional) {
return ChineseMonth.getChineseMonthName(isLeapMonth(), 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 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);
}
/**
* 获取标准化农历日期
* 支持格式
*<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;
}
/** /**
* 获得节气 * 获得节气
* *
@@ -470,12 +382,70 @@ public class ChineseDate {
*/ */
public String toStringNormal() { public String toStringNormal() {
return String.format("%04d-%02d-%02d", this.year, 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 @Override
public String toString() { public String toString() {
return String.format("%s%s年 %s%s", getCyclical(), getChineseZodiac(), getChineseMonthName(), getChineseDay()); return toString(ChineseDateFormat.GXSS);
}
/**
* 获取标准化农历日期
* 支持格式
* <ol>
* <li>{@link ChineseDateFormat#GSS} 干支纪年 数序纪月 数序纪日</li>
* <li>{@link ChineseDateFormat#XSS} 生肖纪年 数序纪月 数序纪日</li>
* <li>{@link ChineseDateFormat#GXSS} 干支生肖纪年 数序纪月(传统表示) 数序纪日日</li>
* <li>{@link ChineseDateFormat#GSG} 干支纪年 数序纪月 干支纪日</li>
* <li>{@link ChineseDateFormat#GGG} 干支纪年 干支纪月 干支纪日</li>
* <li>{@link ChineseDateFormat#MIX} 农历年年首所在的公历年份 干支纪年 数序纪月 数序纪日</li>
* </ol>
*
* @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 @Override
@@ -507,49 +477,9 @@ public class ChineseDate {
*/ */
private String cyclicalm(final int year, final int month, final int day) { private String cyclicalm(final int year, final int month, final int day) {
return StrUtil.format("{}年{}月{}日", return StrUtil.format("{}年{}月{}日",
GanZhi.getGanzhiOfYear(this.year), GanZhi.getGanzhiOfYear(this.year),
GanZhi.getGanzhiOfMonth(year, month, day), GanZhi.getGanzhiOfMonth(year, month, day),
GanZhi.getGanzhiOfDay(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);
} }
/** /**
@@ -564,7 +494,7 @@ public class ChineseDate {
private DateTime lunar2solar(final int chineseYear, final int chineseMonth, final int chineseDay, final boolean isLeapMonth) { private DateTime lunar2solar(final int chineseYear, final int chineseMonth, final int chineseDay, final boolean isLeapMonth) {
//超出了最大极限值 //超出了最大极限值
if ((chineseYear == 2100 && chineseMonth == 12 && chineseDay > 1) || 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; return null;
} }
final int day = LunarInfo.monthDays(chineseYear, chineseMonth); final int day = LunarInfo.monthDays(chineseYear, chineseMonth);

View File

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

View File

@@ -78,15 +78,15 @@ public class GanZhi {
/** /**
* 获取干支日 * 获取干支日
* *
* @param year 公历年 * @param year 公历年
* @param month 公历月从1开始 * @param monthBase1 公历月从1开始
* @param day 公历日 * @param day 公历日
* @return 干支 * @return 干支
* @since 5.4.7 * @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相差天数不包括当天 // 与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天 //1899-12-21是农历1899年腊月甲子日 41相差1900-01-31有41天
return cyclicalm((int) (days - LunarInfo.BASE_DAY + 41)); return cyclicalm((int) (days - LunarInfo.BASE_DAY + 41));
} }

View File

@@ -27,13 +27,13 @@ import java.util.Map;
*/ */
public class LunarFestival { 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 // 来自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<Map.Entry<Integer, Integer>, String> L_FTV = new TableMap<>(16); private static final TableMap<Map.Entry<Integer, Integer>, String> L_FTV = new TableMap<>(16);
static { 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, 2), "犬日");
L_FTV.put(MapUtil.entry(1, 3), "猪日"); L_FTV.put(MapUtil.entry(1, 3), "猪日");
L_FTV.put(MapUtil.entry(1, 4), "羊日"); 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(4, 8), "佛诞节");
// 五月 // 五月
L_FTV.put(MapUtil.entry(5, 5), "端午节 端阳节*"); L_FTV.put(MapUtil.entry(5, 5), "端午节 端阳节");
// 六月 // 六月
L_FTV.put(MapUtil.entry(6, 6), "晒衣节 姑姑节"); 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(7, 30), "地藏节");
// 八月 // 八月
L_FTV.put(MapUtil.entry(8, 15), "中秋节*"); L_FTV.put(MapUtil.entry(8, 15), "中秋节");
// 九月 // 九月
L_FTV.put(MapUtil.entry(9, 9), "重阳节"); L_FTV.put(MapUtil.entry(9, 9), "重阳节");

View File

@@ -13,6 +13,7 @@
package org.dromara.hutool.core.date; package org.dromara.hutool.core.date;
import org.dromara.hutool.core.date.chinese.ChineseDate; 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.dromara.hutool.core.text.StrUtil;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -30,7 +31,7 @@ public class ChineseDateTest {
Assertions.assertEquals(2020, date.getChineseYear()); Assertions.assertEquals(2020, date.getChineseYear());
Assertions.assertEquals(1, date.getMonth()); Assertions.assertEquals(1, date.getMonth());
Assertions.assertEquals("", date.getChineseMonth()); Assertions.assertEquals("", date.getChineseMonth());
Assertions.assertEquals("正月", date.getChineseMonthName()); Assertions.assertEquals("正月", date.getChineseMonthName());
@@ -40,12 +41,12 @@ public class ChineseDateTest {
Assertions.assertEquals("庚子", date.getCyclical()); Assertions.assertEquals("庚子", date.getCyclical());
Assertions.assertEquals("", date.getChineseZodiac()); Assertions.assertEquals("", date.getChineseZodiac());
Assertions.assertEquals("春节", date.getFestivals()); Assertions.assertEquals("春节", date.getFestivals());
Assertions.assertEquals("庚子鼠年 正月初一", date.toString()); Assertions.assertEquals("农历庚子鼠年正月初一", date.toString());
date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-01-14"))); 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"))); 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()); Assertions.assertEquals("2019-12-30", date.toStringNormal());
} }
@@ -59,10 +60,10 @@ public class ChineseDateTest {
@Test @Test
public void parseTest(){ public void parseTest(){
ChineseDate date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1996-07-14"))); 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"))); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1996-07-15")));
Assertions.assertEquals("丙子鼠年 五月三十", date.toString()); Assertions.assertEquals("农历丙子鼠年五月三十", date.toString());
} }
@Test @Test
@@ -92,23 +93,23 @@ public class ChineseDateTest {
// 修复这两个日期不正确的问题 // 修复这两个日期不正确的问题
// 问题出在计算与1900-01-31相差天数的问题上了相差天数非整天 // 问题出在计算与1900-01-31相差天数的问题上了相差天数非整天
ChineseDate date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1991-09-14"))); 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"))); date = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1991-09-15")));
Assertions.assertEquals("辛未羊年 八月初八", date.toString()); Assertions.assertEquals("农历辛未羊年八月初八", date.toString());
} }
@Test @Test
public void dateTest2(){ public void dateTest2(){
//noinspection ConstantConditions //noinspection ConstantConditions
final ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19")); final ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19"));
Assertions.assertEquals("庚子鼠年 九月初三", date.toString()); Assertions.assertEquals("农历庚子鼠年九月初三", date.toString());
} }
@Test @Test
public void dateTest2_2(){ public void dateTest2_2(){
//noinspection ConstantConditions //noinspection ConstantConditions
final ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20")); final ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20"));
Assertions.assertEquals("庚子鼠年 五月三十", date.toString()); Assertions.assertEquals("农历庚子鼠年五月三十", date.toString());
} }
@Test @Test
@@ -116,7 +117,7 @@ public class ChineseDateTest {
// 初一offset为0测试 // 初一offset为0测试
//noinspection ConstantConditions //noinspection ConstantConditions
final ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22")); final ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22"));
Assertions.assertEquals("己未羊年 闰二月初一", date.toString()); Assertions.assertEquals("农历己未羊年闰二月初一", date.toString());
} }
@Test @Test
@@ -126,8 +127,8 @@ public class ChineseDateTest {
//noinspection ConstantConditions //noinspection ConstantConditions
final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27")); final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27"));
Assertions.assertEquals("戊申猴年 五月初五", c1.toString()); Assertions.assertEquals("农历戊申猴年五月初五", c1.toString());
Assertions.assertEquals("戊申猴年 闰五月初五", c2.toString()); Assertions.assertEquals("农历戊申猴年闰五月初五", c2.toString());
} }
@Test @Test
@@ -135,7 +136,7 @@ public class ChineseDateTest {
//https://github.com/dromara/hutool/issues/2112 //https://github.com/dromara/hutool/issues/2112
final ChineseDate springFestival = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2022-02-01"))); final ChineseDate springFestival = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2022-02-01")));
final String chineseMonth = springFestival.getChineseMonth(); final String chineseMonth = springFestival.getChineseMonth();
Assertions.assertEquals("", chineseMonth); Assertions.assertEquals("", chineseMonth);
} }
@Test @Test
@@ -144,17 +145,17 @@ public class ChineseDateTest {
Date date = DateUtil.parse("1970-01-01"); Date date = DateUtil.parse("1970-01-01");
//noinspection ConstantConditions //noinspection ConstantConditions
ChineseDate chineseDate = new ChineseDate(date); ChineseDate chineseDate = new ChineseDate(date);
Assertions.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString()); Assertions.assertEquals("农历己酉鸡年冬月廿四", chineseDate.toString());
date = DateUtil.parse("1970-01-02"); date = DateUtil.parse("1970-01-02");
//noinspection ConstantConditions //noinspection ConstantConditions
chineseDate = new ChineseDate(date); chineseDate = new ChineseDate(date);
Assertions.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString()); Assertions.assertEquals("农历己酉鸡年冬月廿五", chineseDate.toString());
date = DateUtil.parse("1970-01-03"); date = DateUtil.parse("1970-01-03");
//noinspection ConstantConditions //noinspection ConstantConditions
chineseDate = new ChineseDate(date); chineseDate = new ChineseDate(date);
Assertions.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString()); Assertions.assertEquals("农历己酉鸡年冬月廿六", chineseDate.toString());
} }
@Test @Test
@@ -163,7 +164,7 @@ public class ChineseDateTest {
final Date date = DateUtil.parse("1900-01-31"); final Date date = DateUtil.parse("1900-01-31");
//noinspection ConstantConditions //noinspection ConstantConditions
final ChineseDate chineseDate = new ChineseDate(date); final ChineseDate chineseDate = new ChineseDate(date);
Assertions.assertEquals("庚子鼠年 正月初一", chineseDate.toString()); Assertions.assertEquals("农历庚子鼠年正月初一", chineseDate.toString());
} }
@Test @Test
@@ -200,24 +201,22 @@ public class ChineseDateTest {
Assertions.assertEquals(chineseDate2, chineseDate3); Assertions.assertEquals(chineseDate2, chineseDate3);
Assertions.assertNotEquals(chineseDate2, chineseDate4); Assertions.assertNotEquals(chineseDate2, chineseDate4);
} }
@Test @Test
public void getNormalizedDateTest(){ public void getNormalizedDateTest(){
final Date date = DateUtil.parse("2024-4-24"); 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 Date date2 = DateUtil.parse("2024-4-30");
final ChineseDate chineseDate2 = new ChineseDate(Objects.requireNonNull(date2));
final ChineseDate chineseDate = new ChineseDate(date); Assertions.assertEquals("农历甲辰年三月廿二", chineseDate2.toString(ChineseDateFormat.GSS));
final ChineseDate chineseDate2 = new ChineseDate(date2); Assertions.assertEquals("农历龙年三月廿二", chineseDate2.toString(ChineseDateFormat.XSS));
Assertions.assertEquals("农历甲辰年三月甲子日", chineseDate2.toString(ChineseDateFormat.GSG));
Assertions.assertEquals("公元2024年农历甲辰年三月十六", chineseDate.getNormalizedDate()); Assertions.assertEquals("公元2024年农历甲辰年三月廿二", chineseDate2.toString(ChineseDateFormat.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(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));
} }
} }

View File

@@ -13,6 +13,7 @@
package org.dromara.hutool.core.date; package org.dromara.hutool.core.date;
import org.dromara.hutool.core.date.chinese.ChineseDate; 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.dromara.hutool.core.date.chinese.GanZhi;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -30,31 +31,31 @@ public class GanzhiTest {
public void getCyclicalYMDTest(){ public void getCyclicalYMDTest(){
//通过公历构建 //通过公历构建
final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1993-01-06"))); final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1993-01-06")));
final String cyclicalYMD = chineseDate.getCyclicalYMD(); final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG);
Assertions.assertEquals("壬申年癸丑月丁亥日",cyclicalYMD); Assertions.assertEquals("农历壬申年癸丑月丁亥日",cyclicalYMD);
} }
@Test @Test
public void getCyclicalYMDTest2(){ public void getCyclicalYMDTest2(){
//通过农历构建 //通过农历构建
final ChineseDate chineseDate = new ChineseDate(1992,12,14); final ChineseDate chineseDate = new ChineseDate(1992,12,14);
final String cyclicalYMD = chineseDate.getCyclicalYMD(); final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG);
Assertions.assertEquals("壬申年癸丑月丁亥日",cyclicalYMD); Assertions.assertEquals("农历壬申年癸丑月丁亥日",cyclicalYMD);
} }
@Test @Test
public void getCyclicalYMDTest3(){ public void getCyclicalYMDTest3(){
//通过公历构建 //通过公历构建
final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-08-28"))); final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("2020-08-28")));
final String cyclicalYMD = chineseDate.getCyclicalYMD(); final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG);
Assertions.assertEquals("庚子年甲申月癸卯日",cyclicalYMD); Assertions.assertEquals("农历庚子年甲申月癸卯日",cyclicalYMD);
} }
@Test @Test
public void getCyclicalYMDTest4(){ public void getCyclicalYMDTest4(){
//通过公历构建 //通过公历构建
final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1905-08-28"))); final ChineseDate chineseDate = new ChineseDate(Objects.requireNonNull(DateUtil.parse("1905-08-28")));
final String cyclicalYMD = chineseDate.getCyclicalYMD(); final String cyclicalYMD = chineseDate.toString(ChineseDateFormat.GGG);
Assertions.assertEquals("乙巳年甲申月己亥日",cyclicalYMD); Assertions.assertEquals("农历乙巳年甲申月己亥日",cyclicalYMD);
} }
} }

View File

@@ -8,7 +8,7 @@ public class IssueI97WU6Test {
public void getTermTest() { public void getTermTest() {
// 润十月没有三十,十月有三十 // 润十月没有三十,十月有三十
final ChineseDate chineseDate = new ChineseDate(1984, 10, 30, false); final ChineseDate chineseDate = new ChineseDate(1984, 10, 30, false);
Assertions.assertEquals("甲子鼠年 寒月三十", chineseDate.toString()); Assertions.assertEquals("农历甲子鼠年寒月三十", chineseDate.toString());
Assertions.assertEquals("小雪", chineseDate.getTerm()); Assertions.assertEquals("小雪", chineseDate.getTerm());
} }
} }