add method for ChineseDate

This commit is contained in:
Looly
2020-03-20 10:44:11 +08:00
parent 887a2634e9
commit 77aee971a5
7 changed files with 229 additions and 48 deletions

View File

@@ -131,6 +131,20 @@ public class ChineseDate {
day = offset + 1;
}
/**
* 构造方法传入日期
*
* @param chineseYear 农历年
* @param chineseMonth 农历月1表示一月正月
* @param chineseDay 农历日1表示初一
* @since 5.2.4
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay) {
this.day = chineseDay;
this.month = chineseMonth;
this.year = chineseYear;
this.leap = DateUtil.isLeapYear(chineseYear);
}
/**
* 获得农历年份
@@ -142,7 +156,17 @@ public class ChineseDate {
}
/**
* 获农历月份
* 获农历的月从1开始计数
*
* @return 农历的月
* @since 5.2.4
*/
public int getMonth() {
return this.month;
}
/**
* 获得农历月份(中文,例如二月,十二月,或者润一月)
*
* @return 返回农历月份
*/
@@ -151,7 +175,7 @@ public class ChineseDate {
}
/**
* 获得农历月称呼
* 获得农历月称呼(中文,例如二月,腊月,或者润正月)
*
* @return 返回农历月份称呼
*/
@@ -159,6 +183,16 @@ public class ChineseDate {
return (leap ? "" : "") + chineseNumberName[month - 1] + "";
}
/**
* 获取农历的日从1开始计数
*
* @return 农历的日从1开始计数
* @since 5.2.4
*/
public int getDay() {
return this.day;
}
/**
* 获得农历日
*
@@ -230,6 +264,16 @@ public class ChineseDate {
return (cyclicalm(num));
}
/**
* 转换为标准的日期格式来表示农历日期例如2020-01-13
*
* @return 标准的日期格式
* @since 5.2.4
*/
public String toStringNormal(){
return String.format("%04d-%02d-%02d", this.year, this.month, this.day);
}
@Override
public String toString() {
return String.format("%s%s年 %s%s", getCyclical(), getChineseZodiac(), getChineseMonthName(), getChineseDay());

View File

@@ -275,6 +275,48 @@ public class ObjectUtil {
return (null != object) ? object : defaultValue;
}
/**
* 如果给定对象为{@code null}或者 "" 返回默认值
*
* <pre>
* ObjectUtil.defaultIfEmpty(null, null) = null
* ObjectUtil.defaultIfEmpty(null, "") = ""
* ObjectUtil.defaultIfEmpty("", "zz") = "zz"
* ObjectUtil.defaultIfEmpty(" ", "zz") = " "
* ObjectUtil.defaultIfEmpty("abc", *) = "abc"
* </pre>
*
* @param <T> 对象类型必须实现CharSequence接口
* @param str 被检查对象,可能为{@code null}
* @param defaultValue 被检查对象为{@code null}或者 ""返回的默认值,可以为{@code null}或者 ""
* @return 被检查对象为{@code null}或者 ""返回默认值,否则返回原值
* @since 5.0.4
*/
public static <T extends CharSequence> T defaultIfEmpty(final T str, final T defaultValue) {
return StrUtil.isEmpty(str) ? defaultValue : str;
}
/**
* 如果给定对象为{@code null}或者""或者空白符返回默认值
*
* <pre>
* ObjectUtil.defaultIfEmpty(null, null) = null
* ObjectUtil.defaultIfEmpty(null, "") = ""
* ObjectUtil.defaultIfEmpty("", "zz") = "zz"
* ObjectUtil.defaultIfEmpty(" ", "zz") = "zz"
* ObjectUtil.defaultIfEmpty("abc", *) = "abc"
* </pre>
*
* @param <T> 对象类型必须实现CharSequence接口
* @param str 被检查对象,可能为{@code null}
* @param defaultValue 被检查对象为{@code null}或者 ""或者空白符返回的默认值,可以为{@code null}或者 ""或者空白符
* @return 被检查对象为{@code null}或者 ""或者空白符返回默认值,否则返回原值
* @since 5.0.4
*/
public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultValue) {
return StrUtil.isBlank(str) ? defaultValue : str;
}
/**
* 克隆对象<br>
* 如果对象实现Cloneable接口调用其clone方法<br>

View File

@@ -9,9 +9,15 @@ public class ChineseDateTest {
public void chineseDateTest() {
ChineseDate date = new ChineseDate(DateUtil.parseDate("2020-01-25"));
Assert.assertEquals(2020, date.getChineseYear());
Assert.assertEquals(1, date.getMonth());
Assert.assertEquals("一月", date.getChineseMonth());
Assert.assertEquals("正月", date.getChineseMonthName());
Assert.assertEquals(1, date.getDay());
Assert.assertEquals("初一", date.getChineseDay());
Assert.assertEquals("庚子", date.getCyclical());
Assert.assertEquals("", date.getChineseZodiac());
Assert.assertEquals("春节", date.getFestivals());
@@ -21,5 +27,13 @@ public class ChineseDateTest {
Assert.assertEquals("己亥猪年 腊月二十", date.toString());
date = new ChineseDate(DateUtil.parseDate("2020-01-24"));
Assert.assertEquals("己亥猪年 腊月三十", date.toString());
Assert.assertEquals("2019-12-30", date.toStringNormal());
}
@Test
public void toStringNormalTest(){
ChineseDate date = new ChineseDate(DateUtil.parseDate("2020-03-1"));
Assert.assertEquals("2020-02-08", date.toStringNormal());
}
}