1
0
mirror of synced 2025-12-28 08:17:59 +08:00

v1.2.20 支持获取当年第几周;支持2种流派获取起运。

This commit is contained in:
6tail
2022-03-12 20:05:14 +08:00
parent 1a15531fb4
commit 249aa36176
8 changed files with 148 additions and 26 deletions

View File

@@ -556,13 +556,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

@@ -155,7 +155,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 +165,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);
}
/**
* 周推移
*
@@ -228,7 +242,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 +273,7 @@ public class SolarWeek {
*
* @return 本周的阳历日期列表
*/
@SuppressWarnings("all")
public List<Solar> getDays() {
Solar firstDay = getFirstDay();
List<Solar> l = new ArrayList<Solar>();

View File

@@ -15,25 +15,50 @@ public class Yun {
* 性别(1男0女)
*/
private int gender;
/**
* 起运年数
*/
private int startYear;
/**
* 起运月数
*/
private int startMonth;
/**
* 起运天数
*/
private int startDay;
/**
* 起运小时数
*/
private int startHour;
/**
* 是否顺推
*/
private boolean forward;
private 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);
}