1
0
mirror of synced 2026-10-14 18:46:13 +08:00

Compare commits

..

3 Commits

20 changed files with 1124 additions and 182 deletions

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.13</version>
<version>1.2.16</version>
</dependency>
```
@@ -54,3 +54,33 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
## 文档
请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html")
## 更新日志
v1.2.15 南京大XX纪念日更改为国家公祭日;新增初候、二候、三候;新增三元九运;新增道历戊日。
v1.2.14 佛历新增27宿;修复宜忌重复的问题;修复获取气时缺冬至的问题。
v1.2.13 新增道历Tao。
v1.2.12 修复星宿方位错误。
v1.2.11 修正胎神数据;增加福神流派。
v1.2.9 新增2022年法定假日。
v1.2.8 新增佛历Foto;更改Lunar中的getOtherFestivals方法为传统节日。
v1.2.7 修复儒略日转阳历秒数为60的错误。
v1.2.6 新增治水、分饼、耕田、得金、日禄;新增时辰LunarTime;新增获取当天的所有时辰。
v1.2.5 修复公元0至4年转阴历错误的问题。
v1.2.4 修复闰冬月、闰腊月的问题;修复日历不准的问题;修复物候错误;大运、小运、流年支持自定义轮数。
v1.2.3 修复除夕错误;代码优化。
v1.2.2 修正2016年国庆节数据;删除5月23日世界读书日;修复v1.2.0和v1.2.1的重大bug。
v1.2.0 支持0001到9999年。

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.13</version>
<version>1.2.16</version>
</dependency>
```

View File

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

@@ -1,5 +1,7 @@
package com.nlf.calendar;
import com.nlf.calendar.util.SolarUtil;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -20,6 +22,9 @@ public class ExactDate {
Calendar c = Calendar.getInstance(TIME_ZONE);
c.set(year, month - 1, day, hour, minute, second);
c.set(Calendar.MILLISECOND, 0);
if (0 == year) {
c.add(Calendar.YEAR, 1);
}
return c;
}
@@ -33,4 +38,50 @@ public class ExactDate {
c.set(Calendar.MILLISECOND, 0);
return c;
}
/**
* 获取两个日期之间相差的天数(如果日期a比日期b小,天数为正,如果日期a比日期b大,天数为负)
*
* @param ay 年a
* @param am 月a
* @param ad 日a
* @param by 年b
* @param bm 月b
* @param bd 日b
* @return 天数
*/
public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) {
int n;
int days;
int i;
if (ay == by) {
n = SolarUtil.getDaysInYear(by, bm, bd) - SolarUtil.getDaysInYear(ay, am, ad);
} else if (ay > by) {
days = SolarUtil.getDaysOfYear(by) - SolarUtil.getDaysInYear(by, bm, bd);
for (i = by + 1; i < ay; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(ay, am, ad);
n = -days;
} else {
days = SolarUtil.getDaysOfYear(ay) - SolarUtil.getDaysInYear(ay, am, ad);
for (i = ay + 1; i < by; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(by, bm, bd);
n = days;
}
return n;
}
/**
* 获取两个日期之间相差的天数(如果日期a比日期b小,天数为正,如果日期a比日期b大,天数为负)
*
* @param calendar0 日期a
* @param calendar1 日期b
* @return 天数
*/
public static int getDaysBetween(Calendar calendar0, Calendar calendar1) {
return getDaysBetween(calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH) + 1, calendar0.get(Calendar.DATE), calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH) + 1, calendar1.get(Calendar.DATE));
}
}

View File

