1
0
mirror of synced 2026-10-11 01:06:08 +08:00

Compare commits

...

5 Commits

19 changed files with 984 additions and 153 deletions

View File

@@ -1,6 +1,6 @@
# lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar-java/blob/master/LICENSE)
lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道黑道日及吉凶、法定节假日及调休等。
lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶、法定节假日及调休等。
> 支持java 1.5及以上版本。
@@ -10,35 +10,13 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
[English](https://github.com/6tail/lunar-java/blob/master/README_EN.md)
### 正式版本
### Maven
```xml
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.3</version>
</dependency>
```
### 快照版本
```xml
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
```
```xml
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.7</version>
</dependency>
```

View File

@@ -6,35 +6,13 @@ lunar is a calendar library for Solar and Chinese Lunar.
[简体中文](https://github.com/6tail/lunar-java/blob/master/README.md)
### Release
### Maven
```xml
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.3</version>
</dependency>
```
### Snapshot
```xml
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
```
```xml
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.2.7</version>
</dependency>
```

View File

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

@@ -2,6 +2,7 @@ package com.nlf.calendar;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* 精确日期
@@ -10,8 +11,13 @@ import java.util.Date;
*/
public class ExactDate {
/**
* 时区
*/
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT+8");
public static Calendar fromYmdHms(int year, int month, int day, int hour, int minute, int second) {
Calendar c = Calendar.getInstance();
Calendar c = Calendar.getInstance(TIME_ZONE);
c.set(year, month - 1, day, hour, minute, second);
c.set(Calendar.MILLISECOND, 0);
return c;
@@ -22,7 +28,7 @@ public class ExactDate {
}
public static Calendar fromDate(Date date) {
Calendar c = Calendar.getInstance();
Calendar c = Calendar.getInstance(TIME_ZONE);
c.setTime(date);
c.set(Calendar.MILLISECOND, 0);
return c;

View File

@@ -22,7 +22,7 @@ public class Lunar {
/**
* 实际的节气表
*/
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN"};
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN", "YU_SHUI", "JING_ZHE"};
/**
* 农历年
@@ -242,6 +242,14 @@ public class Lunar {
yearGanIndex = offset % 10;
yearZhiIndex = offset % 12;
if (yearGanIndex < 0) {
yearGanIndex += 10;
}
if (yearZhiIndex < 0) {
yearZhiIndex += 12;
}
//以立春作为新一年的开始的干支纪年
int g = yearGanIndex;
int z = yearZhiIndex;
@@ -819,6 +827,10 @@ public class Lunar {
jq = "立春";
}else if("DA_XUE".equals(jq)){
jq = "大雪";
} else if ("YU_SHUI".equals(jq)) {
jq = "雨水";
} else if ("JING_ZHE".equals(jq)) {
jq = "惊蛰";
}
return jq;
}
@@ -2590,6 +2602,44 @@ public class Lunar {
Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay());
int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY);
return LunarUtil.WU_HOU[offset * 3 + days / 5];
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,9 +1,12 @@
package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
import com.nlf.calendar.util.ShouXingUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 农历年
@@ -12,6 +15,28 @@ import java.util.List;
*/
public class LunarYear {
/**
* 闰冬月年份
*/
private static final int[] LEAP_11 = {75, 94, 170, 238, 265, 322, 389, 469, 553, 583, 610, 678, 735, 754, 773, 849, 887, 936, 1050, 1069, 1126, 1145, 1164, 1183, 1259, 1278, 1308, 1373, 1403, 1441, 1460, 1498, 1555, 1593, 1612, 1631, 1642, 2033, 2128, 2147, 2242, 2614, 2728, 2910, 3062, 3244, 3339, 3616, 3711, 3730, 3825, 4007, 4159, 4197, 4322, 4341, 4379, 4417, 4531, 4599, 4694, 4713, 4789, 4808, 4971, 5085, 5104, 5161, 5180, 5199, 5294, 5305, 5476, 5677, 5696, 5772, 5791, 5848, 5886, 6049, 6068, 6144, 6163, 6258, 6402, 6440, 6497, 6516, 6630, 6641, 6660, 6679, 6736, 6774, 6850, 6869, 6899, 6918, 6994, 7013, 7032, 7051, 7070, 7089, 7108, 7127, 7146, 7222, 7271, 7290, 7309, 7366, 7385, 7404, 7442, 7461, 7480, 7491, 7499, 7594, 7624, 7643, 7662, 7681, 7719, 7738, 7814, 7863, 7882, 7901, 7939, 7958, 7977, 7996, 8034, 8053, 8072, 8091, 8121, 8159, 8186, 8216, 8235, 8254, 8273, 8311, 8330, 8341, 8349, 8368, 8444, 8463, 8474, 8493, 8531, 8569, 8588, 8626, 8664, 8683, 8694, 8702, 8713, 8721, 8751, 8789, 8808, 8816, 8827, 8846, 8884, 8903, 8922, 8941, 8971, 9036, 9066, 9085, 9104, 9123, 9142, 9161, 9180, 9199, 9218, 9256, 9294, 9313, 9324, 9343, 9362, 9381, 9419, 9438, 9476, 9514, 9533, 9544, 9552, 9563, 9571, 9582, 9601, 9639, 9658, 9666, 9677, 9696, 9734, 9753, 9772, 9791, 9802, 9821, 9886, 9897, 9916, 9935, 9954, 9973, 9992};
/**
* 闰腊月年份
*/
private static final int[] LEAP_12 = {37, 56, 113, 132, 151, 189, 208, 227, 246, 284, 303, 341, 360, 379, 417, 436, 458, 477, 496, 515, 534, 572, 591, 629, 648, 667, 697, 716, 792, 811, 830, 868, 906, 925, 944, 963, 982, 1001, 1020, 1039, 1058, 1088, 1153, 1202, 1221, 1240, 1297, 1335, 1392, 1411, 1422, 1430, 1517, 1525, 1536, 1574, 3358, 3472, 3806, 3988, 4751, 4941, 5066, 5123, 5275, 5343, 5438, 5457, 5495, 5533, 5552, 5715, 5810, 5829, 5905, 5924, 6421, 6535, 6793, 6812, 6888, 6907, 7002, 7184, 7260, 7279, 7374, 7556, 7746, 7757, 7776, 7833, 7852, 7871, 7966, 8015, 8110, 8129, 8148, 8224, 8243, 8338, 8406, 8425, 8482, 8501, 8520, 8558, 8596, 8607, 8615, 8645, 8740, 8778, 8835, 8865, 8930, 8960, 8979, 8998, 9017, 9055, 9074, 9093, 9112, 9150, 9188, 9237, 9275, 9332, 9351, 9370, 9408, 9427, 9446, 9457, 9465, 9495, 9560, 9590, 9628, 9647, 9685, 9715, 9742, 9780, 9810, 9818, 9829, 9848, 9867, 9905, 9924, 9943, 9962, 10000};
private static final Map<Integer, Integer> LEAP = new HashMap<Integer, Integer>();
private static final Map<Integer, LunarYear> CACHE = new HashMap<Integer, LunarYear>();
static {
for (int y : LEAP_11) {
LEAP.put(y, 13);
}
for (int y : LEAP_12) {
LEAP.put(y, 14);
}
}
/**
* 农历年
*/
@@ -44,18 +69,25 @@ public class LunarYear {
* @return 农历年
*/
public static LunarYear fromYear(int lunarYear) {
return new LunarYear(lunarYear);
LunarYear obj = CACHE.get(lunarYear);
if (null == obj) {
obj = new LunarYear(lunarYear);
CACHE.put(lunarYear, obj);
}
return obj;
}
private void compute() {
// 节气(中午12点)
double[] jq = new double[25];
double[] jq = new double[27];
// 合朔,即每月初一(中午12点)
double[] hs = new double[16];
// 每月天数
int[] dayCounts = new int[hs.length - 1];
int year = this.year - 2000;
int currentYear = this.year;
int year = currentYear - 2000;
// 从上年的大雪到下年的立春
for (int i = 0, j = Lunar.JIE_QI_IN_USE.length; i < j; i++) {
// 精确的节气
@@ -63,7 +95,7 @@ public class LunarYear {
t += ShouXingUtil.ONE_THIRD - ShouXingUtil.dtT(t);
jieQiJulianDays.add(t + Solar.J2000);
// 按中午12点算的节气
if (i > 0 && i < 26) {
if (i > 0 && i < 28) {
jq[i - 1] = Math.round(t);
}
}
@@ -82,25 +114,41 @@ public class LunarYear {
dayCounts[i] = (int) (hs[i + 1] - hs[i]);
}
int leap = -1;
if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
Integer currentYearLeap = LEAP.get(currentYear);
if (null == currentYearLeap) {
currentYearLeap = -1;
if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
}
currentYearLeap = i;
}
leap = i;
}
int y = this.year - 1;
int prevYear = currentYear - 1;
Integer prevYearLeap = LEAP.get(prevYear);
prevYearLeap = null == prevYearLeap ? -1 : prevYearLeap - 12;
int y = prevYear;
int m = 11;
for (int i = 0, j = dayCounts.length; i < j; i++) {
boolean isLeap = false;
if (i == leap) {
isLeap = true;
m--;
int cm = m;
boolean isNextLeap = false;
if (y == currentYear && i == currentYearLeap) {
cm = -cm;
} else if (y == prevYear && i == prevYearLeap) {
cm = -cm;
}
if (y == currentYear && i + 1 == currentYearLeap) {
isNextLeap = true;
} else if (y == prevYear && i + 1 == prevYearLeap) {
isNextLeap = true;
}
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000));
if (!isNextLeap) {
m++;
}
this.months.add(new LunarMonth(y, isLeap ? -m : m, dayCounts[i], hs[i] + Solar.J2000));
m++;
if (m == 13) {
m = 1;
y++;
@@ -164,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

@@ -156,6 +156,14 @@ public class Solar {
f -= minute;
f *= 60;
int second = (int) Math.round(f);
if (second > 59) {
second -= 60;
minute++;
}
if (minute > 59) {
minute -= 60;
hour++;
}
calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second);
this.year = year;

View File

@@ -117,12 +117,21 @@ public class DaYun {
}
/**
* 获取流年
* 获取10轮流年
*
* @return 流年
*/
public LiuNian[] getLiuNian() {
int n = 10;
return getLiuNian(10);
}
/**
* 获取流年
*
* @param n 轮数
* @return 流年
*/
public LiuNian[] getLiuNian(int n) {
if (index < 1) {
n = endYear-startYear+1;
}
@@ -134,12 +143,20 @@ public class DaYun {
}
/**
* 获取小运
* 获取10轮小运
*
* @return 小运
*/
public XiaoYun[] getXiaoYun() {
int n = 10;
return getXiaoYun(10);
}
/**
* 获取小运
* @param n 轮数
* @return 小运
*/
public XiaoYun[] getXiaoYun(int n) {
if (index < 1) {
n = endYear-startYear+1;
}

View File

@@ -145,12 +145,21 @@ public class Yun {
}
/**
* 获取大运
* 获取10轮大运
*
* @return 大运
*/
public DaYun[] getDaYun() {
int n = 10;
return getDaYun(10);
}
/**
* 获取大运
*
* @param n 轮数
* @return 大运
*/
public DaYun[] getDaYun(int n) {
DaYun[] l = new DaYun[n];
for (int i = 0; i < n; i++) {
l[i] = new DaYun(this, i);

View File

@@ -7,7 +7,7 @@ import java.util.Collections;
import java.util.List;
/**
* 法定节假日工具(默认自2001年12月29日起,至2020年12月31日止)
* 法定节假日工具(默认自2001年12月29日起)
*
* @author 6tail
*/

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

@@ -240,4 +240,22 @@ public class BaZiTest {
}
}
@Test
public void test4() {
Lunar lunar = Lunar.fromYmd(1985, 12, 27);
Assert.assertEquals("1995-11-05", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}
@Test
public void test5() {
Lunar lunar = Lunar.fromYmd(1985, 1, 27);
Assert.assertEquals("1989-03-28", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}
@Test
public void test6() {
Lunar lunar = Lunar.fromYmd(1986, 12, 27);
Assert.assertEquals("1990-04-15", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}
}

View File

@@ -173,4 +173,17 @@ public class JieQiTest {
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
}
@Test
public void test7() {
Lunar lunar = Lunar.fromYmd(2012, 9, 1);
Assert.assertEquals("2012-09-07 13:29:00", lunar.getJieQiTable().get("白露").toYmdHms());
}
@Test
public void test8() {
Lunar lunar = Lunar.fromYmd(2050, 12, 1);
Assert.assertEquals("2050-12-07 06:41:00", lunar.getJieQiTable().get("大雪").toYmdHms());
}
}

View File

@@ -13,150 +13,343 @@ import org.junit.Test;
public class LunarTest {
@Test
public void test(){
Lunar date = new Lunar(2019,3,27,0,0,0);
Assert.assertEquals("二〇一九年三月廿七",date.toString());
Assert.assertEquals("二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.toFullString());
Assert.assertEquals("2019-05-01",date.getSolar().toString());
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.getSolar().toFullString());
public void test() {
Lunar date = new Lunar(2019, 3, 27, 0, 0, 0);
Assert.assertEquals("二〇一九年三月廿七", date.toString());
Assert.assertEquals("二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]", date.toFullString());
Assert.assertEquals("2019-05-01", date.getSolar().toString());
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座", date.getSolar().toFullString());
}
@Test
public void test1(){
Solar solar = new Solar(1,1,1,12,0,0);
Assert.assertEquals("〇年冬月十八",solar.getLunar().toString());
public void test1() {
Solar solar = new Solar(1, 1, 1, 12, 0, 0);
Assert.assertEquals("〇年冬月十八", solar.getLunar().toString());
}
@Test
public void test2(){
Solar solar = new Solar(9999,12,31,12,0,0);
Assert.assertEquals("九九九九年腊月初二",solar.getLunar().toString());
public void test2() {
Solar solar = new Solar(9999, 12, 31, 12, 0, 0);
Assert.assertEquals("九九九九年腊月初二", solar.getLunar().toString());
}
@Test
public void test3(){
Lunar lunar = new Lunar(0,11,18,12,0,0);
Assert.assertEquals("0001-01-01",lunar.getSolar().toString());
public void test3() {
Lunar lunar = new Lunar(0, 11, 18, 12, 0, 0);
Assert.assertEquals("0001-01-01", lunar.getSolar().toString());
}
@Test
public void test4(){
Lunar lunar = new Lunar(9999,12,2,12,0,0);
Assert.assertEquals("9999-12-31",lunar.getSolar().toString());
public void test4() {
Lunar lunar = new Lunar(9999, 12, 2, 12, 0, 0);
Assert.assertEquals("9999-12-31", lunar.getSolar().toString());
}
@Test
public void test5(){
Lunar lunar = new Lunar(1905,1,1,12,0,0);
Assert.assertEquals("1905-02-04",lunar.getSolar().toString());
public void test5() {
Lunar lunar = new Lunar(1905, 1, 1, 12, 0, 0);
Assert.assertEquals("1905-02-04", lunar.getSolar().toString());
}
@Test
public void test6(){
Lunar lunar = new Lunar(2038,12,29,12,0,0);
Assert.assertEquals("2039-01-23",lunar.getSolar().toString());
public void test6() {
Lunar lunar = new Lunar(2038, 12, 29, 12, 0, 0);
Assert.assertEquals("2039-01-23", lunar.getSolar().toString());
}
@Test
public void test7(){
Lunar lunar = new Lunar(2020,-4,2,13,0,0);
Assert.assertEquals("二〇二〇年闰四月初二",lunar.toString());
Assert.assertEquals("2020-05-24",lunar.getSolar().toString());
public void test7() {
Lunar lunar = new Lunar(2020, -4, 2, 13, 0, 0);
Assert.assertEquals("二〇二〇年闰四月初二", lunar.toString());
Assert.assertEquals("2020-05-24", lunar.getSolar().toString());
}
@Test
public void test8(){
Lunar lunar = new Lunar(2020,12,10,13,0,0);
Assert.assertEquals("二〇二〇年腊月初十",lunar.toString());
Assert.assertEquals("2021-01-22",lunar.getSolar().toString());
public void test8() {
Lunar lunar = new Lunar(2020, 12, 10, 13, 0, 0);
Assert.assertEquals("二〇二〇年腊月初十", lunar.toString());
Assert.assertEquals("2021-01-22", lunar.getSolar().toString());
}
@Test
public void test9(){
Lunar lunar = new Lunar(1500,1,1,12,0,0);
Assert.assertEquals("1500-01-31",lunar.getSolar().toString());
public void test9() {
Lunar lunar = new Lunar(1500, 1, 1, 12, 0, 0);
Assert.assertEquals("1500-01-31", lunar.getSolar().toString());
}
@Test
public void test10(){
Lunar lunar = new Lunar(1500,12,29,12,0,0);
Assert.assertEquals("1501-01-18",lunar.getSolar().toString());
public void test10() {
Lunar lunar = new Lunar(1500, 12, 29, 12, 0, 0);
Assert.assertEquals("1501-01-18", lunar.getSolar().toString());
}
@Test
public void test11(){
Solar solar = new Solar(1500,1,1,12,0,0);
Assert.assertEquals("一四九九年腊月初一",solar.getLunar().toString());
public void test11() {
Solar solar = new Solar(1500, 1, 1, 12, 0, 0);
Assert.assertEquals("一四九九年腊月初一", solar.getLunar().toString());
}
@Test
public void test12(){
Solar solar = new Solar(1500,12,31,12,0,0);
Assert.assertEquals("一五〇〇年腊月十一",solar.getLunar().toString());
public void test12() {
Solar solar = new Solar(1500, 12, 31, 12, 0, 0);
Assert.assertEquals("一五〇〇年腊月十一", solar.getLunar().toString());
}
@Test
public void test13(){
Solar solar = new Solar(1582,10,4,12,0,0);
Assert.assertEquals("一五八二年九月十八",solar.getLunar().toString());
public void test13() {
Solar solar = new Solar(1582, 10, 4, 12, 0, 0);
Assert.assertEquals("一五八二年九月十八", solar.getLunar().toString());
}
@Test
public void test14(){
Solar solar = new Solar(1582,10,15,12,0,0);
Assert.assertEquals("一五八二年九月十九",solar.getLunar().toString());
public void test14() {
Solar solar = new Solar(1582, 10, 15, 12, 0, 0);
Assert.assertEquals("一五八二年九月十九", solar.getLunar().toString());
}
@Test
public void test15(){
Lunar lunar = new Lunar(1582,9,18,12,0,0);
Assert.assertEquals("1582-10-04",lunar.getSolar().toString());
public void test15() {
Lunar lunar = new Lunar(1582, 9, 18, 12, 0, 0);
Assert.assertEquals("1582-10-04", lunar.getSolar().toString());
}
@Test
public void test16(){
Lunar lunar = new Lunar(1582,9,19,12,0,0);
Assert.assertEquals("1582-10-15",lunar.getSolar().toString());
public void test16() {
Lunar lunar = new Lunar(1582, 9, 19, 12, 0, 0);
Assert.assertEquals("1582-10-15", lunar.getSolar().toString());
}
@Test
public void test17(){
Lunar lunar = new Lunar(2019,12,12,11,22,0);
Assert.assertEquals("2020-01-06",lunar.getSolar().toString());
public void test17() {
Lunar lunar = new Lunar(2019, 12, 12, 11, 22, 0);
Assert.assertEquals("2020-01-06", lunar.getSolar().toString());
}
@Test
public void test18(){
Lunar lunar = new Lunar(2021,12,29);
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
public void test18() {
Lunar lunar = new Lunar(2021, 12, 29);
Assert.assertEquals("除夕", lunar.getFestivals().get(0));
}
@Test
public void test19(){
Lunar lunar = new Lunar(2020,12,30);
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
public void test19() {
Lunar lunar = new Lunar(2020, 12, 30);
Assert.assertEquals("除夕", lunar.getFestivals().get(0));
}
@Test
public void test20(){
Lunar lunar = new Lunar(2020,12,29);
Assert.assertEquals(0,lunar.getFestivals().size());
public void test20() {
Lunar lunar = new Lunar(2020, 12, 29);
Assert.assertEquals(0, lunar.getFestivals().size());
}
@Test
public void test21(){
public void test21() {
Solar solar = Solar.fromYmd(2022, 1, 31);
Lunar lunar = solar.getLunar();
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
Assert.assertEquals("除夕", lunar.getFestivals().get(0));
}
@Test
public void testNext(){
Solar solar = new Solar(2020,1,10,12,0,0);
public void test22() {
Lunar lunar = Lunar.fromYmd(2033, -11, 1);
Assert.assertEquals("2033-12-22", lunar.getSolar().toYmd());
}
@Test
public void test23() {
Lunar lunar = Lunar.fromYmd(2022, 1, 1);
Assert.assertEquals("五黄土玉衡", lunar.getYearNineStar().toString());
}
@Test
public void test24() {
Lunar lunar = Lunar.fromYmd(2033, 1, 1);
Assert.assertEquals("三碧木天玑", lunar.getYearNineStar().toString());
}
@Test
public void test25() {
Solar solar = new Solar(2021, 6, 7, 21, 18, 0);
Lunar lunar = solar.getLunar();
for(int i=-50;i<50;i++){
Assert.assertEquals("推移天数:"+i,solar.next(i).getLunar().toFullString(),lunar.next(i).toFullString());
Assert.assertEquals("二〇二一年四月廿七", lunar.toString());
}
@Test
public void test26() {
Lunar lunar = Lunar.fromYmdHms(2021, 6, 7, 21, 18, 0);
Solar solar = lunar.getSolar();
Assert.assertEquals("2021-07-16", solar.toString());
}
@Test
public void testNext() {
Solar solar = new Solar(2020, 1, 10, 12, 0, 0);
Lunar lunar = solar.getLunar();
for (int i = -50; i < 50; i++) {
Assert.assertEquals("推移天数:" + i, solar.next(i).getLunar().toFullString(), lunar.next(i).toFullString());
}
}
@Test
public void test27() {
Solar solar = Solar.fromYmd(1989, 4, 28);
Lunar lunar = solar.getLunar();
Assert.assertEquals(23, lunar.getDay());
}
@Test
public void test28() {
Solar solar = Solar.fromYmd(1990, 10, 8);
Lunar lunar = solar.getLunar();
Assert.assertEquals("乙酉", lunar.getMonthInGanZhiExact());
}
@Test
public void test29() {
Solar solar = Solar.fromYmd(1990, 10, 9);
Lunar lunar = solar.getLunar();
Assert.assertEquals("丙戌", lunar.getMonthInGanZhiExact());
}
@Test
public void test30() {
Solar solar = Solar.fromYmd(1990, 10, 8);
Lunar lunar = solar.getLunar();
Assert.assertEquals("丙戌", lunar.getMonthInGanZhi());
}
@Test
public void test31() {
Solar solar = Solar.fromYmdHms(1987, 4, 17, 9, 0, 0);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八七年三月二十", lunar.toString());
}
@Test
public void test32() {
Lunar lunar = Lunar.fromYmd(2034, 1, 1);
Assert.assertEquals("2034-02-19", lunar.getSolar().toYmd());
}
@Test
public void test33() {
Lunar lunar = Lunar.fromYmd(2033, 12, 1);
Assert.assertEquals("2034-01-20", lunar.getSolar().toYmd());
}
@Test
public void test34() {
Lunar lunar = Lunar.fromYmd(37, -12, 1);
Assert.assertEquals("闰腊", lunar.getMonthInChinese());
}
@Test
public void test35() {
Lunar lunar = Lunar.fromYmd(56, -12, 1);
Assert.assertEquals("闰腊", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(75, -11, 1);
Assert.assertEquals("闰冬", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(94, -11, 1);
Assert.assertEquals("闰冬", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(94, 12, 1);
Assert.assertEquals("腊", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(113, 12, 1);
Assert.assertEquals("腊", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(113, -12, 1);
Assert.assertEquals("闰腊", lunar.getMonthInChinese());
lunar = Lunar.fromYmd(5552, -12, 1);
Assert.assertEquals("闰腊", lunar.getMonthInChinese());
}
@Test
public void test36() {
Solar solar = Solar.fromYmd(5553, 1, 22);
Lunar lunar = solar.getLunar();
Assert.assertEquals("五五五二年闰腊月初二", lunar.toString());
}
@Test
public void test37() {
Solar solar = Solar.fromYmd(7013, 12, 24);
Lunar lunar = solar.getLunar();
Assert.assertEquals("七〇一三年闰冬月初四", lunar.toString());
}
@Test
public void test38() {
Lunar lunar = Lunar.fromYmd(7013, -11, 4);
Solar solar = lunar.getSolar();
Assert.assertEquals("7013-12-24", solar.toString());
}
@Test
public void test39() {
Solar solar = Solar.fromYmd(1987, 4, 12);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八七年三月十五", lunar.toString());
}
@Test
public void test40() {
Solar solar = Solar.fromYmd(1987, 4, 13);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八七年三月十六", lunar.toString());
}
@Test
public void test41() {
Solar solar = Solar.fromYmd(4, 2, 10);
Lunar lunar = solar.getLunar();
Assert.assertEquals("鼠", lunar.getYearShengXiao());
}
@Test
public void test42() {
Solar solar = Solar.fromYmd(4, 2, 9);
Lunar lunar = solar.getLunar();
Assert.assertEquals("猪", lunar.getYearShengXiao());
}
@Test
public void test43() {
Solar solar = Solar.fromYmd(1, 2, 12);
Lunar lunar = solar.getLunar();
Assert.assertEquals("鸡", lunar.getYearShengXiao());
}
@Test
public void test44() {
Solar solar = Solar.fromYmd(1, 1, 1);
Lunar lunar = solar.getLunar();
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,6 +1,7 @@
package test;
import com.nlf.calendar.Solar;
import com.nlf.calendar.util.SolarUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -44,6 +45,11 @@ public class SolarTest {
Assert.assertEquals("二六年三月初八",solar.getLunar().toString());
}
@Test
public void test10(){
Assert.assertEquals(false, SolarUtil.isLeapYear(1500));
}
@Test
public void testNext(){
Solar date = new Solar(2020,1,23);

View File

@@ -68,4 +68,11 @@ public class WuHouTest {
Assert.assertEquals(solar.toString(),"鹖鴠不鸣",lunar.getWuHou());
}
@Test
public void test9(){
Solar solar = new Solar(1982,12,22);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"蚯蚓结",lunar.getWuHou());
}
}

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());
}
}