Skip to content

Commit

Permalink
v1.3.2 修复阳历推移错误的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Feb 20, 2023
1 parent 8fb86c0 commit 60dde2c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 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.3.1</version>
<version>1.3.2</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.3.1</version>
<version>1.3.2</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.3.1</version>
<version>1.3.2</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
66 changes: 40 additions & 26 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public Solar(int year, int month, int day, int hour, int minute, int second) {
throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", year, month, day));
}
}
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException(String.format("wrong hour %d", hour));
if (month < 1 || month > 12) {
throw new IllegalArgumentException(String.format("wrong month %d", month));
}
if (day < 1 || day > 31) {
throw new IllegalArgumentException(String.format("wrong day %d", day));
}
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException(String.format("wrong minute %d", minute));
Expand Down Expand Up @@ -618,30 +621,35 @@ public int subtractMinute(Solar solar) {
public boolean isAfter(Solar solar) {
if (year > solar.getYear()) {
return true;
} else if (year < solar.getYear()) {
}
if (year < solar.getYear()) {
return false;
}
if (month > solar.getMonth()) {
return true;
} else if (month < solar.getMonth()) {
}
if (month < solar.getMonth()) {
return false;
}
if (day > solar.getDay()) {
return true;
} else if (day < solar.getDay()) {
}
if (day < solar.getDay()) {
return false;
}
if (hour > solar.getHour()) {
return true;
} else if (hour < solar.getHour()) {
}
if (hour < solar.getHour()) {
return false;
}
if (minute > solar.getMinute()) {
return true;
} else if (minute < solar.getMinute()) {
}
if (minute < solar.getMinute()) {
return false;
}
return second > solar.second;
return second > solar.getSecond();
}

/**
Expand All @@ -652,30 +660,35 @@ public boolean isAfter(Solar solar) {
public boolean isBefore(Solar solar) {
if (year > solar.getYear()) {
return false;
} else if (year < solar.getYear()) {
}
if (year < solar.getYear()) {
return true;
}
if (month > solar.getMonth()) {
return false;
} else if (month < solar.getMonth()) {
}
if (month < solar.getMonth()) {
return true;
}
if (day > solar.getDay()) {
return false;
} else if (day < solar.getDay()) {
}
if (day < solar.getDay()) {
return true;
}
if (hour > solar.getHour()) {
return false;
} else if (hour < solar.getHour()) {
}
if (hour < solar.getHour()) {
return true;
}
if (minute > solar.getMinute()) {
return false;
} else if (minute < solar.getMinute()) {
}
if (minute < solar.getMinute()) {
return true;
}
return second < solar.second;
return second < solar.getSecond();
}

/**
Expand All @@ -691,8 +704,7 @@ public Solar nextYear(int years) {
if (2 == m) {
if (d > 28) {
if (!SolarUtil.isLeapYear(y)) {
d -= 28;
m++;
d = 28;
}
}
}
Expand All @@ -719,8 +731,7 @@ public Solar nextMonth(int months) {
if (2 == m) {
if (d > 28) {
if (!SolarUtil.isLeapYear(y)) {
d -= 28;
m++;
d = 28;
}
}
}
Expand All @@ -742,33 +753,36 @@ public Solar next(int days) {
int y = year;
int m = month;
int d = day;
if (1582 == y && 10 == m) {
if (d > 4) {
d -= 10;
}
}
if (days > 0) {
d = day + days;
d += days;
int daysInMonth = SolarUtil.getDaysOfMonth(y, m);
while (d > daysInMonth) {
d -= daysInMonth;
m++;
if (m > 12) {
m -= 12;
m = 1;
y++;
}
daysInMonth = SolarUtil.getDaysOfMonth(y, m);
}
} else if (days < 0) {
int rest = -days;
while (d <= rest) {
rest -= d;
while (d + days <= 0) {
m--;
if (m < 1) {
m = 12;
y--;
}
d = SolarUtil.getDaysOfMonth(y, m);
d += SolarUtil.getDaysOfMonth(y, m);
}
d -= rest;
d += days;
}
if (1582 == y && 10 == m) {
if (d > 4 && d < 15) {
if (d > 4 ) {
d += 10;
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/test/SolarTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test;

import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import com.nlf.calendar.util.SolarUtil;
import org.junit.Assert;
Expand Down Expand Up @@ -145,4 +146,29 @@ public void test22(){
Assert.assertEquals(355, days);
}

@Test
public void test23(){
Solar solar = Solar.fromYmd(1991, 5, 12);
Lunar lunar = solar.getLunar();
Assert.assertEquals("壬午", lunar.getDayInGanZhi());
}

@Test
public void test24(){
Solar solar = new Solar(1582, 10, 15);
Assert.assertEquals("1582-09-30", solar.next(-5).toYmd());
}

@Test
public void test25(){
Solar solar = new Solar(1582, 10, 15);
Assert.assertEquals("1582-10-04", solar.next(-1).toYmd());
}

@Test
public void test26(){
Solar solar = new Solar(1582, 10, 15);
Assert.assertEquals("1582-09-29", solar.next(-6).toYmd());
}

}

0 comments on commit 60dde2c

Please sign in to comment.