@@ -83,11 +83,21 @@ public class Foto {
return l;
}
/**
* 是否月斋
*
* @return true/false
*/
public boolean isMonthZhai() {
int m = getMonth();
return 1 == m || 5 == m || 9 == m;
}
/**
* 是否杨公忌
*
* @return true/false
*/
public boolean isDayYangGong() {
for (FotoFestival f : getFestivals()) {
if ("杨公忌".equals(f.getName())) {
@@ -97,11 +107,21 @@ public class Foto {
return false;
}
/**
* 是否朔望斋
*
* @return true/false
*/
public boolean isDayZhaiShuoWang() {
int d = getDay();
return 1 == d || 15 == d;
}
/**
* 是否六斋日
*
* @return true/false
*/
public boolean isDayZhaiSix() {
int d = getDay();
if (8 == d || 14 == d || 15 == d || 23 == d || 29 == d || 30 == d) {
@@ -113,11 +133,21 @@ public class Foto {
return false;
}
/**
* 是否十斋日
*
* @return true/false
*/
public boolean isDayZhaiTen() {
int d = getDay();
return 1 == d || 8 == d || 14 == d || 15 == d || 18 == d || 23 == d || 24 == d || 28 == d || 29 == d || 30 == d;
}
/**
* 是否观音斋
*
* @return true/false
*/
public boolean isDayZhaiGuanYin() {
String k = getMonth() + "-" + getDay();
for (String d : FotoUtil.DAY_ZHAI_GUAN_YIN) {
@@ -128,6 +158,69 @@ public class Foto {
return false;
}
/**
* 获取星宿
*
* @return 星宿
*/
public String getXiu() {
return FotoUtil.getXiu(getMonth(), getDay());
}
/**
* 获取宿吉凶
*
* @return 吉/凶
*/
public String getXiuLuck() {
return LunarUtil.XIU_LUCK.get(getXiu());
}
/**
* 获取宿歌诀
*
* @return 宿歌诀
*/
public String getXiuSong() {
return LunarUtil.XIU_SONG.get(getXiu());
}
/**
* 获取政
*
* @return 政
*/
public String getZheng() {
return LunarUtil.ZHENG.get(getXiu());
}
/**
* 获取动物
*
* @return 动物
*/
public String getAnimal() {
return LunarUtil.ANIMAL.get(getXiu());
}
/**
* 获取宫
*
* @return 宫
*/
public String getGong() {
return LunarUtil.GONG.get(getXiu());
}
/**
* 获取兽
*
* @return 兽
*/
public String getShou() {
return LunarUtil.SHOU.get(getGong());
}
@Override
public String toString() {
return getYearInChinese() + "年" + getMonthInChinese() + "月" + getDayInChinese();

View File

@@ -11,10 +11,6 @@ import java.util.*;
* @author 6tail
*/
public class Lunar {
/**
* 1天对应的毫秒
*/
private static final long MS_PER_DAY = 86400000L;
/**
* 节气表,国标以冬至为首个气令
*/
@@ -198,18 +194,14 @@ public class Lunar {
@SuppressWarnings("MagicConstant")
public Lunar(Date date) {
solar = new Solar(date);
Calendar c = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
long solarTime = c.getTimeInMillis();
int y = solar.getYear();
LunarYear ly = LunarYear.fromYear(y);
int currentYear = solar.getYear();
int currentMonth = solar.getMonth();
int currentDay = solar.getDay();
LunarYear ly = LunarYear.fromYear(currentYear);
for (LunarMonth m : ly.getMonths()) {
// 初一
Calendar firstDay = Solar.fromJulianDay(m.getFirstJulianDay()).getCalendar();
firstDay.set(Calendar.HOUR_OF_DAY, 0);
firstDay.set(Calendar.MINUTE, 0);
firstDay.set(Calendar.SECOND, 0);
firstDay.set(Calendar.MILLISECOND, 0);
int days = (int) ((solarTime - firstDay.getTimeInMillis()) / MS_PER_DAY);
Solar firstDay = Solar.fromJulianDay(m.getFirstJulianDay());
int days = ExactDate.getDaysBetween(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), currentYear, currentMonth, currentDay);
if (days < m.getDayCount()) {
year = m.getYear();
month = m.getMonth();
@@ -842,8 +834,8 @@ public class Lunar {
*/
public String getJie() {
String jie = "";
for(int i=1,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
for(int i=0,j=JIE_QI_IN_USE.length;i<j;i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
jie = key;
@@ -860,8 +852,8 @@ public class Lunar {
*/
public String getQi() {
String qi = "";
for(int i=0,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
for(int i=1,j=JIE_QI_IN_USE.length;i<j;i+=2){
String key = JIE_QI_IN_USE[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
qi = key;
@@ -1214,6 +1206,39 @@ public class Lunar {
return LunarUtil.POSITION_DESC.get(getDayPositionCai());
}
/**
* 获取日太岁方位
*
* @return 日太岁方位,如艮
*/
public String getDayPositionTaiSui() {
String p;
String gz = getDayInGanZhi();
if ("甲子,乙丑,丙寅,丁卯,戊辰,已巳".contains(gz)) {
p = "震";
} else if ("丙子,丁丑,戊寅,已卯,庚辰,辛巳".contains(gz)) {
p = "离";
} else if ("戊子,已丑,庚寅,辛卯,壬辰,癸巳".contains(gz)) {
p = "中";
} else if ("庚子,辛丑,壬寅,癸卯,甲辰,乙巳".contains(gz)) {
p = "兑";
} else if ("壬子,癸丑,甲寅,乙卯,丙辰,丁巳".contains(gz)) {
p = "坎";
} else{
p = LunarYear.fromYear(this.getYear()).getPositionTaiSui();
}
return p;
}
/**
* 获取日太岁方位描述
*
* @return 日太岁方位描述,如东北
*/
public String getDayPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getDayPositionTaiSui());
}
/**
* 获取时辰喜神方位
*
@@ -1836,11 +1861,7 @@ public class Lunar {
* @return 值年九星
*/
public NineStar getYearNineStar() {
int index = -(year - 1900) % 9;
if (index < 0) {
index += 9;
}
return new NineStar(index);
return LunarYear.fromYear(year).getNineStar();
}
/**
@@ -1849,23 +1870,22 @@ public class Lunar {
* @return 值月九星
*/
public NineStar getMonthNineStar() {
int start = 2;
int n = 12;
String yearZhi = getYearZhi();
if ("子午卯酉".contains(yearZhi)) {
start = 8;
n = 18;
} else if ("辰戌丑未".contains(yearZhi)) {
start = 5;
n = 15;
}
// 寅月起,所以需要-2
int monthIndex = monthZhiIndex - 2;
if (monthIndex < 0) {
monthIndex += 12;
int m = month;
if(m < 0){
m = -m;
}
int index = start - monthIndex - 1;
while (index < 0) {
index += 9;
int offset = (n - m) % 9;
if(0 == offset){
offset = 9;
}
return new NineStar(index);
return NineStar.fromIndex(offset - 1);
}
/**
@@ -1874,40 +1894,45 @@ public class Lunar {
* @return 值日九星
*/
public NineStar getDayNineStar() {
//顺逆
String solarYmd = solar.toYmd();
String dongZhi = jieQi.get("冬至").toYmd();
String yuShui = jieQi.get("雨水").toYmd();
String guYu = jieQi.get("谷雨").toYmd();
String xiaZhi = jieQi.get("夏至").toYmd();
String chuShu = jieQi.get("处暑").toYmd();
String shuangJiang = jieQi.get("霜降").toYmd();
int start = 6;
boolean asc = false;
if (solarYmd.compareTo(dongZhi) >= 0 && solarYmd.compareTo(yuShui) < 0) {
asc = true;
start = 1;
} else if (solarYmd.compareTo(yuShui) >= 0 && solarYmd.compareTo(guYu) < 0) {
asc = true;
start = 7;
} else if (solarYmd.compareTo(guYu) >= 0 && solarYmd.compareTo(xiaZhi) < 0) {
asc = true;
start = 4;
} else if (solarYmd.compareTo(xiaZhi) >= 0 && solarYmd.compareTo(chuShu) < 0) {
start = 9;
} else if (solarYmd.compareTo(chuShu) >= 0 && solarYmd.compareTo(shuangJiang) < 0) {
start = 3;
Solar dongZhi = jieQi.get("冬至");
Solar dongZhi2 = jieQi.get("DONG_ZHI");
Solar xiaZhi = jieQi.get("夏至");
int dongZhiIndex = LunarUtil.getJiaZiIndex(dongZhi.getLunar().getDayInGanZhi());
int dongZhiIndex2 = LunarUtil.getJiaZiIndex(dongZhi2.getLunar().getDayInGanZhi());
int xiaZhiIndex = LunarUtil.getJiaZiIndex(xiaZhi.getLunar().getDayInGanZhi());
Solar solarShunBai;
Solar solarShunBai2;
Solar solarNiZi;
if (dongZhiIndex>29) {
solarShunBai = dongZhi.next(60 - dongZhiIndex);
} else {
solarShunBai = dongZhi.next(-dongZhiIndex);
}
int ganZhiIndex = LunarUtil.getJiaZiIndex(getDayInGanZhi()) % 9;
int index = asc ? start + ganZhiIndex - 1 : start - ganZhiIndex - 1;
if (index > 8) {
index -= 9;
String solarShunBaiYmd = solarShunBai.toYmd();
if (dongZhiIndex2>29) {
solarShunBai2 = dongZhi2.next(60 - dongZhiIndex2);
} else {
solarShunBai2 = dongZhi2.next(-dongZhiIndex2);
}
if (index < 0) {
index += 9;
String solarShunBaiYmd2 = solarShunBai2.toYmd();
if (xiaZhiIndex>29) {
solarNiZi = xiaZhi.next(60 - xiaZhiIndex);
} else {
solarNiZi = xiaZhi.next(-xiaZhiIndex);
}
return new NineStar(index);
String solarNiZiYmd = solarNiZi.toYmd();
int offset = 0;
if (solarYmd.compareTo(solarShunBaiYmd) >= 0 && solarYmd.compareTo(solarNiZiYmd) < 0) {
offset = ExactDate.getDaysBetween(solarShunBai.getCalendar(), this.getSolar().getCalendar()) % 9;
} else if (solarYmd.compareTo(solarNiZiYmd) >= 0 && solarYmd.compareTo(solarShunBaiYmd2) < 0){
offset = 8 - (ExactDate.getDaysBetween(solarNiZi.getCalendar(), this.getSolar().getCalendar()) % 9);
} else if (solarYmd.compareTo(solarShunBaiYmd2) >= 0) {
offset = ExactDate.getDaysBetween(solarShunBai2.getCalendar(), this.getSolar().getCalendar()) % 9;
} else if (solarYmd.compareTo(solarShunBaiYmd) < 0) {
offset = (8 + ExactDate.getDaysBetween(this.getSolar().getCalendar(), solarShunBai.getCalendar())) % 9;
}
return NineStar.fromIndex(offset);
}
/**
@@ -1916,27 +1941,22 @@ public class Lunar {
* @return 值时九星
*/
public NineStar getTimeNineStar() {
//顺逆
String solarYmd = solar.toYmd();
boolean asc = false;
if (solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0) {
if ((solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0)) {
asc = true;
} else if (solarYmd.compareTo(jieQi.get("DONG_ZHI").toYmd()) >= 0) {
asc = true;
}
int start = asc ? 7 : 3;
int start = asc ? 6 : 2;
String dayZhi = getDayZhi();
if ("子午卯酉".contains(dayZhi)) {
start = asc ? 1 : 9;
start = asc ? 0 : 8;
} else if ("辰戌丑未".contains(dayZhi)) {
start = asc ? 4 : 6;
start = asc ? 3 : 5;
}
int index = asc ? start + timeZhiIndex - 1 : start - timeZhiIndex - 1;
if (index > 8) {
index -= 9;
}
if (index < 0) {
index += 9;
}
return new NineStar(index);
int index = asc ? start + timeZhiIndex : start + 9 - timeZhiIndex;
return new NineStar(index % 9);
}
/**
@@ -1954,12 +1974,22 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextJie() {
return getNextJie(false);
}
/**
* 获取下一节令(顺推的第一个节令)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getNextJie(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2];
}
return getNearJieQi(true, conditions);
return getNearJieQi(true, conditions, wholeDay);
}
/**
@@ -1968,12 +1998,22 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevJie() {
return getPrevJie(false);
}
/**
* 获取上一节令(逆推的第一个节令)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getPrevJie(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2];
}
return getNearJieQi(false, conditions);
return getNearJieQi(false, conditions, wholeDay);
}
/**
@@ -1982,12 +2022,22 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextQi() {
return getNextQi(false);
}
/**
* 获取下一气令(顺推的第一个气令)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getNextQi(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2+1];
}
return getNearJieQi(true, conditions);
return getNearJieQi(true, conditions, wholeDay);
}
/**
@@ -1996,12 +2046,22 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevQi() {
return getPrevQi(false);
}
/**
* 获取上一气令(逆推的第一个气令)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getPrevQi(boolean wholeDay) {
int l = JIE_QI_IN_USE.length/2;
String[] conditions = new String[l];
for(int i=0;i<l;i++){
conditions[i] = JIE_QI_IN_USE[i*2+1];
}
return getNearJieQi(false, conditions);
return getNearJieQi(false, conditions, wholeDay);
}
/**
@@ -2010,7 +2070,17 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextJieQi() {
return getNearJieQi(true, null);
return getNextJieQi(false);
}
/**
* 获取下一节气(顺推的第一个节气)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getNextJieQi(boolean wholeDay) {
return getNearJieQi(true, null, wholeDay);
}
/**
@@ -2019,7 +2089,17 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevJieQi() {
return getNearJieQi(false, null);
return getPrevJieQi(false);
}
/**
* 获取上一节气(逆推的第一个节气)
*
* @param wholeDay 是否按天计
* @return 节气
*/
public JieQi getPrevJieQi(boolean wholeDay) {
return getNearJieQi(false, null, wholeDay);
}
/**
@@ -2027,9 +2107,10 @@ public class Lunar {
*
* @param forward 是否顺推,true为顺推,false为逆推
* @param conditions 过滤条件,如果设置过滤条件,仅返回匹配该名称的
* @param wholeDay 是否按天计
* @return 节气
*/
protected JieQi getNearJieQi(boolean forward, String[] conditions) {
protected JieQi getNearJieQi(boolean forward, String[] conditions, boolean wholeDay) {
String name = null;
Solar near = null;
Set<String> filters = new HashSet<String>();
@@ -2037,7 +2118,7 @@ public class Lunar {
Collections.addAll(filters, conditions);
}
boolean filter = !filters.isEmpty();
String today = solar.toYmdHms();
String today = wholeDay ? solar.toYmd() : solar.toYmdHms();
for (Map.Entry<String, Solar> entry : jieQi.entrySet()) {
String jq = convertJieQi(entry.getKey());
if (filter) {
@@ -2046,22 +2127,34 @@ public class Lunar {
}
}
Solar solar = entry.getValue();
String day = solar.toYmdHms();
String day = wholeDay ? solar.toYmd() : solar.toYmdHms();
if (forward) {
if (day.compareTo(today) < 0) {
continue;
}
if (null == near || day.compareTo(near.toYmdHms()) < 0) {
if (null == near) {
name = jq;
near = solar;
} else {
String nearDay = wholeDay ? near.toYmd() : near.toYmdHms();
if (day.compareTo(nearDay) < 0) {
name = jq;
near = solar;
}
}
} else {
if (day.compareTo(today) > 0) {
continue;
}
if (null == near || day.compareTo(near.toYmdHms()) > 0) {
if (null == near) {
name = jq;
near = solar;
} else {
String nearDay = wholeDay ? near.toYmd() : near.toYmdHms();
if (day.compareTo(nearDay) > 0) {
name = jq;
near = solar;
}
}
}
}
@@ -2542,7 +2635,7 @@ public class Lunar {
return null;
}
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
return new ShuJiu(LunarUtil.NUMBER[days / 9 + 1] + "九", days % 9 + 1);
}
@@ -2571,7 +2664,7 @@ public class Lunar {
return null;
}
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
if (days < 10) {
return new Fu("初伏", days + 1);
}
@@ -2579,7 +2672,7 @@ public class Lunar {
// 第4个庚日,中伏第1天
startCalendar.add(Calendar.DATE, 10);
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
if (days < 10) {
return new Fu("中伏", days + 1);
}
@@ -2589,7 +2682,7 @@ public class Lunar {
Calendar liQiuCalendar = ExactDate.fromYmd(liQiu.getYear(), liQiu.getMonth(), liQiu.getDay());
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
// 末伏
if (liQiuCalendar.compareTo(startCalendar) <= 0) {
if (days < 10) {
@@ -2602,7 +2695,7 @@ public class Lunar {
}
// 末伏第1天
startCalendar.add(Calendar.DATE, 10);
days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
if (days < 10) {
return new Fu("末伏", days + 1);
}
@@ -2626,7 +2719,7 @@ public class Lunar {
*/
@SuppressWarnings("MagicConstant")
public String getWuHou() {
JieQi jieQi = getPrevJieQi();
JieQi jieQi = getPrevJieQi(true);
String name = jieQi.getName();
int offset = 0;
for (int i = 0, j = JIE_QI.length; i < j; i++) {
@@ -2640,10 +2733,25 @@ public class Lunar {
Solar startSolar = jieQi.getSolar();
Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay());
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
return LunarUtil.WU_HOU[(offset * 3 + days / 5) % LunarUtil.WU_HOU.length];
}
/**
* 获取候
*
* @return 候
*/
public String getHou() {
JieQi jieQi = getPrevJieQi(true);
String name = jieQi.getName();
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
Solar startSolar = jieQi.getSolar();
Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay());
int days = ExactDate.getDaysBetween(startCalendar, currentCalendar);
return String.format("%s %s", name, LunarUtil.HOU[(days / 5) % LunarUtil.HOU.length]);
}
/**
* 获取日禄
* @return 日禄

View File

@@ -93,6 +93,46 @@ public class LunarMonth {
return firstJulianDay;
}
/**
* 获取太岁方位
*
* @return 太岁方位,如艮
*/
public String getPositionTaiSui() {
String p;
int m = Math.abs(month);
switch(m) {
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()];
}
return p;
}
/**
* 获取太岁方位描述
*
* @return 太岁方位描述,如东北
*/
public String getPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
}
@Override
public String toString() {
return year + "年" + (isLeap() ? "闰" : "") + LunarUtil.MONTH[Math.abs(month)] + "月(" + dayCount + "天)";

View File

@@ -15,6 +15,16 @@ import java.util.Map;
*/
public class LunarYear {
/**
* 元
*/
private static final String[] YUAN = {"下", "上", "中"};
/**
* 运
*/
private static final String[] YUN = {"七", "八", "九", "一", "二", "三", "四", "五", "六"};
/**
* 闰冬月年份
*/
@@ -42,6 +52,16 @@ public class LunarYear {
*/
private int year;
/**
* 天干下标
*/
private int ganIndex;
/**
* 地支下标
*/
private int zhiIndex;
/**
* 农历月们
*/
@@ -59,6 +79,17 @@ public class LunarYear {
*/
public LunarYear(int lunarYear) {
this.year = lunarYear;
int offset = lunarYear - 4;
int yearGanIndex = offset % 10;
int yearZhiIndex = offset % 12;
if (yearGanIndex < 0) {
yearGanIndex += 10;
}
if (yearZhiIndex < 0) {
yearZhiIndex += 12;
}
this.ganIndex = yearGanIndex;
this.zhiIndex = yearZhiIndex;
compute();
}
@@ -183,6 +214,33 @@ public class LunarYear {
return jieQiJulianDays;
}
/**
* 获取天干
*
* @return 天干
*/
public String getGan() {
return LunarUtil.GAN[ganIndex + 1];
}
/**
* 获取地支
*
* @return 地支
*/
public String getZhi() {
return LunarUtil.ZHI[zhiIndex + 1];
}
/**
* 获取干支
*
* @return 干支
*/
public String getGanZhi() {
return getGan() + getZhi();
}
/**
* 获取农历月
*
@@ -212,30 +270,38 @@ public class LunarYear {
return 0;
}
/**
* 获取治水(正月第一个辰日是初几,就是几龙治水)
*
* @return 治水,如:二龙治水
*/
public String getZhiShui() {
int offset = 4 - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayZhiIndex();
if (offset < 0) {
offset += 12;
}
return LunarUtil.NUMBER[offset+1] + "龙治水";
}
/**
* 获取分饼(正月第一个丙日是初几,就是几人分饼)
*
* @return 分饼,如:六人分饼
*/
public String getFenBing() {
int offset = 2 - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayGanIndex();
protected String getZaoByGan(int index, String name) {
int offset = index - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10;
}
return LunarUtil.NUMBER[offset+1] + "人分饼";
return name.replaceFirst("几", LunarUtil.NUMBER[offset + 1]);
}
protected String getZaoByZhi(int index, String name) {
int offset = index - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayZhiIndex();
if (offset < 0) {
offset += 12;
}
return name.replaceFirst("几", LunarUtil.NUMBER[offset + 1]);
}
/**
* 获取几鼠偷粮
*
* @return 几鼠偷粮
*/
public String getTouLiang() {
return getZaoByZhi(0, "几鼠偷粮");
}
/**
* 获取草子几分
*
* @return 草子几分
*/
public String getCaoZi() {
return getZaoByZhi(0, "草子几分");
}
/**
@@ -244,11 +310,79 @@ public class LunarYear {
* @return 耕田,如:六牛耕田
*/
public String getGengTian() {
int offset = 1 - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayZhiIndex();
if (offset < 0) {
offset += 12;
}
return LunarUtil.NUMBER[offset+1] + "牛耕田";
return getZaoByZhi(1, "几牛耕田");
}
/**
* 获取花收几分
*
* @return 花收几分
*/
public String getHuaShou() {
return getZaoByZhi(3, "花收几分");
}
/**
* 获取治水(正月第一个辰日是初几,就是几龙治水)
*
* @return 治水,如:二龙治水
*/
public String getZhiShui() {
return getZaoByZhi(4, "几龙治水");
}
/**
* 获取几马驮谷
*
* @return 几马驮谷
*/
public String getTuoGu() {
return getZaoByZhi(6, "几马驮谷");
}
/**
* 获取几鸡抢米
*
* @return 几鸡抢米
*/
public String getQiangMi() {
return getZaoByZhi(9, "几鸡抢米");
}
/**
* 获取几姑看蚕
*
* @return 几姑看蚕
*/
public String getKanCan() {
return getZaoByZhi(9, "几姑看蚕");
}
/**
* 获取几屠共猪
*
* @return 几屠共猪
*/
public String getGongZhu() {
return getZaoByZhi(11, "几屠共猪");
}
/**
* 获取甲田几分
*
* @return 甲田几分
*/
public String getJiaTian() {
return getZaoByGan(0, "甲田几分");
}
/**
* 获取分饼(正月第一个丙日是初几,就是几人分饼)
*
* @return 分饼,如:六人分饼
*/
public String getFenBing() {
return getZaoByGan(2, "几人分饼");
}
/**
@@ -257,11 +391,191 @@ public class LunarYear {
* @return 得金,如:一日得金
*/
public String getDeJin() {
int offset = 7 - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10;
return getZaoByGan(7, "几日得金");
}
/**
* 获取几人几丙
*
* @return 几人几丙
*/
public String getRenBing() {
return getZaoByGan(2, getZaoByZhi(2, "几人几丙"));
}
/**
* 获取几人几锄
*
* @return 几人几锄
*/
public String getRenChu() {
return getZaoByGan(3, getZaoByZhi(2, "几人几锄"));
}
/**
* 获取三元
*
* @return 元
*/
public String getYuan() {
return YUAN[((year + 2696) / 60) % 3] + "元";
}
/**
* 获取九运
*
* @return 运
*/
public String getYun() {
return YUN[((year + 2696) / 20) % 9] + "运";
}
/**
* 获取九星
*
* @return 九星
*/
public NineStar getNineStar() {
int index = LunarUtil.getJiaZiIndex(this.getGanZhi()) + 1;
int n = 65;
if ("中元".equals(getYuan())) {
n = 68;
} else if ("下元".equals(getYuan())) {
n = 62;
}
return LunarUtil.NUMBER[offset+1] + "日得金";
int offset = (n - index) % 9;
if (0 == offset) {
offset = 9;
}
return NineStar.fromIndex(offset - 1);
}
/**
* 获取喜神方位
*
* @return 喜神方位,如艮
*/
public String getPositionXi() {
return LunarUtil.POSITION_XI[ganIndex + 1];
}
/**
* 获取喜神方位描述
*
* @return 喜神方位描述,如东北
*/
public String getPositionXiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionXi());
}
/**
* 获取阳贵神方位
*
* @return 阳贵神方位,如艮
*/
public String getPositionYangGui() {
return LunarUtil.POSITION_YANG_GUI[ganIndex + 1];
}
/**
* 获取阳贵神方位描述
*
* @return 阳贵神方位描述,如东北
*/
public String getPositionYangGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYangGui());
}
/**
* 获取阴贵神方位
*
* @return 阴贵神方位,如艮
*/
public String getPositionYinGui() {
return LunarUtil.POSITION_YIN_GUI[ganIndex + 1];
}
/**
* 获取阴贵神方位描述
*
* @return 阴贵神方位描述,如东北
*/
public String getPositionYinGuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionYinGui());
}
/**
* 获取福神方位(默认流派:2)
*
* @return 福神方位,如艮
*/
public String getPositionFu() {
return getPositionFu(2);
}
/**
* 获取福神方位
*
* @param sect 流派,1或2
* @return 福神方位,如艮
*/
public String getPositionFu(int sect) {
return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[ganIndex + 1];
}
/**
* 获取福神方位描述(默认流派:2)
*
* @return 福神方位描述,如东北
*/
public String getPositionFuDesc() {
return getPositionFuDesc(2);
}
/**
* 获取福神方位描述
*
* @param sect 流派,1或2
* @return 福神方位描述,如东北
*/
public String getPositionFuDesc(int sect) {
return LunarUtil.POSITION_DESC.get(getPositionFu(sect));
}
/**
* 获取财神方位
*
* @return 财神方位,如艮
*/
public String getPositionCai() {
return LunarUtil.POSITION_CAI[ganIndex + 1];
}
/**
* 获取财神方位描述
*
* @return 财神方位描述,如东北
*/
public String getPositionCaiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionCai());
}
/**
* 获取太岁方位
*
* @return 太岁方位,如艮
*/
public String getPositionTaiSui() {
return LunarUtil.POSITION_TAI_SUI_YEAR[zhiIndex];
}
/**
* 获取太岁方位描述
*
* @return 太岁方位描述,如东北
*/
public String getPositionTaiSuiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionTaiSui());
}
@Override

View File

@@ -5,198 +5,250 @@ import com.nlf.calendar.util.LunarUtil;
/**
* 九星
* <p>玄空九星、奇门九星都来源于北斗九星,九数、七色、五行、后天八卦方位都是相通的。</p>
*
* @author 6tail
*/
public class NineStar {
/** 九数 */
public static final String[] NUMBER = {"一","二","三","四","五","六","七","八","九"};
/**
* 九数
*/
public static final String[] NUMBER = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
/** 七色 */
public static final String[] COLOR = {"白","黒","碧","绿","黄","白","赤","白","紫"};
/**
* 七色
*/
public static final String[] COLOR = {"白", "黒", "碧", "绿", "黄", "白", "赤", "白", "紫"};
/** 五行 */
public static final String[] WU_XING = {"水","土","木","木","土","金","金","土","火"};
/**
* 五行
*/
public static final String[] WU_XING = {"水", "土", "木", "木", "土", "金", "金", "土", "火"};
/** 后天八卦方位 */
public static final String[] POSITION = {"坎","坤","震","巽","中","乾","兑","艮","离"};
/**
* 后天八卦方位
*/
public static final String[] POSITION = {"坎", "坤", "震", "巽", "中", "乾", "兑", "艮", "离"};
/** 北斗九星 */
public static final String[] NAME_BEI_DOU = {"天枢","天璇","天玑","天权","玉衡","开阳","摇光","洞明","隐元"};
/**
* 北斗九星
*/
public static final String[] NAME_BEI_DOU = {"天枢", "天璇", "天玑", "天权", "玉衡", "开阳", "摇光", "洞明", "隐元"};
/** 玄空九星(玄空风水) */
public static final String[] NAME_XUAN_KONG = {"贪狼","巨门","禄存","文曲","廉贞","武曲","破军","左辅","右弼"};
/**
* 玄空九星(玄空风水)
*/
public static final String[] NAME_XUAN_KONG = {"贪狼", "巨门", "禄存", "文曲", "廉贞", "武曲", "破军", "左辅", "右弼"};
/** 奇门九星(奇门遁甲,也称天盘九星) */
public static final String[] NAME_QI_MEN = {"天蓬","天芮","天冲","天辅","天禽","天心","天柱","天任","天英"};
/**
* 奇门九星(奇门遁甲,也称天盘九星)
*/
public static final String[] NAME_QI_MEN = {"天蓬", "天芮", "天冲", "天辅", "天禽", "天心", "天柱", "天任", "天英"};
/** 八门(奇门遁甲) */
public static final String[] BA_MEN_QI_MEN = {"休","死","伤","杜","","开","惊","生","景"};
/**
* 八门(奇门遁甲)
*/
public static final String[] BA_MEN_QI_MEN = {"休", "死", "伤", "杜", "", "开", "惊", "生", "景"};
/** 太乙九神(太乙神数) */
public static final String[] NAME_TAI_YI = {"太乙","摄提","轩辕","招摇","天符","青龙","咸池","太阴","天乙"};
/**
* 太乙九神(太乙神数)
*/
public static final String[] NAME_TAI_YI = {"太乙", "摄提", "轩辕", "招摇", "天符", "青龙", "咸池", "太阴", "天乙"};
/** 太乙九神对应类型 */
public static final String[] TYPE_TAI_YI = {"吉神","凶神","安神","安神","凶神","吉神","凶神","吉神","吉神"};
/**
* 太乙九神对应类型
*/
public static final String[] TYPE_TAI_YI = {"吉神", "凶神", "安神", "安神", "凶神", "吉神", "凶神", "吉神", "吉神"};
/** 太乙九神歌诀(太乙神数) */
public static final String[] SONG_TAI_YI = {"门中太乙明,星官号贪狼,赌彩财喜旺,婚姻大吉昌,出入无阻挡,参谒见贤良,此行三五里,黑衣别阴阳。","门前见摄提,百事必忧疑,相生犹自可,相克祸必临,死门并相会,老妇哭悲啼,求谋并吉事,尽皆不相宜,只可藏隐遁,若动伤身疾。","出入会轩辕,凡事必缠牵,相生全不美,相克更忧煎,远行多不利,博彩尽输钱,九天玄女法,句句不虚言。","招摇号木星,当之事莫行,相克行人阻,阴人口舌迎,梦寐多惊惧,屋响斧自鸣,阴阳消息理,万法弗违情。","五鬼为天符,当门阴女谋,相克无好事,行路阻中途,走失难寻觅,道逢有尼姑,此星当门值,万事有灾除。","神光跃青龙,财气喜重重,投入有酒食,赌彩最兴隆,更逢相生旺,休言克破凶,见贵安营寨,万事总吉同。","吾将为咸池,当之尽不宜,出入多不利,相克有灾情,赌彩全输尽,求财空手回,仙人真妙语,愚人莫与知,动用虚惊退,反复逆风吹。","坐临太阴星,百祸不相侵,求谋悉成就,知交有觅寻,回风归来路,恐有殃伏起,密语中记取,慎乎莫轻行。","迎来天乙星,相逢百事兴,运用和合庆,茶酒喜相迎,求谋并嫁娶,好合有天成,祸福如神验,吉凶甚分明。"};
/**
* 太乙九神歌诀(太乙神数)
*/
public static final String[] SONG_TAI_YI = {"门中太乙明,星官号贪狼,赌彩财喜旺,婚姻大吉昌,出入无阻挡,参谒见贤良,此行三五里,黑衣别阴阳。", "门前见摄提,百事必忧疑,相生犹自可,相克祸必临,死门并相会,老妇哭悲啼,求谋并吉事,尽皆不相宜,只可藏隐遁,若动伤身疾。", "出入会轩辕,凡事必缠牵,相生全不美,相克更忧煎,远行多不利,博彩尽输钱,九天玄女法,句句不虚言。", "招摇号木星,当之事莫行,相克行人阻,阴人口舌迎,梦寐多惊惧,屋响斧自鸣,阴阳消息理,万法弗违情。", "五鬼为天符,当门阴女谋,相克无好事,行路阻中途,走失难寻觅,道逢有尼姑,此星当门值,万事有灾除。", "神光跃青龙,财气喜重重,投入有酒食,赌彩最兴隆,更逢相生旺,休言克破凶,见贵安营寨,万事总吉同。", "吾将为咸池,当之尽不宜,出入多不利,相克有灾情,赌彩全输尽,求财空手回,仙人真妙语,愚人莫与知,动用虚惊退,反复逆风吹。", "坐临太阴星,百祸不相侵,求谋悉成就,知交有觅寻,回风归来路,恐有殃伏起,密语中记取,慎乎莫轻行。", "迎来天乙星,相逢百事兴,运用和合庆,茶酒喜相迎,求谋并嫁娶,好合有天成,祸福如神验,吉凶甚分明。"};
/** 吉凶(玄空风水) */
public static final String[] LUCK_XUAN_KONG = {"吉","凶","凶","吉","凶","吉","凶","吉","吉"};
/**
* 吉凶(玄空风水)
*/
public static final String[] LUCK_XUAN_KONG = {"吉", "凶", "凶", "吉", "凶", "吉", "凶", "吉", "吉"};
/** 吉凶(奇门遁甲) */
public static final String[] LUCK_QI_MEN = {"大凶","大凶","小吉","大吉","大吉","大吉","小凶","小吉","小凶"};
/**
* 吉凶(奇门遁甲)
*/
public static final String[] LUCK_QI_MEN = {"大凶", "大凶", "小吉", "大吉", "大吉", "大吉", "小凶", "小吉", "小凶"};
/** 阴阳(奇门遁甲) */
public static final String[] YIN_YANG_QI_MEN = {"阳","阴","阳","阳","阳","阴","阴","阳","阴"};
/**
* 阴阳(奇门遁甲)
*/
public static final String[] YIN_YANG_QI_MEN = {"阳", "阴", "阳", "阳", "阳", "阴", "阴", "阳", "阴"};
/** 序号,0到8 */
/**
* 序号,0到8
*/
protected int index;
public NineStar(int index) {
this.index = index;
}
public static NineStar fromIndex(int index) {
return new NineStar(index);
}
/**
* 获取九数
*
* @return 九数
*/
public String getNumber(){
public String getNumber() {
return NUMBER[index];
}
/**
* 获取七色
*
* @return 七色
*/
public String getColor(){
public String getColor() {
return COLOR[index];
}
/**
* 获取五行
*
* @return 五行
*/
public String getWuXing(){
public String getWuXing() {
return WU_XING[index];
}
/**
* 获取方位
*
* @return 方位
*/
public String getPosition(){
public String getPosition() {
return POSITION[index];
}
/**
* 获取方位描述
*
* @return 方位描述
*/
public String getPositionDesc(){
public String getPositionDesc() {
return LunarUtil.POSITION_DESC.get(getPosition());
}
/**
* 获取玄空九星名称
*
* @return 玄空九星名称
*/
public String getNameInXuanKong(){
public String getNameInXuanKong() {
return NAME_XUAN_KONG[index];
}
/**
* 获取北斗九星名称
*
* @return 北斗九星名称
*/
public String getNameInBeiDou(){
public String getNameInBeiDou() {
return NAME_BEI_DOU[index];
}
/**
* 获取奇门九星名称
*
* @return 奇门九星名称
*/
public String getNameInQiMen(){
public String getNameInQiMen() {
return NAME_QI_MEN[index];
}
/**
* 获取太乙九神名称
*
* @return 太乙九神名称
*/
public String getNameInTaiYi(){
public String getNameInTaiYi() {
return NAME_TAI_YI[index];
}
/**
* 获取奇门九星吉凶
*
* @return 大吉/小吉/大凶/小凶
*/
public String getLuckInQiMen(){
public String getLuckInQiMen() {
return LUCK_QI_MEN[index];
}
/**
* 获取玄空九星吉凶
*
* @return 吉/凶
*/
public String getLuckInXuanKong(){
public String getLuckInXuanKong() {
return LUCK_XUAN_KONG[index];
}
/**
* 获取奇门九星阴阳
*
* @return 阴/阳
*/
public String getYinYangInQiMen(){
public String getYinYangInQiMen() {
return YIN_YANG_QI_MEN[index];
}
/**
* 获取太乙九神类型
*
* @return 吉神/凶神/安神
*/
public String getTypeInTaiYi(){
public String getTypeInTaiYi() {
return TYPE_TAI_YI[index];
}
/**
* 获取八门(奇门遁甲)
*
* @return 八门
*/
public String getBaMenInQiMen(){
public String getBaMenInQiMen() {
return BA_MEN_QI_MEN[index];
}
/**
* 获取太乙九神歌诀
*
* @return 太乙九神歌诀
*/
public String getSongInTaiYi(){
public String getSongInTaiYi() {
return SONG_TAI_YI[index];
}
/**
* 获取九星序号,从0开始
*
* @return 序号
*/
public int getIndex(){
public int getIndex() {
return index;
}
@Override
public String toString(){
return getNumber()+getColor()+getWuXing()+getNameInBeiDou();
public String toString() {
return getNumber() + getColor() + getWuXing() + getNameInBeiDou();
}
/**
* 获取详细描述
*
* @return 详细描述
*/
public String toFullString(){
public String toFullString() {
StringBuilder s = new StringBuilder();
s.append(getNumber());
s.append(getColor());
@@ -215,7 +267,7 @@ public class NineStar {
s.append(getNameInQiMen());
s.append(" ");
s.append(getLuckInQiMen());
if(getBaMenInQiMen().length()>0) {
if (getBaMenInQiMen().length() > 0) {
s.append(" ");
s.append(getBaMenInQiMen());
s.append("门");

View File

@@ -278,7 +278,7 @@ public class Solar {
if (offsetYear < 0) {
offsetYear = offsetYear + 60;
}
int startYear = today.getYear() - offsetYear;
int startYear = lunar.getYear() - offsetYear;
int hour = 0;
String timeZhi = timeGanZhi.substring(1);
for (int i = 0, j = LunarUtil.ZHI.length; i < j; i++) {

View File

@@ -149,6 +149,62 @@ public class Tao {
return TaoUtil.BA_HUI.containsKey(lunar.getDayInGanZhi());
}
/**
* 是否明戊日
*
* @return true/false
*/
public boolean isDayMingWu() {
return "戊".equals(lunar.getDayGan());
}
/**
* 是否暗戊日
*
* @return true/false
*/
public boolean isDayAnWu() {
return lunar.getDayZhi().equals(TaoUtil.AN_WU[Math.abs(getMonth()) - 1]);
}
/**
* 是否戊日
*
* @return true/false
*/
public boolean isDayWu() {
return isDayMingWu() || isDayAnWu();
}
/**
* 是否天赦日
*
* @return true/false
*/
public boolean isDayTianShe() {
boolean ret = false;
String mz = lunar.getMonthZhi();
String dgz = lunar.getDayInGanZhi();
if ("寅卯辰".contains(mz)) {
if ("戊寅".equals(dgz)) {
ret = true;
}
} else if ("巳午未".contains(mz)) {
if ("甲午".equals(dgz)) {
ret = true;
}
} else if ("申酉戌".contains(mz)) {
if ("戊申".equals(dgz)) {
ret = true;
}
} else if ("亥子丑".contains(mz)) {
if ("甲子".equals(dgz)) {
ret = true;
}
}
return ret;
}
@Override
public String toString() {
return String.format("%s年%s月%s", getYearInChinese(), getMonthInChinese(), getDayInChinese());

View File

@@ -261,4 +261,25 @@ public class FotoUtil {
put("12-30", Collections.nCopies(1, new FotoFestival("诸神下降,察访善恶", "犯者男女俱亡")));
}
};
/**
* 27星宿,佛教从印度传入中国,印度把28星宿改为27星宿,把牛宿(牛金牛)纳入了女宿(女土蝠)。
*/
public static final String[] XIU_27 = {"角", "亢", "氐", "房", "心", "尾", "箕", "斗", "女", "虚", "危", "室", "壁", "奎", "娄", "胃", "昴", "毕", "觜", "参", "井", "鬼", "柳", "星", "张", "翼", "轸"};
/**
* 每月初一的27星宿偏移
*/
private static final int[] XIU_OFFSET = {11, 13, 15, 17, 19, 21, 24, 0, 2, 4, 7, 9};
/**
* 获取27星宿
*
* @param month 佛历月
* @param day 佛历日
* @return 星宿
*/
public static String getXiu(int month, int day) {
return XIU_27[(XIU_OFFSET[Math.abs(month)-1] + day - 1) % XIU_27.length];
}
}

File diff suppressed because one or more lines are too long

View File

@@ -128,7 +128,7 @@ public class SolarUtil {
put("11-28", Collections.nCopies(1, "恩格斯诞辰纪念日"));
put("12-1", Collections.nCopies(1, "世界艾滋病日"));
put("12-12", Collections.nCopies(1, "西安事变纪念日"));
put("12-13", Collections.nCopies(1, "南京大屠杀纪念日"));
put("12-13", Collections.nCopies(1, "国家公祭日"));
put("12-26", Collections.nCopies(1, "毛泽东诞辰纪念日"));
}
};
@@ -146,6 +146,16 @@ public class SolarUtil {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
/**
* 获取某年有多少天(平年365天,闰年366天)
*
* @param year 年
* @return 天数
*/
public static int getDaysOfYear(int year) {
return isLeapYear(year) ? 366 : 365;
}
/**
* 获取某年某月有多少天
*
@@ -154,6 +164,9 @@ public class SolarUtil {
* @return 天数
*/
public static int getDaysOfMonth(int year, int month) {
if (1582 == year && 10 == month) {
return 21;
}
int m = month - 1;
int d = DAYS_OF_MONTH[m];
//公历闰年2月多一天
@@ -163,6 +176,26 @@ public class SolarUtil {
return d;
}
/**
* 获取某天为当年的第几天
*
* @param year 年
* @param month 月
* @param day 日
* @return 第几天
*/
public static int getDaysInYear(int year, int month, int day) {
int days = 0;
for (int i = 1; i < month; i++) {
days += getDaysOfMonth(year, i);
}
days += day;
if (1582 == year && 10 == month && day >= 15) {
days -= 10;
}
return days;
}
/**
* 获取某年某月有多少周
*

View File

@@ -169,4 +169,9 @@ public class TaoUtil {
*/
public static final String[] WU_LA = {"1-1", "5-5", "7-7", "10-1", "12-8"};
/**
* 暗戊
*/
public static final String[] AN_WU = {"未", "戌", "辰", "寅", "午", "子", "酉", "申", "巳", "亥", "卯", "丑"};
}

View File

@@ -18,4 +18,14 @@ public class FotoTest {
Assert.assertEquals("二五六五年十月十四 (三元降) (四天王巡行)", foto.toFullString());
}
@Test
public void test1() {
Foto foto = Foto.fromLunar(Lunar.fromYmd(2020, 4, 13));
Assert.assertEquals("氐", foto.getXiu());
Assert.assertEquals("土", foto.getZheng());
Assert.assertEquals("貉", foto.getAnimal());
Assert.assertEquals("东", foto.getGong());
Assert.assertEquals("青龙", foto.getShou());
}
}

View File

@@ -186,4 +186,12 @@ public class JieQiTest {
Assert.assertEquals("2050-12-07 06:41:00", lunar.getJieQiTable().get("大雪").toYmdHms());
}
@Test
public void test9() {
Solar solar = Solar.fromYmd(2021, 12, 21);
Assert.assertEquals("冬至", solar.getLunar().getJieQi());
Assert.assertEquals("", solar.getLunar().getJie());
Assert.assertEquals("冬至", solar.getLunar().getQi());
}
}

View File

@@ -387,4 +387,11 @@ public class LunarTest {
Assert.assertEquals("厕灶厨 外西南", lunar.getDayPositionTai());
}
@Test
public void test53() {
Solar solar = Solar.fromYmd(2021, 11, 27);
Lunar lunar = solar.getLunar();
Assert.assertEquals("[嫁娶, 祭祀, 祈福, 求嗣, 开光, 出行, 解除, 出火, 拆卸, 进人口, 入宅, 移徙, 安床, 栽种, 动土, 修造, 纳畜, 入殓, 安葬, 立碑, 除服, 成服]", lunar.getDayYi().toString());
}
}

View File

@@ -75,4 +75,32 @@ public class WuHouTest {
Assert.assertEquals(solar.toString(),"蚯蚓结",lunar.getWuHou());
}
@Test
public void test10(){
Solar solar = new Solar(2021,12,21);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"冬至 初候",lunar.getHou());
}
@Test
public void test11(){
Solar solar = new Solar(2021,12,26);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"冬至 二候",lunar.getHou());
}
@Test
public void test12(){
Solar solar = new Solar(2021,12,31);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"冬至 三候",lunar.getHou());
}
@Test
public void test13(){
Solar solar = new Solar(2022,1,5);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"小寒 初候",lunar.getHou());
}
}

View File

@@ -61,4 +61,82 @@ public class YearTest {
Assert.assertEquals("三日得金",year.getDeJin());
}
@Test
public void test5(){
LunarYear year = new LunarYear(1864);
Assert.assertEquals("上元",year.getYuan());
}
@Test
public void test6(){
LunarYear year = new LunarYear(1923);
Assert.assertEquals("上元",year.getYuan());
}
@Test
public void test7(){
LunarYear year = new LunarYear(1924);
Assert.assertEquals("中元",year.getYuan());
}
@Test
public void test8(){
LunarYear year = new LunarYear(1983);
Assert.assertEquals("中元",year.getYuan());
}
@Test
public void test9(){
LunarYear year = new LunarYear(1984);
Assert.assertEquals("下元",year.getYuan());
}
@Test
public void test10(){
LunarYear year = new LunarYear(2043);
Assert.assertEquals("下元",year.getYuan());
}
@Test
public void test11(){
LunarYear year = new LunarYear(1864);
Assert.assertEquals("一运",year.getYun());
}
@Test
public void test12(){
LunarYear year = new LunarYear(1883);
Assert.assertEquals("一运",year.getYun());
}
@Test
public void test13(){
LunarYear year = new LunarYear(1884);
Assert.assertEquals("二运",year.getYun());
}
@Test
public void test14(){
LunarYear year = new LunarYear(1903);
Assert.assertEquals("二运",year.getYun());
}
@Test
public void test15(){
LunarYear year = new LunarYear(1904);
Assert.assertEquals("三运",year.getYun());
}
@Test
public void test16(){
LunarYear year = new LunarYear(1923);
Assert.assertEquals("三运",year.getYun());
}
@Test
public void test17(){
LunarYear year = new LunarYear(2004);
Assert.assertEquals("八运",year.getYun());
}
}