add methods

This commit is contained in:
Looly
2021-03-18 22:22:19 +08:00
parent 8e8518c8d1
commit 81694dadfe
4 changed files with 72 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import cn.hutool.core.date.chinese.LunarFestival;
import cn.hutool.core.date.chinese.LunarInfo;
import cn.hutool.core.util.StrUtil;
import java.util.Calendar;
import java.util.Date;
@@ -151,6 +152,16 @@ public class ChineseDate {
return this.year;
}
/**
* 获取公历的年
*
* @return 公历年
* @since 5.6.1
*/
public int getGregorianYear(){
return this.gyear;
}
/**
* 获取农历的月从1开始计数
*
@@ -161,6 +172,26 @@ public class ChineseDate {
return this.month;
}
/**
* 获取公历的月从1开始计数
*
* @return 公历月
* @since 5.6.1
*/
public int getGregorianMonthBase1(){
return this.gmonth;
}
/**
* 获取公历的月从0开始计数
*
* @return 公历月
* @since 5.6.1
*/
public int getGregorianMonth(){
return this.gmonth -1;
}
/**
* 当前农历月份是否为闰月
*
@@ -200,6 +231,16 @@ public class ChineseDate {
return this.day;
}
/**
* 获取公历的日
*
* @return 公历日
* @since 5.6.1
*/
public int getGregorianDay(){
return this.gday;
}
/**
* 获得农历日
*
@@ -223,6 +264,28 @@ public class ChineseDate {
}
}
/**
* 获取公历的Date
*
* @return 公历Date
* @since 5.6.1
*/
public Date getGregorianDate(){
return DateUtil.date(getGregorianCalendar());
}
/**
* 获取公历的Calendar
*
* @return 公历Calendar
* @since 5.6.1
*/
public Calendar getGregorianCalendar(){
final Calendar calendar = CalendarUtil.calendar();
//noinspection MagicConstant
calendar.set(this.gyear, getGregorianMonth(), this.gday, 0, 0, 0);
return calendar;
}
/**
* 获得节日
@@ -347,4 +410,4 @@ public class ChineseDate {
// ------------------------------------------------------- private method end
}
}

View File

@@ -9,6 +9,7 @@ public class ChineseDateTest {
@Test
public void chineseDateTest() {
ChineseDate date = new ChineseDate(DateUtil.parseDate("2020-01-25"));
Assert.assertEquals("2020-01-25 00:00:00", date.getGregorianDate().toString());
Assert.assertEquals(2020, date.getChineseYear());
Assert.assertEquals(1, date.getMonth());
@@ -50,6 +51,7 @@ public class ChineseDateTest {
@Test
public void getChineseMonthTest(){
ChineseDate chineseDate = new ChineseDate(2020,6,15);
Assert.assertEquals("2020-08-04 00:00:00", chineseDate.getGregorianDate().toString());
Assert.assertEquals("六月", chineseDate.getChineseMonth());
chineseDate = new ChineseDate(2020,4,15);