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

Compare commits

..

5 Commits

19 changed files with 555 additions and 254 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及以上版本。
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.1</version>
<version>1.2.4</version>
</dependency>
```
@@ -38,7 +38,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</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.1</version>
<version>1.2.4</version>
</dependency>
```
@@ -34,7 +34,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
</dependency>
```

View File

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

@@ -1,7 +1,5 @@
package com.nlf.calendar;
import com.nlf.calendar.util.LunarUtil;
/**
* 节气
*
@@ -48,15 +46,13 @@ public class JieQi {
*/
public void setName(String name) {
this.name = name;
for(String key: LunarUtil.JIE){
if(key.equals(name)){
this.jie = true;
return;
}
}
for(String key: LunarUtil.QI){
if(key.equals(name)){
this.qi = true;
for(int i=0,j=Lunar.JIE_QI.length;i<j;i++){
if(name.equals(Lunar.JIE_QI[i])){
if(i%2==0){
this.qi = true;
}else{
this.jie = true;
}
return;
}
}

View File

@@ -11,47 +11,18 @@ import java.util.*;
* @author 6tail
*/
public class Lunar {
/**
* 节气表头部追加农历上年末的节气名(节令:大雪),以示区分
*/
public static final String JIE_QI_PREPEND = "DA_XUE";
/**
* 节气表尾部追加农历下年初的节气名(气令:冬至),以示区分
*/
public static final String JIE_QI_APPEND = "DONG_ZHI";
/**
* 农历年初节气名(气令:冬至)
*/
public static final String JIE_QI_FIRST = "冬至";
/**
* 农历年末节气名(节令:大雪)
*/
public static final String JIE_QI_LAST = "大雪";
/**
* 节气表尾部追加阳历下年初的第一个节气名(节令:小寒),以示区分
*/
public static final String JIE_APPEND_SOLAR_FIRST = "XIAO_HAN";
/**
* 节气表尾部追加阳历下年初的第二个节气名(气令:大寒),以示区分
*/
public static final String QI_APPEND_SOLAR_SECOND = "DA_HAN";
/**
* 阳历下年初的第一个节气名(节令:小寒)
*/
public static final String JIE_SOLAR_FIRST = "小寒";
/**
* 阳历下年初的第二个节气名(气令:大寒)
*/
public static final String QI_SOLAR_SECOND = "大寒";
/**
* 1天对应的毫秒
*/
private static final long MS_PER_DAY = 86400000L;
/**
* 节气表,国标以冬至为首个节气
* 节气表,国标以冬至为首个气令
*/
public static final String[] JIE_QI = {"冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪"};
public static final String[] JIE_QI_IN_USE = {JIE_QI_PREPEND, "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", JIE_QI_APPEND, JIE_APPEND_SOLAR_FIRST, QI_APPEND_SOLAR_SECOND};
/**
* 实际的节气表
*/
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN"};
/**
* 农历年
@@ -279,16 +250,20 @@ public class Lunar {
int gExact = yearGanIndex;
int zExact = yearZhiIndex;
//获取立春的阳历时刻
Solar liChun = jieQi.get("立春");
String liChunYmd = liChun.toYmd();
String liChunYmdHms = liChun.toYmdHms();
int solarYear = solar.getYear();
String solarYmd = solar.toYmd();
String solarYmdHms = solar.toYmdHms();
//获取立春的阳历时刻
Solar liChun = jieQi.get("立春");
if (liChun.getYear() != solarYear) {
liChun = jieQi.get("LI_CHUN");
}
String liChunYmd = liChun.toYmd();
String liChunYmdHms = liChun.toYmdHms();
//阳历和阴历年份相同代表正月初一及以后
if (year == solar.getYear()) {
if (year == solarYear) {
//立春日期判断
if (solarYmd.compareTo(liChunYmd) < 0) {
g--;
@@ -299,7 +274,7 @@ public class Lunar {
gExact--;
zExact--;
}
} else {
} else if (year < solarYear) {
if (solarYmd.compareTo(liChunYmd) >= 0) {
g++;
z++;
@@ -310,91 +285,60 @@ public class Lunar {
}
}
if (g < 0) {
g += 10;
}
if (g >= 10) {
g -= 10;
}
if (z < 0) {
z += 12;
}
if (z >= 12) {
z -= 12;
}
if (gExact < 0) {
gExact += 10;
}
if (gExact >= 10) {
gExact -= 10;
}
if (zExact < 0) {
zExact += 12;
}
if (zExact >= 12) {
zExact -= 12;
}
yearGanIndexByLiChun = (g<0?g+10:g)%10;
yearZhiIndexByLiChun = (z<0?z+12:z)%12;
yearGanIndexByLiChun = g;
yearZhiIndexByLiChun = z;
yearGanIndexExact = gExact;
yearZhiIndexExact = zExact;
yearGanIndexExact = (gExact<0?gExact+10:gExact)%10;
yearZhiIndexExact = (zExact<0?zExact+12:zExact)%12;
}
/**
* 干支纪月计算
* 计算干支纪月
*/
private void computeMonth() {
Solar start = null;
Solar end;
//干偏移值(以立春当天起算)
int gOffset = ((yearGanIndexByLiChun % 5 + 1) * 2) % 10;
//干偏移值(以立春交接时刻起算)
int gOffsetExact = ((yearGanIndexExact % 5 + 1) * 2) % 10;
String ymd = solar.toYmd();
String time = solar.toYmdHms();
int size = JIE_QI_IN_USE.length;
//序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
int index = -2;
for (String jie : LunarUtil.JIE) {
end = jieQi.get(jie);
String ymd = solar.toYmd();
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
int index = -3;
for (int i=0;i<size;i+=2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String symd = null == start ? ymd : start.toYmd();
String eymd = end.toYmd();
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(eymd) < 0) {
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
break;
}
start = end;
index++;
}
if (index < 0) {
index += 12;
}
monthGanIndex = (index + gOffset) % 10;
monthZhiIndex = (index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
//干偏移值(以立春当天起算)
int offset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndex = ((index<0?index+10:index) + offset) % 10;
monthZhiIndex = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
//序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
int indexExact = -2;
for (String jie : LunarUtil.JIE) {
end = jieQi.get(jie);
String time = solar.toYmdHms();
start = null;
index = -3;
for (int i=0;i<size;i+=2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String stime = null == start ? time : start.toYmdHms();
String etime = end.toYmdHms();
if (time.compareTo(stime) >= 0 && time.compareTo(etime) < 0) {
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
break;
}
start = end;
indexExact++;
index++;
}
if (indexExact < 0) {
indexExact += 12;
}
monthGanIndexExact = (indexExact + gOffsetExact) % 10;
monthZhiIndexExact = (indexExact + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
//干偏移值(以立春交接时刻起算)
offset = (((yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndexExact = ((index<0?index+10:index) + offset) % 10;
monthZhiIndexExact = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
}
/**
* 干支纪日计算
* 计算干支纪日
*/
private void computeDay() {
Solar noon = Solar.fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), 12, 0, 0);
@@ -427,7 +371,7 @@ public class Lunar {
}
/**
* 干支纪时计算
* 计算干支纪时
*/
private void computeTime() {
String hm = (hour < 10 ? "0" : "") + hour + ":" + (minute < 10 ? "0" : "") + minute;
@@ -436,7 +380,7 @@ public class Lunar {
}
/**
* 星期计算
* 计算星期
*/
private void computeWeek() {
this.weekIndex = solar.getWeek();
@@ -863,29 +807,38 @@ public class Lunar {
return LunarUtil.SEASON[Math.abs(month)];
}
protected String convertJieQi(String name){
String jq = name;
if("DONG_ZHI".equals(jq)){
jq = "冬至";
}else if("DA_HAN".equals(jq)){
jq = "大寒";
}else if("XIAO_HAN".equals(jq)){
jq = "小寒";
}else if("LI_CHUN".equals(jq)){
jq = "立春";
}else if("DA_XUE".equals(jq)){
jq = "大雪";
}
return jq;
}
/**
* 获取节令
*
* @return 节令
*/
public String getJie() {
for (String jie : LunarUtil.JIE) {
Solar d = jieQi.get(jie);
String jie = "";
for(int i=1,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return jie;
jie = key;
break;
}
}
// 追加的节令:大雪
Solar d = jieQi.get(JIE_QI_PREPEND);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return JIE_QI_LAST;
}
// 追加的节令:小寒
d = jieQi.get(JIE_APPEND_SOLAR_FIRST);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return JIE_SOLAR_FIRST;
}
return "";
return convertJieQi(jie);
}
/**
@@ -894,23 +847,16 @@ public class Lunar {
* @return 气令
*/
public String getQi() {
for (String qi : LunarUtil.QI) {
Solar d = jieQi.get(qi);
String qi = "";
for(int i=0,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return qi;
qi = key;
break;
}
}
// 追加的气令:冬至
Solar d = jieQi.get(JIE_QI_APPEND);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return JIE_QI_FIRST;
}
// 追加的气令:大寒
d = jieQi.get(QI_APPEND_SOLAR_SECOND);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
return QI_SOLAR_SECOND;
}
return "";
return convertJieQi(qi);
}
/**
@@ -1005,6 +951,9 @@ public class Lunar {
if (null != f) {
l.add(f);
}
if (Math.abs(month) == 12 && day >= 29 && year != next(1).getYear()) {
l.add("除夕");
}
return l;
}
@@ -1954,7 +1903,12 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextJie() {
return getNearJieQi(true, LunarUtil.JIE);
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);
}
/**
@@ -1963,7 +1917,12 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevJie() {
return getNearJieQi(false, LunarUtil.JIE);
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);
}
/**
@@ -1972,7 +1931,12 @@ public class Lunar {
* @return 节气
*/
public JieQi getNextQi() {
return getNearJieQi(true, LunarUtil.QI);
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);
}
/**
@@ -1981,7 +1945,12 @@ public class Lunar {
* @return 节气
*/
public JieQi getPrevQi() {
return getNearJieQi(false, LunarUtil.QI);
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);
}
/**
@@ -2019,19 +1988,7 @@ public class Lunar {
boolean filter = !filters.isEmpty();
String today = solar.toYmdHms();
for (Map.Entry<String, Solar> entry : jieQi.entrySet()) {
String jq = entry.getKey();
if (JIE_QI_APPEND.equals(jq)) {
jq = JIE_QI_FIRST;
}
if (JIE_QI_PREPEND.equals(jq)) {
jq = JIE_QI_LAST;
}
if (JIE_APPEND_SOLAR_FIRST.equals(jq)) {
jq = JIE_SOLAR_FIRST;
}
if (QI_APPEND_SOLAR_SECOND.equals(jq)) {
jq = QI_SOLAR_SECOND;
}
String jq = convertJieQi(entry.getKey());
if (filter) {
if (!filters.contains(jq)) {
continue;
@@ -2077,16 +2034,7 @@ public class Lunar {
break;
}
}
if (JIE_QI_APPEND.equals(name)) {
name = JIE_QI_FIRST;
} else if (JIE_QI_PREPEND.equals(name)) {
name = JIE_QI_LAST;
} else if (JIE_APPEND_SOLAR_FIRST.equals(name)) {
name = JIE_SOLAR_FIRST;
} else if (QI_APPEND_SOLAR_SECOND.equals(name)) {
name = QI_SOLAR_SECOND;
}
return name;
return convertJieQi(name);
}
/**
@@ -2528,11 +2476,11 @@ public class Lunar {
@SuppressWarnings("MagicConstant")
public ShuJiu getShuJiu() {
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
Solar start = jieQi.get(JIE_QI_APPEND);
Solar start = jieQi.get("DONG_ZHI");
Calendar startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
if (currentCalendar.compareTo(startCalendar) < 0) {
start = jieQi.get(JIE_QI_FIRST);
start = jieQi.get("冬至");
startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
}
@@ -2642,6 +2590,6 @@ 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];
}
}

View File

@@ -3,7 +3,9 @@ package com.nlf.calendar;
import com.nlf.calendar.util.ShouXingUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 农历年
@@ -12,6 +14,26 @@ 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>();
static {
for (int y : LEAP_11) {
LEAP.put(y, 13);
}
for (int y : LEAP_12) {
LEAP.put(y, 14);
}
}
/**
* 农历年
*/
@@ -51,12 +73,14 @@ public class LunarYear {
// 节气(中午12点)
double[] jq = new double[25];
// 合朔,即每月初一(中午12点)
double[] hs = new double[15];
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++) {
// 精确的节气
double t = 36525 * ShouXingUtil.saLonT((year + (17 + i) * 15d / 360) * ShouXingUtil.PI_2);
@@ -82,25 +106,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++;

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

File diff suppressed because one or more lines are too long

View File

@@ -112,10 +112,6 @@ public class LunarUtil{
public static final String[] SEASON = {"","孟春","仲春","季春","孟夏","仲夏","季夏","孟秋","仲秋","季秋","孟冬","仲冬","季冬"};
/** 生肖 */
public static final String[] SHENGXIAO = {"","鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
/** 气 */
public static final String[] QI = {"大寒","雨水","春分","谷雨","小满","夏至","大暑","处暑","秋分","霜降","小雪","冬至"};
/** 节 */
public static final String[] JIE = {"小寒","立春","惊蛰","清明","立夏","芒种","小暑","立秋","白露","寒露","立冬","大雪"};
/** 日 */
public static final String[] DAY = {"","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
/** 月相,朔月也叫新月,望月也叫满月 */
@@ -132,7 +128,6 @@ public class LunarUtil{
put("8-15","中秋节");
put("9-9","重阳节");
put("12-8","腊八节");
put("12-30","除夕");
}
};
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
@@ -1190,7 +1185,8 @@ public class LunarUtil{
}
}
if(matched) {
String ys = left.substring(left.indexOf(":") + 1, left.indexOf(","));
String ys = left.substring(left.indexOf(':') + 1);
ys = ys.substring(0, ys.indexOf(','));
for (int i = 0, j = ys.length(); i < j; i += 2) {
String m = ys.substring(i, i + 2);
l.add(YI_JI[Integer.parseInt(m,16)]);

View File

@@ -97,7 +97,6 @@ public class SolarUtil {
put("5-5", Collections.nCopies(1, "马克思诞辰纪念日"));
put("5-8", Collections.nCopies(1, "世界红十字日"));
put("5-11", Collections.nCopies(1, "世界肥胖日"));
put("5-23", Collections.nCopies(1, "世界读书日"));
put("5-27", Collections.nCopies(1, "上海解放日"));
put("5-31", Collections.nCopies(1, "世界无烟日"));
put("6-5", Collections.nCopies(1, "世界环境日"));

View File

@@ -57,6 +57,16 @@ public class BaZiTest {
Assert.assertEquals("时柱", "辛亥", eightChar.getTime());
}
@Test
public void testGanZhi3(){
Lunar lunar = new Lunar(2019,12,12,11,22,0);
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("年柱", "己亥", eightChar.getYear());
Assert.assertEquals("月柱", "丁丑", eightChar.getMonth());
Assert.assertEquals("日柱", "戊申", eightChar.getDay());
Assert.assertEquals("时柱", "戊午", eightChar.getTime());
}
@Test
public void testHideGan() {
Solar solar = new Solar(2005, 12, 23, 8, 37, 0);

View File

@@ -73,17 +73,6 @@ public class GanZhiTest {
Assert.assertEquals("丁丑",lunar.getMonthInGanZhi());
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
solar = new Solar(2020,2,4,13,22,0);
lunar = solar.getLunar();
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun());
Assert.assertEquals("己亥",lunar.getYearInGanZhiExact());
Assert.assertEquals("戊寅",lunar.getMonthInGanZhi());
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
solar = new Solar(2020,2,4,18,22,0);
lunar = solar.getLunar();
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
@@ -320,4 +309,28 @@ public class GanZhiTest {
Assert.assertEquals("戊辰",lunar.getYearInGanZhiExact());
}
@Test
public void test17() {
Solar solar = new Solar(2019, 2, 8, 13, 22, 0);
Lunar lunar = solar.getLunar();
Assert.assertEquals("己亥", lunar.getYearInGanZhi());
Assert.assertEquals("己亥", lunar.getYearInGanZhiByLiChun());
Assert.assertEquals("己亥", lunar.getYearInGanZhiExact());
Assert.assertEquals("丙寅", lunar.getMonthInGanZhi());
Assert.assertEquals("丙寅", lunar.getMonthInGanZhiExact());
}
@Test
public void test18() {
Solar solar = new Solar(2020,2,4,13,22,0);
Lunar lunar = solar.getLunar();
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun());
Assert.assertEquals("己亥",lunar.getYearInGanZhiExact());
Assert.assertEquals("戊寅",lunar.getMonthInGanZhi());
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
}
}

View File

@@ -1,5 +1,6 @@
package test;
import com.nlf.calendar.Holiday;
import com.nlf.calendar.Solar;
import com.nlf.calendar.util.HolidayUtil;
import org.junit.Assert;
@@ -116,4 +117,12 @@ public class HolidayTest {
Assert.assertEquals("2021-11-11 结婚纪念日 2021-11-11", HolidayUtil.getHoliday("2021-11-11") + "");
Assert.assertEquals("2021-12-01 她的生日 2021-12-01", HolidayUtil.getHoliday("2021-12-01") + "");
}
@Test
public void test1() {
Holiday holiday = HolidayUtil.getHoliday(2016,10,4);
Assert.assertNotNull(holiday);
Assert.assertNotNull(holiday.getTarget());
Assert.assertEquals("2016-10-01",holiday.getTarget());
}
}

View File

@@ -13,64 +13,280 @@ 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 testNext(){
Solar solar = new Solar(2020,1,10,12,0,0);
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());
}
@Test
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());
}
@Test
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());
}
@Test
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());
}
@Test
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());
}
@Test
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));
}
@Test
public void test20() {
Lunar lunar = new Lunar(2020, 12, 29);
Assert.assertEquals(0, lunar.getFestivals().size());
}
@Test
public void test21() {
Solar solar = Solar.fromYmd(2022, 1, 31);
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.getFestivals().get(0));
}
@Test
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();
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());
}
}

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

@@ -24,4 +24,33 @@ public class YunTest {
Assert.assertEquals("起运阳历", "1989-02-18", yun.getStartSolar().toYmd());
}
@Test
public void test2() {
Lunar lunar = new Lunar(2019, 12, 12, 11, 22, 0);
System.out.println(lunar.getMonth());
System.out.println(lunar.toFullString());
System.out.println(lunar.getSolar().toFullString());
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(1);
Assert.assertEquals("起运年数", 0, yun.getStartYear());
Assert.assertEquals("起运月数", 1, yun.getStartMonth());
Assert.assertEquals("起运天数", 0, yun.getStartDay());
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
}
@Test
public void test3() {
Solar solar = new Solar(2020, 1, 6, 11, 22, 0);
Lunar lunar = solar.getLunar();
System.out.println(lunar.getMonth());
System.out.println(lunar.toFullString());
System.out.println(lunar.getSolar().toFullString());
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(1);
Assert.assertEquals("起运年数", 0, yun.getStartYear());
Assert.assertEquals("起运月数", 1, yun.getStartMonth());
Assert.assertEquals("起运天数", 0, yun.getStartDay());
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
}
}