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

Compare commits

...

6 Commits

32 changed files with 436 additions and 161 deletions

View File

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

View File

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

View File

@@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.2.19</version>
<version>1.2.23</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>

View File

@@ -474,6 +474,26 @@ public class EightChar {
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;
}
}
int zhiIndex = (2 + (monthZhiIndex + timeZhiIndex)) % 12;
int zhiIndex = 2 + monthZhiIndex + timeZhiIndex;
if (zhiIndex > 12) {
zhiIndex -= 12;
}
int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex - zhiIndex);
if (jiaZiIndex >= 60) {
jiaZiIndex -= 60;
@@ -556,13 +579,24 @@ public class EightChar {
}
/**
* 获取运
* 使用默认流派1获取运
*
* @param gender 性别:1男,0女
* @return 运
*/
public Yun getYun(int gender) {
return new Yun(this, gender);
return getYun(gender, 1);
}
/**
* 获取运
*
* @param gender 性别:1男,0女
* @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
* @return 运
*/
public Yun getYun(int gender, int sect) {
return new Yun(this, gender, sect);
}
/**

View File

@@ -18,6 +18,7 @@ public class ExactDate {
*/
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT+8");
@SuppressWarnings("MagicConstant")
public static Calendar fromYmdHms(int year, int month, int day, int hour, int minute, int second) {
Calendar c = Calendar.getInstance(TIME_ZONE);
c.set(year, month - 1, day, hour, minute, second);

View File

@@ -18,7 +18,7 @@ public class Foto {
/**
* 阴历
*/
private Lunar lunar;
private final Lunar lunar;
public Foto(Lunar lunar) {
this.lunar = lunar;
@@ -228,7 +228,7 @@ public class Foto {
public String toFullString() {
StringBuilder s = new StringBuilder();
s.append(toString());
s.append(this);
for (FotoFestival f : getFestivals()) {
s.append(" (");
s.append(f);

View File

@@ -10,22 +10,22 @@ public class FotoFestival {
/**
* 是日何日,如:雷斋日
*/
private String name;
private final String name;
/**
* 犯之因果,如:犯者夺纪
*/
private String result;
private final String result;
/**
* 是否每月同
*/
private boolean everyMonth;
private final boolean everyMonth;
/**
* 备注,如:宜先一日即戒
*/
private String remark;
private final String remark;
public FotoFestival(String name, String result, boolean everyMonth, String remark) {
this.name = name;

View File

@@ -35,7 +35,7 @@ public class Lunar {
/**
* 对应阳历
*/
private Solar solar;
private final Solar solar;
/**
* 时对应的天干下标,0-9
*/
@@ -116,15 +116,15 @@ public class Lunar {
/**
* 阳历小时
*/
private int hour;
private final int hour;
/**
* 阳历分钟
*/
private int minute;
private final int minute;
/**
* 阳历秒钟
*/
private int second;
private final int second;
/**
* 八字
*/
@@ -132,7 +132,7 @@ public class Lunar {
/**
* 24节气表(对应阳历的准确时刻)
*/
private Map<String, Solar> jieQi = new LinkedHashMap<String, Solar>();
private final Map<String, Solar> jieQi = new LinkedHashMap<String, Solar>();
/**
* 默认使用当前日期初始化
@@ -191,7 +191,6 @@ public class Lunar {
*
* @param date 阳历日期
*/
@SuppressWarnings("MagicConstant")
public Lunar(Date date) {
solar = new Solar(date);
int currentYear = solar.getYear();
@@ -968,9 +967,27 @@ public class Lunar {
if (null != fs) {
l.addAll(fs);
}
if (solar.toYmd().equals(jieQi.get("清明").next(-1).toYmd())) {
String solarYmd = solar.toYmd();
if (solarYmd.equals(jieQi.get("清明").next(-1).toYmd())) {
l.add("寒食节");
}
Solar jq = jieQi.get("立春");
int offset = 4 - jq.getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10;
}
if (solarYmd.equals(jq.next(offset + 40).toYmd())) {
l.add("春社");
}
jq = jieQi.get("立秋");
offset = 4 - jq.getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10;
}
if (solarYmd.equals(jq.next(offset + 40).toYmd())) {
l.add("秋社");
}
return l;
}
@@ -1292,14 +1309,12 @@ public class Lunar {
public String getMonthPositionTaiSui(int sect) {
int monthZhiIndex;
int monthGanIndex;
switch (sect) {
case 3:
monthZhiIndex = this.monthZhiIndexExact;
monthGanIndex = this.monthGanIndexExact;
break;
default:
monthZhiIndex = this.monthZhiIndex;
monthGanIndex = this.monthGanIndex;
if (sect == 3) {
monthZhiIndex = this.monthZhiIndexExact;
monthGanIndex = this.monthGanIndexExact;
} else {
monthZhiIndex = this.monthZhiIndex;
monthGanIndex = this.monthGanIndex;
}
return getMonthPositionTaiSui(monthZhiIndex, monthGanIndex);
}
@@ -1828,21 +1843,41 @@ public class Lunar {
}
/**
* 获取每日宜,如果没有,返回["无"]
* 使用默认流派1(以节交接当天起算月)获取每日宜,如果没有,返回["无"]
*
* @return 宜
*/
public List<String> getDayYi() {
return LunarUtil.getDayYi(getMonthInGanZhiExact(), getDayInGanZhi());
return getDayYi(1);
}
/**
* 获取每日宜,如果没有,返回["无"]
*
* @param sect 流派,1以节交接当天起算月,2以节交接时刻起算月
* @return 宜
*/
public List<String> getDayYi(int sect) {
return LunarUtil.getDayYi(2 == sect ? getMonthInGanZhiExact() : getMonthInGanZhi(), getDayInGanZhi());
}
/**
* 使用默认流派1(以节交接当天起算月)获取每日忌,如果没有,返回["无"]
*
* @return 忌
*/
public List<String> getDayJi() {
return getDayJi(1);
}
/**
* 获取每日忌,如果没有,返回["无"]
*
* @param sect 流派,1以节交接当天起算月,2以节交接时刻起算月
* @return 忌
*/
public List<String> getDayJi() {
return LunarUtil.getDayJi(getMonthInGanZhiExact(), getDayInGanZhi());
public List<String> getDayJi(int sect) {
return LunarUtil.getDayJi(2 == sect ? getMonthInGanZhiExact() : getMonthInGanZhi(), getDayInGanZhi());
}
/**
@@ -1869,7 +1904,7 @@ public class Lunar {
* @return 日冲,如申
*/
public String getDayChong() {
return LunarUtil.CHONG[dayZhiIndex + 1];
return LunarUtil.CHONG[dayZhiIndex];
}
/**
@@ -1911,7 +1946,7 @@ public class Lunar {
* @return 无情之克的日冲天干,如甲
*/
public String getDayChongGan() {
return LunarUtil.CHONG_GAN[dayGanIndex + 1];
return LunarUtil.CHONG_GAN[dayGanIndex];
}
/**
@@ -1920,7 +1955,7 @@ public class Lunar {
* @return 有情之克的日冲天干,如甲
*/
public String getDayChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[dayGanIndex + 1];
return LunarUtil.CHONG_GAN_TIE[dayGanIndex];
}
/**
@@ -1929,7 +1964,7 @@ public class Lunar {
* @return 时冲,如申
*/
public String getTimeChong() {
return LunarUtil.CHONG[timeZhiIndex + 1];
return LunarUtil.CHONG[timeZhiIndex];
}
/**
@@ -1971,7 +2006,7 @@ public class Lunar {
* @return 无情之克的时冲天干,如甲
*/
public String getTimeChongGan() {
return LunarUtil.CHONG_GAN[timeGanIndex + 1];
return LunarUtil.CHONG_GAN[timeGanIndex];
}
/**
@@ -1980,7 +2015,7 @@ public class Lunar {
* @return 有情之克的时冲天干,如甲
*/
public String getTimeChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[timeGanIndex + 1];
return LunarUtil.CHONG_GAN_TIE[timeGanIndex];
}
/**
@@ -2438,7 +2473,7 @@ public class Lunar {
public String toFullString() {
StringBuilder s = new StringBuilder();
s.append(toString());
s.append(this);
s.append(" ");
s.append(getYearInGanZhi());
s.append("(");
@@ -2842,7 +2877,6 @@ public class Lunar {
*
* @return 数九,如果不是数九天,返回null
*/
@SuppressWarnings("MagicConstant")
public ShuJiu getShuJiu() {
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
Solar start = jieQi.get("DONG_ZHI");
@@ -2869,7 +2903,6 @@ public class Lunar {
*
* @return 三伏,如果不是伏天,返回null
*/
@SuppressWarnings("MagicConstant")
public Fu getFu() {
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
Solar xiaZhi = jieQi.get("夏至");
@@ -2942,7 +2975,6 @@ public class Lunar {
*
* @return 物候
*/
@SuppressWarnings("MagicConstant")
public String getWuHou() {
JieQi jieQi = getPrevJieQi(true);
String name = jieQi.getName();

View File

@@ -14,22 +14,22 @@ public class LunarMonth {
/**
* 农历年
*/
private int year;
private final int year;
/**
* 农历月:1-12,闰月为负数,如闰2月为-2
*/
private int month;
private final int month;
/**
* 天数,大月30天,小月29天
*/
private int dayCount;
private final int dayCount;
/**
* 初一的儒略日
*/
private double firstJulianDay;
private final double firstJulianDay;
/**
* 初始化

View File

@@ -15,17 +15,17 @@ public class LunarTime {
/**
* 天干下标,0-9
*/
private int ganIndex;
private final int ganIndex;
/**
* 地支下标,0-11
*/
private int zhiIndex;
private final int zhiIndex;
/**
* 阴历
*/
private Lunar lunar;
private final Lunar lunar;
public LunarTime(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second) {
this.lunar = Lunar.fromYmdHms(lunarYear, lunarMonth, lunarDay, hour, minute, second);
@@ -235,7 +235,7 @@ public class LunarTime {
* @return 时冲,如申
*/
public String getChong() {
return LunarUtil.CHONG[zhiIndex + 1];
return LunarUtil.CHONG[zhiIndex];
}
/**
@@ -277,7 +277,7 @@ public class LunarTime {
* @return 无情之克的时冲天干,如甲
*/
public String getChongGan() {
return LunarUtil.CHONG_GAN[ganIndex + 1];
return LunarUtil.CHONG_GAN[ganIndex];
}
/**
@@ -286,7 +286,7 @@ public class LunarTime {
* @return 有情之克的时冲天干,如甲
*/
public String getChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[ganIndex + 1];
return LunarUtil.CHONG_GAN_TIE[ganIndex];
}
/**
@@ -316,10 +316,7 @@ public class LunarTime {
//顺逆
String solarYmd = lunar.getSolar().toYmd();
Map<String, Solar> jieQi = lunar.getJieQiTable();
boolean asc = false;
if (solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0) {
asc = true;
}
boolean asc = solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0;
int start = asc ? 7 : 3;
String dayZhi = lunar.getDayZhi();
if ("子午卯酉".contains(dayZhi)) {

View File

@@ -50,27 +50,27 @@ public class LunarYear {
/**
* 农历年
*/
private int year;
private final int year;
/**
* 天干下标
*/
private int ganIndex;
private final int ganIndex;
/**
* 地支下标
*/
private int zhiIndex;
private final int zhiIndex;
/**
* 农历月们
*/
private List<LunarMonth> months = new ArrayList<LunarMonth>();
private final List<LunarMonth> months = new ArrayList<LunarMonth>();
/**
* 节气儒略日们
*/
private List<Double> jieQiJulianDays = new ArrayList<Double>();
private final List<Double> jieQiJulianDays = new ArrayList<Double>();
/**
* 初始化

View File

@@ -19,34 +19,41 @@ public class Solar {
* 2000年儒略日数(2000-1-1 12:00:00 UTC)
*/
public static final double J2000 = 2451545;
/**
* 年
*/
private int year;
private final int year;
/**
* 月
*/
private int month;
private final int month;
/**
* 日
*/
private int day;
private final int day;
/**
* 时
*/
private int hour;
private final int hour;
/**
* 分
*/
private int minute;
private final int minute;
/**
* 秒
*/
private int second;
private final int second;
/**
* 日历
*/
private Calendar calendar;
private final Calendar calendar;
/**
* 默认使用当前日期初始化
@@ -76,7 +83,6 @@ public class Solar {
* @param minute 分钟,0到59
* @param second 秒钟,0到59
*/
@SuppressWarnings("MagicConstant")
public Solar(int year, int month, int day, int hour, int minute, int second) {
calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
this.year = year;
@@ -123,7 +129,6 @@ public class Solar {
*
* @param julianDay 儒略日
*/
@SuppressWarnings("MagicConstant")
public Solar(double julianDay) {
int d = (int) (julianDay + 0.5);
double f = julianDay + 0.5 - d;
@@ -381,6 +386,12 @@ public class Solar {
if (null != f) {
l.add(f);
}
if (day + 7 > SolarUtil.getDaysOfMonth(year, month)) {
f = SolarUtil.WEEK_FESTIVAL.get(month + "-0-" + week);
if (null != f) {
l.add(f);
}
}
return l;
}
@@ -515,10 +526,7 @@ public class Solar {
int m = this.month;
double d = this.day + ((this.second * 1D / 60 + this.minute) / 60 + this.hour) / 24;
int n = 0;
boolean g = false;
if (y * 372 + m * 31 + (int) d >= 588829) {
g = true;
}
boolean g = y * 372 + m * 31 + (int) d >= 588829;
if (m <= 2) {
m += 12;
y--;
@@ -595,7 +603,6 @@ public class Solar {
* @param onlyWorkday 是否仅限工作日
* @return 阳历日期
*/
@SuppressWarnings("MagicConstant")
public Solar next(int days, boolean onlyWorkday) {
Calendar c = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
if (0 != days) {

View File

@@ -14,11 +14,11 @@ public class SolarHalfYear {
/**
* 年
*/
private int year;
private final int year;
/**
* 月
*/
private int month;
private final int month;
/**
* 半年的月数
@@ -124,7 +124,6 @@ public class SolarHalfYear {
* @param halfYears 推移的半年数,负数为倒推
* @return 推移后的半年
*/
@SuppressWarnings("MagicConstant")
public SolarHalfYear next(int halfYears) {
if (0 == halfYears) {
return new SolarHalfYear(year, month);

View File

@@ -16,11 +16,11 @@ public class SolarMonth {
/**
* 年
*/
private int year;
private final int year;
/**
* 月
*/
private int month;
private final int month;
/**
* 默认当月
@@ -128,7 +128,6 @@ public class SolarMonth {
* @param months 月数
* @return 阳历月
*/
@SuppressWarnings("MagicConstant")
public SolarMonth next(int months) {
Calendar c = ExactDate.fromYmd(year, month, 1);
c.add(Calendar.MONTH, months);

View File

@@ -14,11 +14,13 @@ public class SolarSeason {
/**
* 年
*/
private int year;
private final int year;
/**
* 月
*/
private int month;
private final int month;
/**
* 一个季度的月数
*/

View File

@@ -16,19 +16,22 @@ public class SolarWeek {
/**
* 年
*/
private int year;
private final int year;
/**
* 月
*/
private int month;
private final int month;
/**
* 日
*/
private int day;
private final int day;
/**
* 星期几作为一周的开始,1234560分别代表星期一至星期天
*/
private int start;
private final int start;
/**
* 默认当月
@@ -155,7 +158,6 @@ public class SolarWeek {
*
* @return 周序号,从1开始
*/
@SuppressWarnings("MagicConstant")
public int getIndex() {
Calendar c = ExactDate.fromYmd(year, month, 1);
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -166,6 +168,21 @@ public class SolarWeek {
return (int) Math.ceil((day + offset) / 7D);
}
/**
* 获取当前日期是在当年第几周
*
* @return 周序号,从1开始
*/
public int getIndexInYear() {
Calendar c = ExactDate.fromYmd(year, 1, 1);
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
int offset = firstDayWeek - start;
if(offset < 0) {
offset += 7;
}
return (int) Math.ceil((SolarUtil.getDaysInYear(year, month, day) + offset) / 7D);
}
/**
* 周推移
*
@@ -173,7 +190,6 @@ public class SolarWeek {
* @param separateMonth 是否按月单独计算
* @return 推移后的阳历周
*/
@SuppressWarnings("MagicConstant")
public SolarWeek next(int weeks, boolean separateMonth) {
if (0 == weeks) {
return new SolarWeek(year, month, day, start);
@@ -228,7 +244,6 @@ public class SolarWeek {
*
* @return 本周第一天的阳历日期
*/
@SuppressWarnings("MagicConstant")
public Solar getFirstDay() {
Calendar c = ExactDate.fromYmd(year, month, day);
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -260,6 +275,7 @@ public class SolarWeek {
*
* @return 本周的阳历日期列表
*/
@SuppressWarnings("all")
public List<Solar> getDays() {
Solar firstDay = getFirstDay();
List<Solar> l = new ArrayList<Solar>();

View File

@@ -14,7 +14,7 @@ public class SolarYear {
/**
* 年
*/
private int year;
private final int year;
/**
* 一年的月数

View File

@@ -18,7 +18,7 @@ public class Tao {
/**
* 阴历
*/
private Lunar lunar;
private final Lunar lunar;
public Tao(Lunar lunar) {
this.lunar = lunar;

View File

@@ -10,12 +10,12 @@ public class TaoFestival {
/**
* 名称
*/
private String name;
private final String name;
/**
* 备注
*/
private String remark;
private final String remark;
public TaoFestival(String name, String remark) {
this.name = name;

View File

@@ -12,28 +12,34 @@ public class DaYun {
/**
* 开始年(含)
*/
private int startYear;
private final int startYear;
/**
* 结束年(含)
*/
private int endYear;
private final int endYear;
/**
* 开始年龄(含)
*/
private int startAge;
private final int startAge;
/**
* 结束年龄(含)
*/
private int endAge;
private final int endAge;
/**
* 序数,0-9
*/
private int index;
private final int index;
/**
* 运
*/
private Yun yun;
private Lunar lunar;
private final Yun yun;
private final Lunar lunar;
public DaYun(Yun yun, int index) {
this.yun = yun;

View File

@@ -12,20 +12,24 @@ public class LiuNian {
/**
* 序数,0-9
*/
private int index;
private final int index;
/**
* 大运
*/
private DaYun daYun;
private final DaYun daYun;
/**
* 年
*/
private int year;
private final int year;
/**
* 年龄
*/
private int age;
private Lunar lunar;
private final int age;
private final Lunar lunar;
public LiuNian(DaYun daYun, int index) {
this.daYun = daYun;

View File

@@ -11,8 +11,9 @@ public class LiuYue {
/**
* 序数,0-9
*/
private int index;
private LiuNian liuNian;
private final int index;
private final LiuNian liuNian;
public LiuYue(LiuNian liuNian, int index) {
this.liuNian = liuNian;

View File

@@ -12,24 +12,29 @@ public class XiaoYun {
/**
* 序数,0-9
*/
private int index;
private final int index;
/**
* 大运
*/
private DaYun daYun;
private final DaYun daYun;
/**
* 年
*/
private int year;
private final int year;
/**
* 年龄
*/
private int age;
private final int age;
/**
* 是否顺推
*/
private boolean forward;
private Lunar lunar;
private final boolean forward;
private final Lunar lunar;
public XiaoYun(DaYun daYun, int index, boolean forward) {
this.daYun = daYun;

View File

@@ -14,26 +14,51 @@ public class Yun {
/**
* 性别(1男,0女)
*/
private int gender;
private final int gender;
/**
* 起运年数
*/
private int startYear;
/**
* 起运月数
*/
private int startMonth;
/**
* 起运天数
*/
private int startDay;
/**
* 起运小时数
*/
private int startHour;
/**
* 是否顺推
*/
private boolean forward;
private Lunar lunar;
private final boolean forward;
private final Lunar lunar;
/**
* 使用默认流派1初始化运
* @param eightChar 八字
* @param gender 性别,1男,0女
*/
public Yun(EightChar eightChar, int gender) {
this(eightChar, gender, 1);
}
/**
* 初始化运
* @param eightChar 八字
* @param gender 性别,1男,0女
* @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
*/
public Yun(EightChar eightChar, int gender, int sect) {
this.lunar = eightChar.getLunar();
this.gender = gender;
// 阳
@@ -41,14 +66,13 @@ public class Yun {
// 男
boolean man = 1 == gender;
forward = (yang && man) || (!yang && !man);
computeStart();
computeStart(sect);
}
/**
* 起运计算
*/
@SuppressWarnings("MagicConstant")
private void computeStart() {
private void computeStart(int sect) {
// 上节
JieQi prev = lunar.getPrevJie();
// 下节
@@ -58,24 +82,46 @@ public class Yun {
// 阳男阴女顺推,阴男阳女逆推
Solar start = forward ? current : prev.getSolar();
Solar end = forward ? next.getSolar() : current;
int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
// 时辰差
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
// 天数差
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;
int year;
int month;
int day;
int hour = 0;
if (2 == sect) {
long minutes = (end.getCalendar().getTimeInMillis() - start.getCalendar().getTimeInMillis()) / 60000;
long y = minutes / 4320;
minutes -= y * 4320;
long m = minutes / 360;
minutes -= m * 360;
long d = minutes / 12;
minutes -= d * 12;
long h = minutes * 2;
year = (int)y;
month = (int)m;
day = (int)d;
hour = (int)h;
} else {
int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
// 时辰差
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
// 天数差
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;
}
int monthDiff = hourDiff * 10 / 30;
month = dayDiff * 4 + monthDiff;
day = hourDiff * 10 - monthDiff * 30;
year = month / 12;
month = month - year * 12;
}
int monthDiff = hourDiff * 10 / 30;
int month = dayDiff * 4 + monthDiff;
int day = hourDiff * 10 - monthDiff * 30;
int year = month / 12;
month = month - year * 12;
this.startYear = year;
this.startMonth = month;
this.startDay = day;
this.startHour = hour;
}
/**
@@ -114,6 +160,15 @@ public class Yun {
return startDay;
}
/**
* 获取起运小时数
*
* @return 起运小时数
*/
public int getStartHour() {
return startHour;
}
/**
* 是否顺推
*
@@ -132,13 +187,13 @@ public class Yun {
*
* @return 阳历日期
*/
@SuppressWarnings("MagicConstant")
public Solar getStartSolar() {
Solar birth = lunar.getSolar();
Calendar c = ExactDate.fromYmd(birth.getYear(), birth.getMonth(), birth.getDay());
Calendar c = ExactDate.fromYmdHms(birth.getYear(), birth.getMonth(), birth.getDay(), birth.getHour(), birth.getMinute(), birth.getSecond());
c.add(Calendar.YEAR, startYear);
c.add(Calendar.MONTH, startMonth);
c.add(Calendar.DATE, startDay);
c.add(Calendar.HOUR, startHour);
return Solar.fromCalendar(c);
}

View File

@@ -152,7 +152,7 @@ public class LunarUtil{
put("1-25",Collections.nCopies(1,"填仓节"));
put("1-30",Collections.nCopies(1,"正月晦"));
put("2-1",Collections.nCopies(1,"中和节"));
put("2-2",Collections.nCopies(1,"春社"));
put("2-2",Collections.nCopies(1,"社日节"));
put("3-3",Collections.nCopies(1,"上巳节"));
put("5-20",Collections.nCopies(1,"分龙节"));
put("5-25",Collections.nCopies(1,"会龙节"));
@@ -351,32 +351,17 @@ public class LunarUtil{
}
};
/** 地支相冲(子午相冲,丑未相冲,寅申相冲,辰戌相冲,卯酉相冲,巳亥相冲),由于地支对应十二生肖,也就对应了生肖相冲 */
public static final String[] CHONG = {"","午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳"};
public static final String[] CHONG = {"午","未","申","酉","戌","亥","子","丑","寅","卯","辰","巳"};
/** 天干相冲之无情之克(阳克阳,阴克阴) */
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 = {"戊","己","庚","辛","壬","癸","甲","乙","丙","丁"};
/** 天干相冲之有情之克(阳克阴,阴克阳) */
public static final String[] CHONG_GAN_TIE = {"","己","戊","辛","庚","癸","壬","乙","甲","丁","丙"};
/** 天干五合(有情之克中最有情的5个) */
public static final Map<String,String> CHONG_GAN_TIE_GOOD = new HashMap<String,String>(){
private static final long serialVersionUID = -1;
{
put("甲","己");
put("丙","辛");
put("戊","癸");
put("庚","乙");
put("壬","丁");
}
};
public static final String[] CHONG_GAN_TIE = {"己","戊","辛","庚","癸","壬","乙","甲","丁","丙"};
/** 天干四冲(无情之克中克得最严重的4个) */
public static final String[] CHONG_GAN_4 = {"庚","辛","壬","癸","","","甲","乙","丙","丁"};
/** 天干五合(有情之克中最有情的5个,甲己合,乙庚合,丙辛合,丁壬合,戊癸合) */
public static final String[] HE_GAN_5 = {"己","庚","辛","壬","癸","甲","乙","丙","丁","戊"};
/** 地支六合(子丑合,寅亥合,卯戌合,辰酉合,巳申合,午未合) */
public static final String[] HE_ZHI_6 = {"丑","子","亥","戌","酉","申","未","午","巳","辰","卯","寅"};
/** 煞(逢巳日、酉日、丑日必煞东;亥日、卯日、未日必煞西;申日、子日、辰日必煞南;寅日、午日、戌日必煞北) */
public static final Map<String,String> SHA = new HashMap<String,String>(){
private static final long serialVersionUID = -1;

View File

@@ -42,6 +42,8 @@ public class SolarUtil {
put("8-1", "建军节");
put("9-10", "教师节");
put("10-1", "国庆节");
put("10-31", "万圣节前夜");
put("11-1", "万圣节");
put("12-24", "平安夜");
put("12-25", "圣诞节");
}
@@ -53,6 +55,7 @@ public class SolarUtil {
private static final long serialVersionUID = -1;
{
put("3-0-1", "全国中小学生安全教育日");
put("5-2-0", "母亲节");
put("6-3-0", "父亲节");
put("11-4-4", "感恩节");
@@ -97,6 +100,7 @@ public class SolarUtil {
put("5-5", Collections.nCopies(1, "马克思诞辰纪念日"));
put("5-8", Collections.nCopies(1, "世界红十字日"));
put("5-11", Collections.nCopies(1, "世界肥胖日"));
put("5-25", Collections.nCopies(1, "525心理健康节"));
put("5-27", Collections.nCopies(1, "上海解放日"));
put("5-31", Collections.nCopies(1, "世界无烟日"));
put("6-5", Collections.nCopies(1, "世界环境日"));

View File

@@ -230,6 +230,24 @@ public class BaZiTest {
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
public void testBaZi2Solar() {
Solar solar = Solar.fromYmdHms(2027,1,27,12,0,0);

View File

@@ -333,4 +333,22 @@ public class GanZhiTest {
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
}
@Test
public void test19() {
Solar solar = new Solar(2022,4,6,20,18,0);
Lunar lunar = solar.getLunar();
Assert.assertEquals("壬寅",lunar.getYearInGanZhi());
Assert.assertEquals("壬寅",lunar.getYearInGanZhiByLiChun());
Assert.assertEquals("壬寅",lunar.getYearInGanZhiExact());
Assert.assertEquals("甲辰",lunar.getMonthInGanZhi());
Assert.assertEquals("甲辰",lunar.getMonthInGanZhiExact());
Assert.assertEquals("己丑",lunar.getDayInGanZhi());
Assert.assertEquals("己丑",lunar.getDayInGanZhiExact());
Assert.assertEquals("己丑",lunar.getDayInGanZhiExact2());
Assert.assertEquals("甲戌",lunar.getTimeInGanZhi());
}
}

View File

@@ -415,4 +415,48 @@ public class LunarTest {
Assert.assertEquals("一九八六年七月初九", lunar.toString());
}
@Test
public void test56() {
Solar solar = new Solar(2022, 4, 5);
Lunar lunar = solar.getLunar();
Assert.assertEquals("甲辰", lunar.getMonthInGanZhi());
Assert.assertEquals("癸卯", lunar.getMonthInGanZhiExact());
Assert.assertEquals(4, lunar.getDayJi().size());
}
@Test
public void test57() {
Solar solar = new Solar(1991, 2, 5);
Lunar lunar = solar.getLunar();
Assert.assertEquals("庚寅", lunar.getMonthInGanZhi());
}
@Test
public void test58() {
Solar solar = new Solar(2021, 3, 21);
Lunar lunar = solar.getLunar();
Assert.assertEquals("春社", lunar.getOtherFestivals().get(0));
}
@Test
public void test59() {
Solar solar = new Solar(2022, 3, 16);
Lunar lunar = solar.getLunar();
Assert.assertEquals("春社", lunar.getOtherFestivals().get(0));
}
@Test
public void test60() {
Solar solar = new Solar(1722, 9, 25);
Lunar lunar = solar.getLunar();
Assert.assertEquals("秋社", lunar.getOtherFestivals().get(0));
}
@Test
public void test61() {
Solar solar = new Solar(840, 9, 14);
Lunar lunar = solar.getLunar();
Assert.assertEquals("秋社", lunar.getOtherFestivals().get(0));
}
}

View File

@@ -47,7 +47,7 @@ public class SolarTest {
@Test
public void test10(){
Assert.assertEquals(false, SolarUtil.isLeapYear(1500));
Assert.assertFalse(SolarUtil.isLeapYear(1500));
}
@Test
@@ -73,4 +73,10 @@ public class SolarTest {
Assert.assertEquals("2020-01-19",date.next(1,true).toString());
}
@Test
public void test11(){
Solar solar = new Solar(2022, 3, 28);
Assert.assertEquals("全国中小学生安全教育日",solar.getFestivals().get(0));
}
}

View File

@@ -71,4 +71,16 @@ public class SolarWeekTest {
Assert.assertEquals(2, week.getIndex());
}
@Test
public void test10() {
SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 0);
Assert.assertEquals(11, week.getIndexInYear());
}
@Test
public void test11() {
SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 1);
Assert.assertEquals(10, week.getIndexInYear());
}
}

View File

@@ -53,4 +53,34 @@ public class YunTest {
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
}
@Test
public void test4() {
Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
Lunar lunar = solar.getLunar();
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(1);
Assert.assertEquals("起运阳历", "2030-12-19", yun.getStartSolar().toYmd());
}
@Test
public void test5() {
Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
Lunar lunar = solar.getLunar();
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(1, 2);
Assert.assertEquals("起运年数", 8, yun.getStartYear());
Assert.assertEquals("起运月数", 9, yun.getStartMonth());
Assert.assertEquals("起运天数", 2, yun.getStartDay());
Assert.assertEquals("起运阳历", "2030-12-12", yun.getStartSolar().toYmd());
}
@Test
public void test6() {
Solar solar = new Solar(2018, 6, 11, 9, 30, 0);
Lunar lunar = solar.getLunar();
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(0, 2);
Assert.assertEquals("起运阳历", "2020-03-21", yun.getStartSolar().toYmd());
}
}