Skip to content

Commit

Permalink
v1.2.29 修复1582年总天数问题;修复八字转阳历遗漏问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Jan 30, 2023
1 parent c3082db commit 30f1e63
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 80 deletions.
2 changes: 1 addition & 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.2.28</version>
<version>1.2.29</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 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.2.28</version>
<version>1.2.29</version>
</dependency>
```

Expand Down
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.2.28</version>
<version>1.2.29</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
24 changes: 2 additions & 22 deletions src/main/java/com/nlf/calendar/ExactDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,7 @@ public static Calendar fromDate(Date date) {
* @return 天数
*/
public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) {
int n;
int days;
int i;
if (ay == by) {
n = SolarUtil.getDaysInYear(by, bm, bd) - SolarUtil.getDaysInYear(ay, am, ad);
} else if (ay > by) {
days = SolarUtil.getDaysOfYear(by) - SolarUtil.getDaysInYear(by, bm, bd);
for (i = by + 1; i < ay; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(ay, am, ad);
n = -days;
} else {
days = SolarUtil.getDaysOfYear(ay) - SolarUtil.getDaysInYear(ay, am, ad);
for (i = ay + 1; i < by; i++) {
days += SolarUtil.getDaysOfYear(i);
}
days += SolarUtil.getDaysInYear(by, bm, bd);
n = days;
}
return n;
return SolarUtil.getDaysBetween(ay, am, ad, by, bm, bd);
}

/**
Expand All @@ -83,6 +63,6 @@ public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd)
* @return 天数
*/
public static int getDaysBetween(Calendar calendar0, Calendar calendar1) {
return getDaysBetween(calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH) + 1, calendar0.get(Calendar.DATE), calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH) + 1, calendar1.get(Calendar.DATE));
return SolarUtil.getDaysBetween(calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH) + 1, calendar0.get(Calendar.DATE), calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH) + 1, calendar1.get(Calendar.DATE));
}
}
64 changes: 19 additions & 45 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,64 +277,38 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) {
sect = (1 == sect) ? 1 : 2;
List<Solar> l = new ArrayList<Solar>();
Solar today = new Solar();
Lunar lunar = today.getLunar();
int offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact()) - LunarUtil.getJiaZiIndex(yearGanZhi);
if (offsetYear < 0) {
offsetYear = offsetYear + 60;
List<Integer> years = new ArrayList<Integer>();
Solar today = fromDate(new Date());
int offsetYear = LunarUtil.getJiaZiIndex(today.getLunar().getYearInGanZhiExact())-LunarUtil.getJiaZiIndex(yearGanZhi);
if(offsetYear < 0){
offsetYear += 60;
}
int startYear = today.getYear() - offsetYear - 1;
while (startYear >= baseYear) {
years.add(startYear);
startYear -= 60;
}
int startYear = lunar.getYear() - offsetYear;
int hour = 0;
String timeZhi = timeGanZhi.substring(1);
for (int i = 0, j = LunarUtil.ZHI.length; i < j; i++) {
if (LunarUtil.ZHI[i].equals(timeZhi)) {
for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){
if(LunarUtil.ZHI[i].equals(timeZhi)){
hour = (i - 1) * 2;
}
}
while (startYear >= baseYear) {
int year = startYear - 1;
int counter = 0;
int month = 12;
int day;
boolean found = false;
while (counter < 15) {
if (year >= baseYear) {
day = 1;
Solar solar = new Solar(year, month, day, hour, 0, 0);
lunar = solar.getLunar();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi)) {
found = true;
break;
}
}
month++;
if (month > 12) {
month = 1;
year++;
}
counter++;
}
if (found) {
counter = 0;
month--;
if (month < 1) {
month = 12;
year--;
}
day = 1;
Solar solar = new Solar(year, month, day, hour, 0, 0);
while (counter < 61) {
lunar = solar.getLunar();
for (Integer integer : years) {
inner: for (int x = 0; x < 3; x++) {
int year = integer + x;
Solar solar = fromYmdHms(year, 1, 1, hour, 0, 0);
while (solar.getYear() == year) {
Lunar lunar = solar.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
l.add(solar);
break;
break inner;
}
solar = solar.next(1);
counter++;
}
}
startYear -= 60;
}
return l;
}
Expand Down
74 changes: 71 additions & 3 deletions src/main/java/com/nlf/calendar/util/SolarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public static boolean isLeapYear(int year) {
* @return 天数
*/
public static int getDaysOfYear(int year) {
if (1582 == year) {
return 355;
}
return isLeapYear(year) ? 366 : 365;
}

Expand Down Expand Up @@ -193,10 +196,15 @@ public static int getDaysInYear(int year, int month, int day) {
for (int i = 1; i < month; i++) {
days += getDaysOfMonth(year, i);
}
days += day;
if (1582 == year && 10 == month && day >= 15) {
days -= 10;
int d = day;
if (1582 == year && 10 == month) {
if (day >= 15) {
d -= 10;
} else if (day > 4) {
throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", year, month, day));
}
}
days += d;
return days;
}

Expand All @@ -213,4 +221,64 @@ public static int getWeeksOfMonth(int year, int month, int start) {
int week = ExactDate.fromYmd(year, month, 1).get(Calendar.DAY_OF_WEEK) - 1;
return (int) Math.ceil((days + week - start) * 1D / WEEK.length);
}

/**
* 获取两个日期之间相差的天数(如果日期a比日期b小,天数为正,如果日期a比日期b大,天数为负)
*
* @param ay 年a
* @param am 月a
* @param ad 日a
* @param by 年b
* @param bm 月b
* @param bd 日b
* @return 天数
*/
public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) {
int n;
int days;
int i;
if (ay == by) {
n = getDaysInYear(by, bm, bd) - getDaysInYear(ay, am, ad);
} else if (ay > by) {
days = getDaysOfYear(by) - getDaysInYear(by, bm, bd);
for (i = by + 1; i < ay; i++) {
days += getDaysOfYear(i);
}
days += getDaysInYear(ay, am, ad);
n = -days;
} else {
days = getDaysOfYear(ay) - getDaysInYear(ay, am, ad);
for (i = ay + 1; i < by; i++) {
days += getDaysOfYear(i);
}
days += getDaysInYear(by, bm, bd);
n = days;
}
return n;
}

public static int getWeek(int y, int m, int d) {
if (1582 == y && 10 == m) {
if (d > 4 && d < 15) {
throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", y, m, d));
}
}
Calendar start = ExactDate.fromYmd(1582, 10, 15);
Calendar current = ExactDate.fromYmd(y, m, d);
// 蔡勒公式
if (m < 3) {
m += 12;
y--;
}
int c = y/100;
y = y - c * 100;
int w;
int x = y + y / 4 + c / 4 - 2 * c;
if (current.before(start)) {
w = (x + (13*(m+1))/5 + d + 2) % 7;
} else {
w = (x + (26*(m+1))/10 + d - 1) % 7;
}
return (w + 7) % 7;
}
}
66 changes: 59 additions & 7 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -250,20 +251,71 @@ public void testShenGong3() {

@Test
public void testBaZi2Solar() {
Solar solar = Solar.fromYmdHms(2027,1,27,12,0,0);
System.out.println(solar.getLunar().getEightChar().toString());
List<Solar> l = Solar.fromBaZi("丙午","辛丑","丙午","甲午");
for(Solar s:l){
System.out.println(s.toFullString());
List<Solar> l = Solar.fromBaZi("丙辰","丁酉","丙子","甲午");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1976-09-21 12:00:00");
expected.add("1916-10-06 12:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void testBaZi2Solar1() {
List<Solar> l = Solar.fromBaZi("壬寅","庚戌","己未","乙亥");
for(Solar s:l){
System.out.println(s.toFullString());
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("2022-11-02 22:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void testBaZi2Solar2() {
List<Solar> l = Solar.fromBaZi("己卯","辛未","甲戌","壬申");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1999-07-21 16:00:00");
expected.add("1939-08-05 16:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void testBaZi2Solar3() {
List<Solar> l = Solar.fromBaZi("庚子","戊子","己卯","庚午");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1960-12-17 12:00:00");
expected.add("1901-01-01 12:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void testBaZi2Solar4() {
List<Solar> l = Solar.fromBaZi("庚子","癸未","乙丑","丁亥");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("2020-07-21 22:00:00");
expected.add("1960-08-05 22:00:00");
Assert.assertEquals(expected, actual);
}

@Test
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/test/LunarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,16 @@ public void test61() {
Assert.assertEquals("秋社", lunar.getOtherFestivals().get(0));
}

@Test
public void test62() {
Lunar lunar = Lunar.fromYmd(1582, 9, 18);
Assert.assertEquals("1582-10-04", lunar.getSolar().toString());
}

@Test
public void test63() {
Lunar lunar = Lunar.fromYmd(1582, 9, 19);
Assert.assertEquals("1582-10-15", lunar.getSolar().toString());
}

}
Loading

0 comments on commit 30f1e63

Please sign in to comment.