1
0
mirror of synced 2025-12-16 18:38:00 +08:00

v1.2.6 新增治水、分饼、耕田、得金、日禄;新增时辰LunarTime;新增获取当天的所有时辰。

This commit is contained in:
6tail
2021-10-13 22:26:11 +08:00
parent 2694429549
commit fc0a6291d1
10 changed files with 564 additions and 4 deletions

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.5</version>
<version>1.2.6</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.5</version>
<version>1.2.6</version>
</dependency>
```

View File

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

@@ -2604,4 +2604,42 @@ public class Lunar {
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
return LunarUtil.WU_HOU[(offset * 3 + days / 5) % LunarUtil.WU_HOU.length];
}
/**
* 获取日禄
* @return 日禄
*/
public String getDayLu() {
String gan = LunarUtil.LU.get(getDayGan());
String zhi = LunarUtil.LU.get(getDayZhi());
String lu = gan + "命互禄";
if (null != zhi) {
lu += " " + zhi + "命进禄";
}
return lu;
}
/**
* 获取时辰
*
* @return 时辰
*/
public LunarTime getTime() {
return new LunarTime(year, month, day, hour, minute, second);
}
/**
* 获取当天的时辰列表
*
* @return 时辰列表
*/
public List<LunarTime> getTimes() {
List<LunarTime> l = new ArrayList<LunarTime>();
l.add(new LunarTime(year, month, day, 0, 0, 0));
for(int i = 0; i < 12; i++){
l.add(new LunarTime(year, month, day, (i+1)*2-1, 0, 0));
}
return l;
}
}

View File

@@ -0,0 +1,373 @@
package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
import java.util.List;
import java.util.Map;
/**
* 时辰
*
* @author 6tail
*/
public class LunarTime {
/**
* 天干下标0-9
*/
private int ganIndex;
/**
* 地支下标0-11
*/
private int zhiIndex;
/**
* 阴历
*/
private 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);
this.zhiIndex = LunarUtil.getTimeZhiIndex(String.format("%02d:%02d", hour, minute));
this.ganIndex = (lunar.getDayGanIndexExact() % 5 * 2 + zhiIndex) % 10;
}
public static LunarTime fromYmdHms(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second) {
return new LunarTime(lunarYear, lunarMonth, lunarDay, hour, minute, second);
}
public int getGanIndex() {
return ganIndex;
}
public int getZhiIndex() {
return zhiIndex;
}
/**
* 获取生肖
*
* @return 生肖,如虎
*/
public String getShengXiao() {
return LunarUtil.SHENGXIAO[zhiIndex + 1];
}
/**
* 获取地支
*
* @return 地支
*/
public String getZhi() {
return LunarUtil.ZHI[zhiIndex + 1];
}
/**
* 获取天干
*
* @return 天干
*/
public String getGan() {
return LunarUtil.GAN[ganIndex + 1];
}
/**
* 获取干支(时柱)
*
* @return 干支(时柱)
*/
public String getGanZhi() {
return getGan() + getZhi();
}
/**
* 获取喜神方位
*
* @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());
}
/**
* 获取福神方位
*
* @return 福神方位,如艮
*/
public String getPositionFu() {
return LunarUtil.POSITION_FU[ganIndex + 1];
}
/**
* 获取福神方位描述
*
* @return 福神方位描述,如东北
*/
public String getPositionFuDesc() {
return LunarUtil.POSITION_DESC.get(getPositionFu());
}
/**
* 获取财神方位
*
* @return 财神方位,如艮
*/
public String getPositionCai() {
return LunarUtil.POSITION_CAI[ganIndex + 1];
}
/**
* 获取财神方位描述
*
* @return 财神方位描述,如东北
*/
public String getPositionCaiDesc() {
return LunarUtil.POSITION_DESC.get(getPositionCai());
}
/**
* 获取纳音
*
* @return 纳音,如剑锋金
*/
public String getNaYin() {
return LunarUtil.NAYIN.get(getGanZhi());
}
/**
* 获取值时天神
*
* @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 值时天神类型:黄道/黑道
*/
public String getTianShenType() {
return LunarUtil.TIAN_SHEN_TYPE.get(getTianShen());
}
/**
* 获取值时天神吉凶
*
* @return 吉/凶
*/
public String getTianShenLuck() {
return LunarUtil.TIAN_SHEN_TYPE_LUCK.get(getTianShenType());
}
/**
* 获取时冲
*
* @return 时冲,如申
*/
public String getChong() {
return LunarUtil.CHONG[zhiIndex + 1];
}
/**
* 获取时煞
*
* @return 时煞,如北
*/
public String getSha() {
return LunarUtil.SHA.get(getZhi());
}
/**
* 获取时冲生肖
*
* @return 时冲生肖,如猴
*/
public String getChongShengXiao() {
String chong = getChong();
for (int i = 0, j = LunarUtil.ZHI.length; i < j; i++) {
if (LunarUtil.ZHI[i].equals(chong)) {
return LunarUtil.SHENGXIAO[i];
}
}
return "";
}
/**
* 获取时冲描述
*
* @return 时冲描述,如(壬申)猴
*/
public String getChongDesc() {
return "(" + getChongGan() + getChong() + ")" + getChongShengXiao();
}
/**
* 获取无情之克的时冲天干
*
* @return 无情之克的时冲天干,如甲
*/
public String getChongGan() {
return LunarUtil.CHONG_GAN[ganIndex + 1];
}
/**
* 获取有情之克的时冲天干
*
* @return 有情之克的时冲天干,如甲
*/
public String getChongGanTie() {
return LunarUtil.CHONG_GAN_TIE[ganIndex + 1];
}
/**
* 获取宜,如果没有,返回["无"]
*
* @return 宜
*/
public List<String> getYi() {
return LunarUtil.getTimeYi(lunar.getDayInGanZhiExact(), getGanZhi());
}
/**
* 获取忌,如果没有,返回["无"]
*
* @return 忌
*/
public List<String> getJi() {
return LunarUtil.getTimeJi(lunar.getDayInGanZhiExact(), getGanZhi());
}
/**
* 获取值时九星(时家紫白星歌诀:三元时白最为佳,冬至阳生顺莫差,孟日七宫仲一白,季日四绿发萌芽,每把时辰起甲子,本时星耀照光华,时星移入中宫去,顺飞八方逐细查。夏至阴生逆回首,孟归三碧季加六,仲在九宫时起甲,依然掌中逆轮跨。)
*
* @return 值时九星
*/
public NineStar getNineStar() {
//顺逆
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;
}
int start = asc ? 7 : 3;
String dayZhi = lunar.getDayZhi();
if ("子午卯酉".contains(dayZhi)) {
start = asc ? 1 : 9;
} else if ("辰戌丑未".contains(dayZhi)) {
start = asc ? 4 : 6;
}
int index = asc ? start + zhiIndex - 1 : start - zhiIndex - 1;
if (index > 8) {
index -= 9;
}
if (index < 0) {
index += 9;
}
return new NineStar(index);
}
/**
* 获取所在旬
*
* @return 旬
*/
public String getXun() {
return LunarUtil.getXun(getGanZhi());
}
/**
* 获取值时空亡
*
* @return 空亡(旬空)
*/
public String getXunKong() {
return LunarUtil.getXunKong(getGanZhi());
}
/**
* 获取当前时辰的最早时分
*
* @return 时分21:00
*/
public String getMinHm() {
int hour = lunar.getHour();
if (hour <1){
return "00:00";
} else if (hour > 22) {
return "23:00";
}
return String.format("%02d:00", hour % 2 == 0? hour - 1 : hour);
}
/**
* 获取当前时辰的最晚时分
*
* @return 时分22:59
*/
public String getMaxHm() {
int hour = lunar.getHour();
if (hour <1){
return "00:59";
} else if (hour > 22) {
return "23:59";
}
return String.format("%02d:59", hour % 2 == 0? hour : hour + 1);
}
@Override
public String toString() {
return getGanZhi();
}
}

View File

@@ -1,5 +1,6 @@
package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
import com.nlf.calendar.util.ShouXingUtil;
import java.util.ArrayList;
@@ -211,6 +212,58 @@ 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();
if (offset < 0) {
offset += 10;
}
return LunarUtil.NUMBER[offset+1] + "人分饼";
}
/**
* 获取耕田(正月第一个丑日是初几,就是几牛耕田)
*
* @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 得金,如:一日得金
*/
public String getDeJin() {
int offset = 7 - Solar.fromJulianDay(getMonth(1).getFirstJulianDay()).getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10;
}
return LunarUtil.NUMBER[offset+1] + "日得金";
}
@Override
public String toString() {
return year + "";

View File

@@ -105,7 +105,7 @@ public class LunarUtil{
/** 彭祖百忌.地支 */
public static final String[] PENGZU_ZHI = {"","子不问卜自惹祸殃","丑不冠带主不还乡","寅不祭祀神鬼不尝","卯不穿井水泉不香","辰不哭泣必主重丧","巳不远行财物伏藏","午不苫盖屋主更张","未不服药毒气入肠","申不安床鬼祟入房","酉不会客醉坐颠狂","戌不吃犬作怪上床","亥不嫁娶不利新郎"};
/** 数字 */
public static final String[] NUMBER = {"","","","","","","","","",""};
public static final String[] NUMBER = {"","","","","","","","","","","","十一","十二"};
/** 月 */
public static final String[] MONTH = {"","","","","","","","","","","","",""};
/** 季节 */
@@ -1097,6 +1097,32 @@ public class LunarUtil{
}
};
/** 禄(甲禄在寅,乙禄在卯,丙戊禄在巳、丁己禄在午、庚禄在申、辛禄在酉、壬禄在亥、癸禄在子) */
public static final Map<String,String> LU = new HashMap<String, String>(){
private static final long serialVersionUID = -1L;
{
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","");
put("","丙,戊");
put("","丁,己");
put("","");
put("","");
put("","");
put("","");
}
};
protected LunarUtil(){}
/**

View File

@@ -108,6 +108,15 @@ public class BaZiTestNew {
}
}
@Test
public void testBazi2Solar3() {
List<Solar> l = Solar.fromBaZi("辛丑", "丁酉", "丙寅", "戊戌");
// [2021-09-15 20:00:00 星期三 处女座, 1961-09-30 20:00:00 星期六 天秤座]
for (Solar solar : l) {
System.out.println(solar.toFullString());
}
}
@Test
public void testBaziShiShenZhi() {
Solar solar = new Solar(2020, 1, 1, 22, 35, 0);

View File

@@ -331,4 +331,25 @@ public class LunarTest {
Assert.assertEquals("", lunar.getYearShengXiao());
}
@Test
public void test45() {
Solar solar = Solar.fromYmd(1946, 9, 30);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九四六年九月初六", lunar.toString());
}
@Test
public void test46() {
Solar solar = Solar.fromYmd(1946, 9, 29);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九四六年九月初五", lunar.toString());
}
@Test
public void test47() {
Solar solar = Solar.fromYmd(2017, 2, 15);
Lunar lunar = solar.getLunar();
Assert.assertEquals("子命互禄 辛命进禄", lunar.getDayLu());
}
}

View File

@@ -1,5 +1,6 @@
package test;
import com.nlf.calendar.LunarYear;
import com.nlf.calendar.SolarYear;
import org.junit.Assert;
import org.junit.Test;
@@ -21,4 +22,43 @@ public class YearTest {
Assert.assertEquals("2020年",year.next(1).toFullString());
}
@Test
public void test1(){
LunarYear year = new LunarYear(2017);
Assert.assertEquals("二龙治水",year.getZhiShui());
year = new LunarYear(2018);
Assert.assertEquals("二龙治水",year.getZhiShui());
year = new LunarYear(2019);
Assert.assertEquals("八龙治水",year.getZhiShui());
year = new LunarYear(5);
Assert.assertEquals("三龙治水",year.getZhiShui());
}
@Test
public void test2(){
LunarYear year = new LunarYear(2017);
Assert.assertEquals("二人分饼",year.getFenBing());
year = new LunarYear(2018);
Assert.assertEquals("八人分饼",year.getFenBing());
year = new LunarYear(5);
Assert.assertEquals("一人分饼",year.getFenBing());
}
@Test
public void test3(){
LunarYear year = new LunarYear(2021);
Assert.assertEquals("十一牛耕田",year.getGengTian());
}
@Test
public void test4(){
LunarYear year = new LunarYear(2018);
Assert.assertEquals("三日得金",year.getDeJin());
}
}