1
0
mirror of synced 2026-10-24 15:36:11 +08:00

v1.2.19 新增阴历年、阴历月的推移;修复三候错误;修复道历节日错别字。

This commit is contained in:
6tail
2022-02-16 20:11:13 +08:00
parent 0a5ea27b20
commit 1a15531fb4
9 changed files with 249 additions and 163 deletions

View File

@@ -2,6 +2,8 @@ package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
import java.util.List;
/**
* 农历月
*
@@ -31,9 +33,10 @@ public class LunarMonth {
/**
* 初始化
* @param lunarYear 农历年
* @param lunarMonth 农历月:1-12,闰月为负数,如闰2月为-2
* @param dayCount 天数
*
* @param lunarYear 农历年
* @param lunarMonth 农历月:1-12,闰月为负数,如闰2月为-2
* @param dayCount 天数
* @param firstJulianDay 初一的儒略日
*/
public LunarMonth(int lunarYear, int lunarMonth, int dayCount, double firstJulianDay) {
@@ -45,16 +48,18 @@ public class LunarMonth {
/**
* 通过农历年月初始化
* @param lunarYear 农历年
*
* @param lunarYear 农历年
* @param lunarMonth 农历月:1-12,闰月为负数,如闰2月为-2
* @return 农历月
*/
public static LunarMonth fromYm(int lunarYear, int lunarMonth){
public static LunarMonth fromYm(int lunarYear, int lunarMonth) {
return LunarYear.fromYear(lunarYear).getMonth(lunarMonth);
}
/**
* 获取农历年
*
* @return 农历年
*/
public int getYear() {
@@ -63,6 +68,7 @@ public class LunarMonth {
/**
* 获取农历月
*
* @return 农历月:1-12,闰月为负数,如闰2月为-2
*/
public int getMonth() {
@@ -71,6 +77,7 @@ public class LunarMonth {
/**
* 是否闰月
*
* @return true/false
*/
public boolean isLeap() {
@@ -79,6 +86,7 @@ public class LunarMonth {
/**
* 获取天数
*
* @return 天数
*/
public int getDayCount() {
@@ -87,6 +95,7 @@ public class LunarMonth {
/**
* 获取初一的儒略日
*
* @return 初一的儒略日
*/
public double getFirstJulianDay() {
@@ -96,27 +105,21 @@ public class LunarMonth {
/**
* 获取太岁方位
*
* @return 太岁方位,如艮
* @return 方位,如艮
*/
public String getPositionTaiSui() {
String p;
int m = Math.abs(month);
switch(m) {
int m = Math.abs(month) % 4;
switch (m) {
case 0:
p = "巽";
break;
case 1:
case 5:
case 9:
p = "艮";
break;
case 3:
case 7:
case 11:
p = "坤";
break;
case 4:
case 8:
case 12:
p = "巽";
break;
default:
p = LunarUtil.POSITION_GAN[Solar.fromJulianDay(this.getFirstJulianDay()).getLunar().getMonthGanIndex()];
}
@@ -126,7 +129,7 @@ public class LunarMonth {
/**
* 获取太岁方位描述
*
* @return 太岁方位描述,如东北
* @return 方位描述,如东北
*/
public String getPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
@@ -141,7 +144,7 @@ public class LunarMonth {
int index = LunarYear.fromYear(year).getZhiIndex() % 3;
int m = Math.abs(month);
int monthZhiIndex = (13 + m) % 12;
int n = 27 - (index * 3);
int n = 27 - index * 3;
if (monthZhiIndex < LunarUtil.BASE_MONTH_ZHI_INDEX) {
n -= 3;
}
@@ -153,4 +156,72 @@ public class LunarMonth {
public String toString() {
return year + "年" + (isLeap() ? "闰" : "") + LunarUtil.MONTH[Math.abs(month)] + "月(" + dayCount + "天)";
}
/**
* 获取往后推几个月的阴历月,如果要往前推,则月数用负数
*
* @param n 月数
* @return 阴历月
*/
public LunarMonth next(int n) {
if (0 == n) {
return LunarMonth.fromYm(year, month);
} else if (n > 0) {
int rest = n;
int ny = year;
int iy = ny;
int im = month;
int index = 0;
List<LunarMonth> months = LunarYear.fromYear(ny).getMonths();
while (true) {
int size = months.size();
for (int i = 0; i < size; i++) {
LunarMonth m = months.get(i);
if (m.getYear() == iy && m.getMonth() == im) {
index = i;
break;
}
}
int more = size - index - 1;
if (rest < more) {
break;
}
rest -= more;
LunarMonth lastMonth = months.get(size - 1);
iy = lastMonth.getYear();
im = lastMonth.getMonth();
ny++;
months = LunarYear.fromYear(ny).getMonths();
}
return months.get(index + rest);
} else {
int rest = -n;
int ny = year;
int iy = ny;
int im = month;
int index = 0;
List<LunarMonth> months = LunarYear.fromYear(ny).getMonths();
while (true) {
int size = months.size();
for (int i = 0; i < size; i++) {
LunarMonth m = months.get(i);
if (m.getYear() == iy && m.getMonth() == im) {
index = i;
break;
}
}
if (rest <= index) {
break;
}
rest -= index;
LunarMonth firstMonth = months.get(0);
iy = firstMonth.getYear();
im = firstMonth.getMonth();
ny--;
months = LunarYear.fromYear(ny).getMonths();
}
return months.get(index - rest);
}
}
}