1
0
mirror of synced 2026-10-26 00:16:11 +08:00

Compare commits

..

5 Commits

13 changed files with 130 additions and 53 deletions

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency> <dependency>
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<version>1.2.22</version> <version>1.2.25</version>
</dependency> </dependency>
``` ```

View File

@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency> <dependency>
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<version>1.2.22</version> <version>1.2.25</version>
</dependency> </dependency>
``` ```

11
pom.xml
View File

@@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId> <groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId> <artifactId>lunar</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.2.22</version> <version>1.2.25</version>
<name>${project.groupId}:${project.artifactId}</name> <name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/6tail/lunar-java</url> <url>https://github.com/6tail/lunar-java</url>
<description>a calendar library for Solar and Chinese Lunar</description> <description>a calendar library for Solar and Chinese Lunar</description>
@@ -48,6 +48,7 @@
<sourceEncoding>UTF-8</sourceEncoding> <sourceEncoding>UTF-8</sourceEncoding>
<source-version>1.5</source-version> <source-version>1.5</source-version>
<target-version>1.5</target-version> <target-version>1.5</target-version>
<creator>6tail</creator>
<maven-compiler-plugin-version>3.3</maven-compiler-plugin-version> <maven-compiler-plugin-version>3.3</maven-compiler-plugin-version>
<maven-surefire-plugin-version>2.5</maven-surefire-plugin-version> <maven-surefire-plugin-version>2.5</maven-surefire-plugin-version>
<maven-javadoc-plugin-version>2.9</maven-javadoc-plugin-version> <maven-javadoc-plugin-version>2.9</maven-javadoc-plugin-version>
@@ -60,7 +61,7 @@
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.13.1</version> <version>4.13.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@@ -154,6 +155,12 @@
<descriptors> <descriptors>
<descriptor>${basedir}/src/assembly/assembly.xml</descriptor> <descriptor>${basedir}/src/assembly/assembly.xml</descriptor>
</descriptors> </descriptors>
<archive>
<manifestEntries>
<Created-By>${creator}</Created-By>
<Built-By>${creator}</Built-By>
</manifestEntries>
</archive>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@@ -474,6 +474,26 @@ public class EightChar {
return LunarUtil.NAYIN.get(getTaiYuan()); return LunarUtil.NAYIN.get(getTaiYuan());
} }
/**
* 获取胎息
*
* @return 胎息
*/
public String getTaiXi() {
int ganIndex = (2 == sect) ? lunar.getDayGanIndexExact2() : lunar.getDayGanIndexExact();
int zhiIndex = (2 == sect) ? lunar.getDayZhiIndexExact2() : lunar.getDayZhiIndexExact();
return LunarUtil.HE_GAN_5[ganIndex] + LunarUtil.HE_ZHI_6[zhiIndex];
}
/**
* 获取胎息纳音
*
* @return 纳音
*/
public String getTaiXiNaYin() {
return LunarUtil.NAYIN.get(getTaiXi());
}
/** /**
* 获取命宫 * 获取命宫
* *
@@ -531,7 +551,10 @@ public class EightChar {
timeZhiIndex = i; timeZhiIndex = i;
} }
} }
int zhiIndex = (2 + (monthZhiIndex + timeZhiIndex)) % 12; int zhiIndex = 2 + monthZhiIndex + timeZhiIndex;
if (zhiIndex > 12) {
zhiIndex -= 12;
}
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex); int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex);
if (jiaZiIndex >= 60) { if (jiaZiIndex >= 60) {
jiaZiIndex -= 60; jiaZiIndex -= 60;

View File

@@ -183,6 +183,9 @@ public class Lunar {
this.second = second; this.second = second;
Solar noon = Solar.fromJulianDay(m.getFirstJulianDay() + lunarDay - 1); Solar noon = Solar.fromJulianDay(m.getFirstJulianDay() + lunarDay - 1);
this.solar = Solar.fromYmdHms(noon.getYear(), noon.getMonth(), noon.getDay(), hour, minute, second); this.solar = Solar.fromYmdHms(noon.getYear(), noon.getMonth(), noon.getDay(), hour, minute, second);
if (noon.getYear() != lunarYear) {
y = LunarYear.fromYear(noon.getYear());
}
compute(y); compute(y);
} }
@@ -1904,7 +1907,7 @@ public class Lunar {
* @return 日冲,如申 * @return 日冲,如申
*/ */
public String getDayChong() { public String getDayChong() {
return LunarUtil.CHONG[dayZhiIndex + 1]; return LunarUtil.CHONG[dayZhiIndex];
} }
/** /**
@@ -1946,7 +1949,7 @@ public class Lunar {
* @return 无情之克的日冲天干,如甲 * @return 无情之克的日冲天干,如甲
*/ */
public String getDayChongGan() { public String getDayChongGan() {
return LunarUtil.CHONG_GAN[dayGanIndex + 1]; return LunarUtil.CHONG_GAN[dayGanIndex];
} }
/** /**
@@ -1955,7 +1958,7 @@ public class Lunar {
* @return 有情之克的日冲天干,如甲 * @return 有情之克的日冲天干,如甲
*/ */
public String getDayChongGanTie() { public String getDayChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[dayGanIndex + 1]; return LunarUtil.CHONG_GAN_TIE[dayGanIndex];
} }
/** /**
@@ -1964,7 +1967,7 @@ public class Lunar {
* @return 时冲,如申 * @return 时冲,如申
*/ */
public String getTimeChong() { public String getTimeChong() {
return LunarUtil.CHONG[timeZhiIndex + 1]; return LunarUtil.CHONG[timeZhiIndex];
} }
/** /**
@@ -2006,7 +2009,7 @@ public class Lunar {
* @return 无情之克的时冲天干,如甲 * @return 无情之克的时冲天干,如甲
*/ */
public String getTimeChongGan() { public String getTimeChongGan() {
return LunarUtil.CHONG_GAN[timeGanIndex + 1]; return LunarUtil.CHONG_GAN[timeGanIndex];
} }
/** /**
@@ -2015,7 +2018,7 @@ public class Lunar {
* @return 有情之克的时冲天干,如甲 * @return 有情之克的时冲天干,如甲
*/ */
public String getTimeChongGanTie() { public String getTimeChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[timeGanIndex + 1]; return LunarUtil.CHONG_GAN_TIE[timeGanIndex];
} }
/** /**

View File

@@ -235,7 +235,7 @@ public class LunarTime {
* @return 时冲,如申 * @return 时冲,如申
*/ */
public String getChong() { public String getChong() {
return LunarUtil.CHONG[zhiIndex + 1]; return LunarUtil.CHONG[zhiIndex];
} }
/** /**
@@ -277,7 +277,7 @@ public class LunarTime {
* @return 无情之克的时冲天干,如甲 * @return 无情之克的时冲天干,如甲
*/ */
public String getChongGan() { public String getChongGan() {
return LunarUtil.CHONG_GAN[ganIndex + 1]; return LunarUtil.CHONG_GAN[ganIndex];
} }
/** /**
@@ -286,7 +286,7 @@ public class LunarTime {
* @return 有情之克的时冲天干,如甲 * @return 有情之克的时冲天干,如甲
*/ */
public String getChongGanTie() { public String getChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[ganIndex + 1]; return LunarUtil.CHONG_GAN_TIE[ganIndex];
} }
/** /**

View File

@@ -386,7 +386,7 @@ public class Solar {
if (null != f) { if (null != f) {
l.add(f); l.add(f);
} }
if (day + 7 >= SolarUtil.getDaysOfMonth(year, month)) { if (day + 7 > SolarUtil.getDaysOfMonth(year, month)) {
f = SolarUtil.WEEK_FESTIVAL.get(month + "-0-" + week); f = SolarUtil.WEEK_FESTIVAL.get(month + "-0-" + week);
if (null != f) { if (null != f) {
l.add(f); l.add(f);

View File

@@ -17,6 +17,8 @@ public class HolidayUtil {
private static final int SIZE = 18; private static final int SIZE = 18;
/** 0 */ /** 0 */
private static final char ZERO = '0'; private static final char ZERO = '0';
/** 删除标识 */
private static final String TAG_REMOVE = "~";
/** 默认节假日名称(元旦0,春节1,清明2,劳动3,端午4,中秋5,国庆6,国庆中秋7,抗战胜利日8) */ /** 默认节假日名称(元旦0,春节1,清明2,劳动3,端午4,中秋5,国庆6,国庆中秋7,抗战胜利日8) */
public static final String[] NAMES = {"元旦节","春节","清明节","劳动节","端午节","中秋节","国庆节","国庆中秋","抗战胜利日"}; public static final String[] NAMES = {"元旦节","春节","清明节","劳动节","端午节","中秋节","国庆节","国庆中秋","抗战胜利日"};
/** 默认节假日数据,日期YYYYMMDD+名称下标+是否调休+对应节日YYYYMMDD */ /** 默认节假日数据,日期YYYYMMDD+名称下标+是否调休+对应节日YYYYMMDD */
@@ -188,35 +190,38 @@ public class HolidayUtil {
* @param data 需要修正或追加的节假日数据,每18位表示1天依次排列,格式:当天年月日YYYYMMDD(8位)+节假日名称下标(1位)+调休标识(1位)+节假日当天YYYYMMDD(8位)。例:202005023120200501代表2020-05-02为劳动节放假,对应节假日为2020-05-01 * @param data 需要修正或追加的节假日数据,每18位表示1天依次排列,格式:当天年月日YYYYMMDD(8位)+节假日名称下标(1位)+调休标识(1位)+节假日当天YYYYMMDD(8位)。例:202005023120200501代表2020-05-02为劳动节放假,对应节假日为2020-05-01
*/ */
public static void fix(String[] names, String data){ public static void fix(String[] names, String data){
if(null!=names){ if(null != names){
NAMES_IN_USE = names; NAMES_IN_USE = names;
} }
if(null==data){ if(null == data){
return; return;
} }
StringBuilder append = new StringBuilder(); StringBuilder append = new StringBuilder();
while(data.length()>=SIZE){ while(data.length() >= SIZE){
String segment = data.substring(0,SIZE); String segment = data.substring(0, SIZE);
String day = segment.substring(0,8); String day = segment.substring(0, 8);
boolean remove = TAG_REMOVE.equals(segment.substring(8, 9));
Holiday holiday = getHoliday(day); Holiday holiday = getHoliday(day);
if(null==holiday){ if(null == holiday){
append.append(segment); if (!remove) {
append.append(segment);
}
}else{ }else{
int nameIndex = -1; int nameIndex = -1;
for(int i=0,j=NAMES_IN_USE.length;i<j;i++){ for(int i = 0,j = NAMES_IN_USE.length; i < j; i++){
if(NAMES_IN_USE[i].equals(holiday.getName())){ if(NAMES_IN_USE[i].equals(holiday.getName())){
nameIndex = i; nameIndex = i;
break; break;
} }
} }
if(nameIndex>-1) { if(nameIndex>-1) {
String old = day+(char)(nameIndex+ZERO)+(holiday.isWork()?ZERO:'1')+holiday.getTarget().replace("-",""); String old = day + (char)(nameIndex + ZERO) + (holiday.isWork() ? ZERO : '1') + holiday.getTarget().replace("-","");
DATA_IN_USE = DATA_IN_USE.replace(old, segment); DATA_IN_USE = DATA_IN_USE.replace(old, remove ? "" : segment);
} }
} }
data = data.substring(SIZE); data = data.substring(SIZE);
} }
if(append.length()>0){ if(append.length() > 0){
DATA_IN_USE += append.toString(); DATA_IN_USE += append.toString();
} }
} }

View File

@@ -351,32 +351,17 @@ public class LunarUtil{
} }
}; };
/** 地支相冲(子午相冲,丑未相冲,寅申相冲,辰戌相冲,卯酉相冲,巳亥相冲),由于地支对应十二生肖,也就对应了生肖相冲 */ /** 地支相冲(子午相冲,丑未相冲,寅申相冲,辰戌相冲,卯酉相冲,巳亥相冲),由于地支对应十二生肖,也就对应了生肖相冲 */
public static final String[] CHONG = {"","午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳"}; public static final String[] CHONG = {"午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳"};
/** 天干相冲之无情之克(阳克阳,阴克阴) */ /** 天干相冲之无情之克(阳克阳,阴克阴) */
public static final String[] CHONG_GAN = {"","戊","己","庚","辛","壬","癸","甲","乙","丙","丁"}; public static final String[] CHONG_GAN = {"戊","己","庚","辛","壬","癸","甲","乙","丙","丁"};
/** 天干四冲(无情之克中克得最严重的4个) */
public static final Map<String,String> CHONG_GAN_BAD = new HashMap<String,String>(){
private static final long serialVersionUID = -1;
{
put("庚","甲");
put("辛","乙");
put("壬","丙");
put("癸","丁");
}
};
/** 天干相冲之有情之克(阳克阴,阴克阳) */ /** 天干相冲之有情之克(阳克阴,阴克阳) */
public static final String[] CHONG_GAN_TIE = {"","己","戊","辛","庚","癸","壬","乙","甲","丁","丙"}; public static final String[] CHONG_GAN_TIE = {"己","戊","辛","庚","癸","壬","乙","甲","丁","丙"};
/** 天干五合(有情之克中最有情的5个) */ /** 天干四冲(无情之克中克得最严重的4个) */
public static final Map<String,String> CHONG_GAN_TIE_GOOD = new HashMap<String,String>(){ public static final String[] CHONG_GAN_4 = {"庚","辛","壬","癸","","","甲","乙","丙","丁"};
private static final long serialVersionUID = -1; /** 天干五合(有情之克中最有情的5个,甲己合,乙庚合,丙辛合,丁壬合,戊癸合) */
{ public static final String[] HE_GAN_5 = {"己","庚","辛","壬","癸","甲","乙","丙","丁","戊"};
put("甲","己"); /** 地支六合(子丑合,寅亥合,卯戌合,辰酉合,巳申合,午未合) */
put("丙","辛"); public static final String[] HE_ZHI_6 = {"丑","子","亥","戌","酉","申","未","午","巳","辰","卯","寅"};
put("戊","癸");
put("庚","乙");
put("壬","丁");
}
};
/** 煞(逢巳日、酉日、丑日必煞东;亥日、卯日、未日必煞西;申日、子日、辰日必煞南;寅日、午日、戌日必煞北) */ /** 煞(逢巳日、酉日、丑日必煞东;亥日、卯日、未日必煞西;申日、子日、辰日必煞南;寅日、午日、戌日必煞北) */
public static final Map<String,String> SHA = new HashMap<String,String>(){ public static final Map<String,String> SHA = new HashMap<String,String>(){
private static final long serialVersionUID = -1; private static final long serialVersionUID = -1;

View File

@@ -100,6 +100,7 @@ public class SolarUtil {
put("5-5", Collections.nCopies(1, "马克思诞辰纪念日")); put("5-5", Collections.nCopies(1, "马克思诞辰纪念日"));
put("5-8", Collections.nCopies(1, "世界红十字日")); put("5-8", Collections.nCopies(1, "世界红十字日"));
put("5-11", Collections.nCopies(1, "世界肥胖日")); put("5-11", Collections.nCopies(1, "世界肥胖日"));
put("5-25", Collections.nCopies(1, "525心理健康节"));
put("5-27", Collections.nCopies(1, "上海解放日")); put("5-27", Collections.nCopies(1, "上海解放日"));
put("5-31", Collections.nCopies(1, "世界无烟日")); put("5-31", Collections.nCopies(1, "世界无烟日"));
put("6-5", Collections.nCopies(1, "世界环境日")); put("6-5", Collections.nCopies(1, "世界环境日"));

View File

@@ -230,6 +230,24 @@ public class BaZiTest {
Assert.assertEquals("身宫", "壬午", lunar.getEightChar().getShenGong()); Assert.assertEquals("身宫", "壬午", lunar.getEightChar().getShenGong());
} }
@Test
public void testShenGong1() {
Lunar lunar = new Solar(1994, 12, 6, 2, 0, 0).getLunar();
Assert.assertEquals("身宫", "丁丑", lunar.getEightChar().getShenGong());
}
@Test
public void testShenGong2() {
Lunar lunar = new Solar(1990, 12, 11, 6, 0, 0).getLunar();
Assert.assertEquals("身宫", "庚辰", lunar.getEightChar().getShenGong());
}
@Test
public void testShenGong3() {
Lunar lunar = new Solar(1993, 5, 23, 4, 0, 0).getLunar();
Assert.assertEquals("身宫", "庚申", lunar.getEightChar().getShenGong());
}
@Test @Test
public void testBaZi2Solar() { public void testBaZi2Solar() {
Solar solar = Solar.fromYmdHms(2027,1,27,12,0,0); Solar solar = Solar.fromYmdHms(2027,1,27,12,0,0);
@@ -258,4 +276,23 @@ public class BaZiTest {
Assert.assertEquals("1990-04-15", lunar.getEightChar().getYun(1).getStartSolar().toYmd()); Assert.assertEquals("1990-04-15", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
} }
@Test
public void test10(){
Lunar lunar = Solar.fromYmdHms(1988, 2, 15, 23, 30, 0).getLunar();
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
}
@Test
public void test11(){
Lunar lunar = Lunar.fromYmdHms(1987, 12, 28, 23, 30, 0);
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
}
} }

View File

@@ -94,8 +94,11 @@ public class HolidayTest {
Assert.assertEquals("2020-01-01 春节 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + ""); Assert.assertEquals("2020-01-01 春节 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + "");
Assert.assertEquals("2099-01-01 元旦节 2099-01-01", HolidayUtil.getHoliday("2099-01-01") + ""); Assert.assertEquals("2099-01-01 元旦节 2099-01-01", HolidayUtil.getHoliday("2099-01-01") + "");
// 更改节假日名称 // 自定义节假日名称
String[] names = HolidayUtil.NAMES; String[] names = new String[HolidayUtil.NAMES.length];
for (int i = 0, j = HolidayUtil.NAMES.length; i < j; i++) {
names[i] = HolidayUtil.NAMES[i];
}
names[0] = "元旦"; names[0] = "元旦";
names[1] = "大年初一"; names[1] = "大年初一";
@@ -125,4 +128,17 @@ public class HolidayTest {
Assert.assertNotNull(holiday.getTarget()); Assert.assertNotNull(holiday.getTarget());
Assert.assertEquals("2016-10-01",holiday.getTarget()); Assert.assertEquals("2016-10-01",holiday.getTarget());
} }
@Test
public void testRemove() {
// 设置默认的节假日名称
HolidayUtil.fix(HolidayUtil.NAMES, null);
Holiday holiday = HolidayUtil.getHoliday(2010,1,1);
Assert.assertNotNull(holiday);
Assert.assertEquals("元旦节",holiday.getName());
HolidayUtil.fix("20100101~000000000000000000000000000");
holiday = HolidayUtil.getHoliday(2010,1,1);
Assert.assertNull(holiday);
}
} }

View File

@@ -183,7 +183,7 @@ public class JieQiTest {
@Test @Test
public void test8() { public void test8() {
Lunar lunar = Lunar.fromYmd(2050, 12, 1); Lunar lunar = Lunar.fromYmd(2050, 12, 1);
Assert.assertEquals("2050-12-07 06:41:00", lunar.getJieQiTable().get("大雪").toYmdHms()); Assert.assertEquals("2050-12-07 06:41:00", lunar.getJieQiTable().get("DA_XUE").toYmdHms());
} }
@Test @Test