Skip to content

Commit

Permalink
v1.3.5 Solar增加薪资比例;修正与寿星v5.10的节气误差;修复古时阴阳历转换错误的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Aug 20, 2023
1 parent 3ef8d0c commit d66cd71
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 112 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</dependency>
```

Expand Down Expand Up @@ -54,3 +54,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
## 文档

请移步至 [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html")

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=6tail/lunar-java&type=Date)](https://star-history.com/#6tail/lunar-java&Date)
6 changes: 5 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.4</version>
<version>1.3.5</version>
</dependency>
```

Expand Down Expand Up @@ -50,3 +50,7 @@ Output:
## Documentation

Please visit [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html")

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=6tail/lunar-java&type=Date)](https://star-history.com/#6tail/lunar-java&Date)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.3.4</version>
<version>1.3.5</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>
Expand Down
162 changes: 87 additions & 75 deletions src/main/java/com/nlf/calendar/LunarYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.nlf.calendar.util.ShouXingUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 农历年
Expand All @@ -28,24 +26,15 @@ 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_11 = {75, 94, 170, 265, 322, 398, 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 int[] YMC = {11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

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);
}
}
private static LunarYear CACHE_YEAR = null;

/**
* 农历年
Expand Down Expand Up @@ -93,104 +82,127 @@ public LunarYear(int lunarYear) {
compute();
}

private static boolean contains(int[] arr, int n) {
for (int o : arr) {
if (n == o) {
return true;
}
}
return false;
}

/**
* 通过农历年初始化
*
* @param lunarYear 农历年
* @return 农历年
*/
public static LunarYear fromYear(int lunarYear) {
LunarYear obj = CACHE.get(lunarYear);
if (null == obj) {
obj = new LunarYear(lunarYear);
CACHE.put(lunarYear, obj);
public synchronized static LunarYear fromYear(int lunarYear) {
LunarYear y;
if (null == LunarYear.CACHE_YEAR || LunarYear.CACHE_YEAR.getYear() != lunarYear) {
y = new LunarYear(lunarYear);
LunarYear.CACHE_YEAR = y;
} else {
y = LunarYear.CACHE_YEAR;
}
return obj;
return y;
}

private void compute() {
// 节气(中午12点)
// 节气
double[] jq = new double[27];
// 合朔,即每月初一(中午12点)
// 合朔,即每月初一
double[] hs = new double[16];
// 每月天数
int[] dayCounts = new int[hs.length - 1];
int[] dayCounts = new int[15];
int[] months = new int[15];

int currentYear = this.year;

int year = currentYear - 2000;
double jd = Math.floor((currentYear - 2000) * 365.2422 + 180);
// 355是2000.12冬至,得到较靠近jd的冬至估计值
double w = Math.floor((jd - 355 + 183) / 365.2422) * 365.2422 + 355;
if (ShouXingUtil.calcQi(w) > jd) {
w -= 365.2422;
}
// 25个节气时刻(北京时间),从冬至开始到下一个冬至以后
for (int i = 0; i < 26; i++) {
jq[i] = ShouXingUtil.calcQi(w + 15.2184 * i);
}
// 从上年的大雪到下年的立春
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);
t += ShouXingUtil.ONE_THIRD - ShouXingUtil.dtT(t);
jieQiJulianDays.add(t + Solar.J2000);
// 按中午12点算的节气
if (i > 0 && i < 28) {
jq[i - 1] = Math.round(t);
if (i == 0) {
jd = ShouXingUtil.qiAccurate2(jq[0] - 15.2184);
} else if (i <= 26) {
jd = ShouXingUtil.qiAccurate2(jq[i - 1]);
} else {
jd = ShouXingUtil.qiAccurate2(jq[25] + 15.2184 * (i - 26));
}
jieQiJulianDays.add(jd + Solar.J2000);
}

// 冬至前的初一
double w = ShouXingUtil.calcShuo(jq[0]);
// 冬至前的初一,今年"首朔"的日月黄经差w
w = ShouXingUtil.calcShuo(jq[0]);
if (w > jq[0]) {
if (currentYear != 41 && currentYear != 193 && currentYear != 288 && currentYear != 345 && currentYear != 918 && currentYear != 1013) {
w -= 29.5306;
}
w -= 29.53;
}
// 递推每月初一
for (int i = 0, j = hs.length; i < j; i++) {
for (int i = 0; i < 16; i++) {
hs[i] = ShouXingUtil.calcShuo(w + 29.5306 * i);
}
// 每月天数
for (int i = 0, j = dayCounts.length; i < j; i++) {
dayCounts[i] = (int) (hs[i + 1] - hs[i]);
// 每月
for (int i = 0; i < 15; i++) {
dayCounts[i] = (int)(hs[i + 1] - hs[i]);
months[i] = i;
}

int prevYear = currentYear - 1;

int leapYear = -1;
int leapIndex = -1;

Integer leap = LEAP.get(currentYear);
if (null == leap) {
leap = LEAP.get(prevYear);
if (null == leap) {
if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
}
leapYear = currentYear;
leapIndex = i;
}
} else {
leapYear = prevYear;
leapIndex = leap - 12;
int leapIndex = 16;
if (contains(LEAP_11, currentYear)) {
leapIndex = 13;
} else if (contains(LEAP_12, currentYear)) {
leapIndex = 14;
} else if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
}
} else {
leapYear = currentYear;
leapIndex = leap;
leapIndex = i;
}
for (int i = leapIndex; i < 15; i++) {
months[i] -= 1;
}

int fm = -1;
int index = -1;
int y = prevYear;
int m = 11;
int index = m;
for (int i = 0, j = dayCounts.length; i < j; i++) {
int cm = m;
if (y == leapYear && i == leapIndex) {
cm = -cm;
for (int i = 0; i < 15; i++) {
double dm = hs[i] + Solar.J2000;
int v2 = months[i];
int mc = YMC[v2 % 12];
if (1724360 <= dm && dm < 1729794) {
mc = YMC[(v2 + 1) % 12];
} else if (1807724 <= dm && dm < 1808699) {
mc = YMC[(v2 + 1) % 12];
} else if (dm == 1729794 || dm == 1808699) {
mc = 12;
}
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000, index));
if (y != leapYear || i + 1 != leapIndex) {
m++;
if (fm == -1) {
fm = mc;
index = mc;
}
index++;
if (m == 13) {
m = 1;
if (mc < fm) {
y += 1;
index = 1;
y++;
}
fm = mc;
if (i == leapIndex) {
mc = -mc;
} else if (dm == 1729794 || dm == 1808699) {
mc = -11;
}
this.months.add(new LunarMonth(y, mc, dayCounts[i], hs[i] + Solar.J2000, index));
index++;
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public Solar(double julianDay) {
minute -= 60;
hour++;
}
if (hour > 23) {
hour -= 24;
day += 1;
}
this.year = year;
this.month = month;
this.day = day;
Expand Down Expand Up @@ -852,4 +856,54 @@ public Solar nextHour(int hours) {
return fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), hour, solar.getMinute(), solar.getSecond());
}

/**
* 获取薪资比例(感谢 https://gitee.com/smr1987)
* @return 薪资比例:1/2/3
*/
public int getSalaryRate() {
// 元旦节
if (month == 1 && day == 1) {
return 3;
}
// 劳动节
if (month == 5 && day == 1) {
return 3;
}
// 国庆
if (month == 10 && day >= 1 && day <= 3) {
return 3;
}
Lunar lunar = getLunar();
// 春节
if (lunar.getMonth() == 1 && lunar.getDay() >= 1 && lunar.getDay() <= 3) {
return 3;
}
// 端午
if (lunar.getMonth() == 5 && lunar.getDay() == 5) {
return 3;
}
// 中秋
if (lunar.getMonth() == 8 && lunar.getDay() == 15) {
return 3;
}
// 清明
if ("清明".equals(lunar.getJieQi())) {
return 3;
}
Holiday holiday = HolidayUtil.getHoliday(year, month, day);
if (null != holiday) {
// 法定假日非上班
if (!holiday.isWork()) {
return 2;
}
} else {
// 周末
int week = getWeek();
if (week == 6 || week == 0) {
return 2;
}
}
// 工作日
return 1;
}
}
390 changes: 383 additions & 7 deletions src/main/java/com/nlf/calendar/util/ShouXingUtil.java

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/main/java/com/nlf/calendar/util/SolarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ protected SolarUtil() {
* @return true/false 闰年/非闰年
*/
public static boolean isLeapYear(int year) {
if (year < 1600) {
return year % 4 == 0;
}
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

Expand Down
Loading

0 comments on commit d66cd71

Please sign in to comment.