Skip to content

Commit

Permalink
v1.3.4 LunarMonth增加序号、干支、财神方位等;完善纪念日。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Mar 27, 2023
1 parent 586a8d3 commit 3ef8d0c
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.3</version>
<version>1.3.4</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.3.3</version>
<version>1.3.4</version>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/6tail/lunar-java</url>
<description>a calendar library for Solar and Chinese Lunar</description>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/nlf/calendar/EightChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ public int getDayZhiIndex() {
}

private String getDiShi(int zhiIndex) {
int offset = CHANG_SHENG_OFFSET.get(getDayGan());
int index = offset + (getDayGanIndex() % 2 == 0 ? zhiIndex : -zhiIndex);
int index = CHANG_SHENG_OFFSET.get(getDayGan()) + (getDayGanIndex() % 2 == 0 ? zhiIndex : -zhiIndex);
if (index >= 12) {
index -= 12;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/nlf/calendar/Foto.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public String getDayInChinese() {
return lunar.getDayInChinese();
}

/**
* 获取因果犯忌
* @return 因果犯忌
*/
public List<FotoFestival> getFestivals() {
List<FotoFestival> l = new ArrayList<FotoFestival>();
List<FotoFestival> fs = FotoUtil.FESTIVAL.get(getMonth() + "-" + getDay());
Expand All @@ -83,6 +87,19 @@ public List<FotoFestival> getFestivals() {
return l;
}

/**
* 获取纪念日
* @return 纪念日
*/
public List<String> getOtherFestivals() {
List<String> l = new ArrayList<String>();
List<String> fs = FotoUtil.OTHER_FESTIVAL.get(getMonth() + "-" + getDay());
if (null != fs) {
l.addAll(fs);
}
return l;
}

/**
* 是否月斋
*
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -1776,9 +1776,7 @@ public String getZhiXing() {
* @return 值日天神
*/
public String getDayTianShen() {
String monthZhi = getMonthZhi();
int offset = LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(monthZhi);
return LunarUtil.TIAN_SHEN[(dayZhiIndex + offset) % 12 + 1];
return LunarUtil.TIAN_SHEN[(dayZhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(getMonthZhi())) % 12 + 1];
}

/**
Expand All @@ -1787,9 +1785,7 @@ public String getDayTianShen() {
* @return 值时天神
*/
public String getTimeTianShen() {
String dayZhi = getDayZhiExact();
int offset = LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(dayZhi);
return LunarUtil.TIAN_SHEN[(timeZhiIndex + offset) % 12 + 1];
return LunarUtil.TIAN_SHEN[(timeZhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(getDayZhiExact())) % 12 + 1];
}

/**
Expand Down
75 changes: 73 additions & 2 deletions src/main/java/com/nlf/calendar/LunarMonth.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class LunarMonth {
*/
private final double firstJulianDay;

private final int index;

private final int zhiIndex;

/**
* 初始化
*
Expand All @@ -39,11 +43,13 @@ public class LunarMonth {
* @param dayCount 天数
* @param firstJulianDay 初一的儒略日
*/
public LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay) {
public LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay, int index) {
this.year = lunarYear;
this.month = lunarMonth;
this.dayCount = dayCount;
this.firstJulianDay = firstJulianDay;
this.index = index;
this.zhiIndex = (index - 1 + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
}

/**
Expand Down Expand Up @@ -93,6 +99,31 @@ public int getDayCount() {
return dayCount;
}

public int getIndex() {
return index;
}

public int getZhiIndex() {
return zhiIndex;
}

public int getGanIndex() {
int offset = (LunarYear.fromYear(year).getGanIndex() + 1) % 5 * 2;
return (index - 1 + offset) % 10;
}

public String getGan() {
return LunarUtil.GAN[getGanIndex() + 1];
}

public String getZhi() {
return LunarUtil.ZHI[zhiIndex + 1];
}

public String getGanZhi() {
return getGan() + getZhi();
}

/**
* 获取初一的儒略日
*
Expand All @@ -102,6 +133,46 @@ public double getFirstJulianDay() {
return firstJulianDay;
}

public String getPositionXi() {
return LunarUtil.POSITION_XI[getGanIndex() + 1];
}

public String getPositionXiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionXi());
}

public String getPositionYangGui() {
return LunarUtil.POSITION_YANG_GUI[getGanIndex() + 1];
}

public String getPositionYangGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYangGui());
}

public String getPositionYinGui() {
return LunarUtil.POSITION_YIN_GUI[getGanIndex() + 1];
}

public String getPositionYinGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYinGui());
}

public String getPositionFu(int sect) {
return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[getGanIndex() + 1];
}

public String getPositionFuDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getPositionFu(sect));
}

public String getPositionCai() {
return LunarUtil.POSITION_CAI[getGanIndex() + 1];
}

public String getPositionCaiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionCai());
}

/**
* 获取太岁方位
*
Expand All @@ -121,7 +192,7 @@ public String getPositionTaiSui() {
p = "坤";
break;
default:
p = LunarUtil.POSITION_GAN[Solar.fromJulianDay(this.getFirstJulianDay()).getLunar().getMonthGanIndex()];
p = LunarUtil.POSITION_GAN[Solar.fromJulianDay(getFirstJulianDay()).getLunar().getMonthGanIndex()];
}
return p;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/nlf/calendar/LunarTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ public String getNaYin() {
* @return 值时天神
*/
public String getTianShen() {
String dayZhi = lunar.getDayZhiExact();
int offset = LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(dayZhi);
return LunarUtil.TIAN_SHEN[(zhiIndex + offset) % 12 + 1];
return LunarUtil.TIAN_SHEN[(zhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(lunar.getDayZhiExact())) % 12 + 1];
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/nlf/calendar/LunarYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,20 @@ private void compute() {

int y = prevYear;
int m = 11;
int index = m;
for (int i = 0, j = dayCounts.length; i < j; i++) {
int cm = m;
if (y == leapYear && i == leapIndex) {
cm = -cm;
}
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000));
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000, index));
if (y != leapYear || i + 1 != leapIndex) {
m++;
}
index++;
if (m == 13) {
m = 1;
index = 1;
y++;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
}
List<Integer> hours = new ArrayList<Integer>(2);
String timeZhi = timeGanZhi.substring(1);
for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){
for(int i = 1, j = LunarUtil.ZHI.length; i < j; i++){
if(LunarUtil.ZHI[i].equals(timeZhi)){
hours.add((i - 1) * 2);
break;
}
}
if ("子".equals(timeZhi)) {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/nlf/calendar/util/FotoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,46 @@ public class FotoUtil {
}
};

/**
* 纪念日
*/
public static final Map<String, List<String>> OTHER_FESTIVAL = new HashMap<String, List<String>>() {
private static final long serialVersionUID = -1;

{
put("1-1", Collections.nCopies(1, "弥勒菩萨圣诞"));
put("1-6", Collections.nCopies(1, "定光佛圣诞"));
put("2-8", Collections.nCopies(1, "释迦牟尼佛出家"));
put("2-15", Collections.nCopies(1, "释迦牟尼佛涅槃"));
put("2-19", Collections.nCopies(1, "观世音菩萨圣诞"));
put("2-21", Collections.nCopies(1, "普贤菩萨圣诞"));
put("3-16", Collections.nCopies(1, "准提菩萨圣诞"));
put("4-4", Collections.nCopies(1, "文殊菩萨圣诞"));
put("4-8", Collections.nCopies(1, "释迦牟尼佛圣诞"));
put("4-15", Collections.nCopies(1, "佛吉祥日"));
put("4-28", Collections.nCopies(1, "药王菩萨圣诞"));
put("5-13", Collections.nCopies(1, "伽蓝菩萨圣诞"));
put("6-3", Collections.nCopies(1, "韦驮菩萨圣诞"));
put("6-19", Collections.nCopies(1, "观音菩萨成道"));
put("7-13", Collections.nCopies(1, "大势至菩萨圣诞"));
put("7-15", Collections.nCopies(1, "佛欢喜日"));
put("7-24", Collections.nCopies(1, "龙树菩萨圣诞"));
put("7-30", Collections.nCopies(1, "地藏菩萨圣诞"));
put("8-15", Collections.nCopies(1, "月光菩萨圣诞"));
put("8-22", Collections.nCopies(1, "燃灯佛圣诞"));
put("9-9", Collections.nCopies(1, "摩利支天菩萨圣诞"));
put("9-19", Collections.nCopies(1, "观世音菩萨出家"));
put("9-30", Collections.nCopies(1, "药师琉璃光佛圣诞"));
put("10-5", Collections.nCopies(1, "达摩祖师圣诞"));
put("10-20", Collections.nCopies(1, "文殊菩萨出家"));
put("11-17", Collections.nCopies(1, "阿弥陀佛圣诞"));
put("11-19", Collections.nCopies(1, "日光菩萨圣诞"));
put("12-8", Collections.nCopies(1, "释迦牟尼佛成道"));
put("12-23", Collections.nCopies(1, "监斋菩萨圣诞"));
put("12-29", Collections.nCopies(1, "华严菩萨圣诞"));
}
};

/**
* 27星宿,佛教从印度传入中国,印度把28星宿改为27星宿,把牛宿(牛金牛)纳入了女宿(女土蝠)。
*/
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class LunarUtil{
/** 日 */
public static final String[] DAY = {"","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
/** 月相,朔月也叫新月,望月也叫满月 */
public static final String[] YUE_XIANG = {"","朔","既朔","蛾眉新","蛾眉新","蛾眉","夕月","上弦","上弦","九夜","宵","宵","宵","渐盈凸","小望","望","既望","立待","居待","寝待","更待","渐亏凸","下弦","下弦","有明","有明","蛾眉残","蛾眉残","残","晓","晦"};
public static final String[] YUE_XIANG = {"","朔","既朔","蛾眉新","蛾眉新","蛾眉","","上弦","上弦","九夜","宵","宵","宵","渐盈凸","小望","望","既望","立待","居待","寝待","更待","渐亏凸","下弦","下弦","有明","有明","蛾眉残","蛾眉残","残","晓","晦"};
/** 农历日期对应的节日 */
public static final Map<String,String> FESTIVAL = new HashMap<String,String>(){
private static final long serialVersionUID = -1;
Expand Down Expand Up @@ -1118,8 +1118,7 @@ public static List<String> getDayYi(String monthGanZhi,String dayGanZhi){
boolean matched = false;
String months = left.substring(0, left.indexOf(":"));
for (int i = 0, j = months.length(); i < j; i += 2) {
String m = months.substring(i, i + 2);
if (m.equals(month)) {
if (month.equals(months.substring(i, i + 2))) {
matched = true;
break;
}
Expand All @@ -1128,8 +1127,7 @@ public static List<String> getDayYi(String monthGanZhi,String dayGanZhi){
String ys = left.substring(left.indexOf(":") + 1);
ys = ys.substring(0, ys.indexOf(","));
for (int i = 0, j = ys.length(); i < j; i += 2) {
String m = ys.substring(i, i + 2);
l.add(YI_JI[Integer.parseInt(m,16)]);
l.add(YI_JI[Integer.parseInt(ys.substring(i, i + 2),16)]);
}
break;
}
Expand Down
Loading

0 comments on commit 3ef8d0c

Please sign in to comment.