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

Compare commits

...

4 Commits

16 changed files with 341 additions and 100 deletions

3
.gitignore vendored
View File

@@ -13,4 +13,5 @@ target/
*.cache
*.diff
*.patch
*.tmp
*.tmp
.DS_Store

View File

@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.2.25</version>
<version>1.2.29</version>
</dependency>
```
@@ -53,4 +53,4 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
## 文档
请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html")
请移步至 [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html")

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.25</version>
<version>1.2.29</version>
</dependency>
```
@@ -49,4 +49,4 @@ Output:
## Documentation
Please visit [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html")
Please visit [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html")

View File

@@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.2.25</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>
@@ -105,6 +105,7 @@
</goals>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
<source>${source-version}</source>
</configuration>
</execution>
</executions>
@@ -148,7 +149,9 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}-${project.version}</finalName>

View File

@@ -52,27 +52,7 @@ public class ExactDate {
* @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);
}
/**
@@ -83,6 +63,6 @@ public class ExactDate {
* @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));
}
}

View File

@@ -2049,13 +2049,16 @@ public class Lunar {
}
protected NineStar getYearNineStar(String yearInGanZhi) {
int index = LunarUtil.getJiaZiIndex(yearInGanZhi) + 1;
int yearOffset = 0;
if (index != LunarUtil.getJiaZiIndex(this.getYearInGanZhi()) + 1) {
yearOffset = -1;
int indexExact = LunarUtil.getJiaZiIndex(yearInGanZhi) + 1;
int index = LunarUtil.getJiaZiIndex(this.getYearInGanZhi()) + 1;
int yearOffset = indexExact - index;
if (yearOffset > 1) {
yearOffset -= 60;
} else if (yearOffset < -1) {
yearOffset += 60;
}
int yuan = ((this.year + yearOffset + 2696) / 60) % 3;
int offset = (62 + yuan * 3 - index) % 9;
int offset = (62 + yuan * 3 - indexExact) % 9;
if (0 == offset) {
offset = 9;
}
@@ -2990,7 +2993,11 @@ public class Lunar {
}
Solar startSolar = jieQi.getSolar();
int days = ExactDate.getDaysBetween(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay(), solar.getYear(), solar.getMonth(), solar.getDay());
return LunarUtil.WU_HOU[(offset * 3 + days / 5) % LunarUtil.WU_HOU.length];
int index = days / 5;
if (index > 2) {
index = 2;
}
return LunarUtil.WU_HOU[(offset * 3 + index) % LunarUtil.WU_HOU.length];
}
/**

View File

@@ -277,64 +277,38 @@ public class Solar {
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;
}

View File

@@ -122,6 +122,25 @@ public class SolarMonth {
return l;
}
/**
* 获取本月的阳历周列表
* @param start 星期几作为一周的开始,1234560分别代表星期一至星期天
* @return 周列表
*/
public List<SolarWeek> getWeeks(int start) {
List<SolarWeek> l = new ArrayList<SolarWeek>();
SolarWeek week = SolarWeek.fromYmd(year, month, 1, start);
while (true) {
l.add(week);
week = week.next(1, false);
Solar firstDay = week.getFirstDay();
if (firstDay.getYear() > year || firstDay.getMonth() > month) {
break;
}
}
return l;
}
/**
* 获取往后推几个月的阳历月,如果要往前推,则月数用负数
*

File diff suppressed because one or more lines are too long

View File

@@ -157,6 +157,9 @@ public class SolarUtil {
* @return 天数
*/
public static int getDaysOfYear(int year) {
if (1582 == year) {
return 355;
}
return isLeapYear(year) ? 366 : 365;
}
@@ -193,10 +196,15 @@ public class SolarUtil {
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;
}
@@ -213,4 +221,64 @@ public class SolarUtil {
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;
}
}

View File

@@ -6,6 +6,7 @@ import com.nlf.calendar.Solar;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
@@ -250,12 +251,71 @@ public class BaZiTest {
@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("壬寅","庚戌","己未","乙亥");
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
@@ -295,4 +355,14 @@ public class BaZiTest {
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
}
@Test
public void test13(){
Lunar lunar = Lunar.fromYmdHms(1991, 4, 5, 3, 37, 0);
EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("年柱", "辛未", eightChar.getYear());
Assert.assertEquals("月柱", "癸巳", eightChar.getMonth());
Assert.assertEquals("日柱", "戊子", eightChar.getDay());
Assert.assertEquals("时柱", "甲寅", eightChar.getTime());
}
}

View File

@@ -161,18 +161,6 @@ public class LunarTest {
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);
@@ -459,4 +447,16 @@ public class LunarTest {
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());
}
}

View File

@@ -0,0 +1,26 @@
package test;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import org.junit.Assert;
import org.junit.Test;
public class NineStarTest {
@Test
public void test1() {
Lunar lunar = Solar.fromYmd(1985, 2, 19).getLunar();
Assert.assertEquals("六", lunar.getYearNineStar().getNumber());
}
@Test
public void test2() {
Lunar lunar = Lunar.fromYmd(2022, 1, 1);
Assert.assertEquals("六白金开阳", lunar.getYearNineStar().toString());
}
@Test
public void test3() {
Lunar lunar = Lunar.fromYmd(2033, 1, 1);
Assert.assertEquals("四绿木天权", lunar.getYearNineStar().toString());
}
}

View File

@@ -79,4 +79,70 @@ public class SolarTest {
Assert.assertEquals("全国中小学生安全教育日",solar.getFestivals().get(0));
}
@Test
public void test12(){
Solar solar = new Solar(2022, 1, 1);
Assert.assertEquals("2022-01-02", solar.next(1).toYmd());
}
@Test
public void test13(){
Solar solar = new Solar(2022, 1, 31);
Assert.assertEquals("2022-02-01", solar.next(1).toYmd());
}
@Test
public void test14(){
Solar solar = new Solar(2022, 1, 1);
Assert.assertEquals("2023-01-01", solar.next(365).toYmd());
}
@Test
public void test15(){
Solar solar = new Solar(2023, 1, 1);
Assert.assertEquals("2022-01-01", solar.next(-365).toYmd());
}
@Test
public void test16(){
Solar solar = new Solar(1582, 10, 4);
Assert.assertEquals("1582-10-15", solar.next(1).toYmd());
}
@Test
public void test17(){
Solar solar = new Solar(1582, 10, 4);
Assert.assertEquals("1582-11-01", solar.next(18).toYmd());
}
@Test
public void test18(){
Solar solar = new Solar(1582, 11, 1);
Assert.assertEquals("1582-10-04", solar.next(-18).toYmd());
}
@Test
public void test19(){
Solar solar = new Solar(1582, 11, 1);
Assert.assertEquals("1582-10-15", solar.next(-17).toYmd());
}
@Test
public void test20(){
int days = SolarUtil.getDaysBetween(1582, 10, 4, 1582, 10, 15);
Assert.assertEquals(1, days);
}
@Test
public void test21(){
int days = SolarUtil.getDaysBetween(1582, 10, 4, 1582, 11, 1);
Assert.assertEquals(18, days);
}
@Test
public void test22(){
int days = SolarUtil.getDaysBetween(1582, 1, 1, 1583, 1, 1);
Assert.assertEquals(355, days);
}
}

View File

@@ -1,5 +1,6 @@
package test;
import com.nlf.calendar.Solar;
import com.nlf.calendar.SolarWeek;
import com.nlf.calendar.util.SolarUtil;
import org.junit.Assert;
@@ -41,4 +42,16 @@ public class WeekTest {
//当周第一天(本月)
Assert.assertEquals("2019-05-01",week.getFirstDayInMonth().toString());
}
@Test
public void test1(){
Solar solar = Solar.fromYmd(1582, 10, 1);
Assert.assertEquals(1, solar.getWeek());
}
@Test
public void test2(){
Solar solar = Solar.fromYmd(1582, 10, 15);
Assert.assertEquals(5, solar.getWeek());
}
}

View File

@@ -103,4 +103,18 @@ public class WuHouTest {
Assert.assertEquals(solar.toString(),"小寒 初候",lunar.getHou());
}
@Test
public void test15(){
Solar solar = new Solar(2022,8,22);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"寒蝉鸣",lunar.getWuHou());
}
@Test
public void test16(){
Solar solar = new Solar(2022,8,23);
Lunar lunar = solar.getLunar();
Assert.assertEquals(solar.toString(),"鹰乃祭鸟",lunar.getWuHou());
}
}