Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85d56e5c3b | ||
|
|
b7fac53a81 | ||
|
|
72c4d0df71 |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.1.9</version>
|
<version>1.1.10</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.1.9</version>
|
<version>1.1.10</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>1.1.9</version>
|
<version>1.1.10</version>
|
||||||
<name>${project.groupId}:${project.artifactId}</name>
|
<name>${project.groupId}:${project.artifactId}</name>
|
||||||
<url>https://github.com/6tail/lunar-java</url>
|
<url>https://github.com/6tail/lunar-java</url>
|
||||||
<description>a calendar library for Solar and Chinese Lunar</description>
|
<description>a calendar library for Solar and Chinese Lunar</description>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.util.Calendar;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.nlf.calendar.util.HolidayUtil;
|
||||||
import com.nlf.calendar.util.LunarUtil;
|
import com.nlf.calendar.util.LunarUtil;
|
||||||
import com.nlf.calendar.util.SolarUtil;
|
import com.nlf.calendar.util.SolarUtil;
|
||||||
|
|
||||||
@@ -536,9 +537,36 @@ public class Solar{
|
|||||||
* @return 阳历日期
|
* @return 阳历日期
|
||||||
*/
|
*/
|
||||||
public Solar next(int days){
|
public Solar next(int days){
|
||||||
|
return next(days,false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Solar next(int days, boolean onlyWorkday){
|
||||||
Calendar c = Calendar.getInstance();
|
Calendar c = Calendar.getInstance();
|
||||||
c.set(year,month-1,day,hour,minute,second);
|
c.set(year,month-1,day,hour,minute,second);
|
||||||
c.add(Calendar.DATE,days);
|
if(0!=days) {
|
||||||
|
if(!onlyWorkday){
|
||||||
|
c.add(Calendar.DATE,days);
|
||||||
|
}else {
|
||||||
|
int rest = Math.abs(days);
|
||||||
|
int add = days < 1 ? -1 : 1;
|
||||||
|
while (rest > 0) {
|
||||||
|
c.add(Calendar.DATE, add);
|
||||||
|
boolean work = true;
|
||||||
|
Holiday holiday = HolidayUtil.getHoliday(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DAY_OF_MONTH));
|
||||||
|
if(null==holiday){
|
||||||
|
int week = c.get(Calendar.DAY_OF_WEEK);
|
||||||
|
if(1==week||7==week){
|
||||||
|
work = false;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
work = holiday.isWork();
|
||||||
|
}
|
||||||
|
if(work){
|
||||||
|
rest--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return new Solar(c);
|
return new Solar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
49
src/test/java/test/HolidayTest.java
Normal file
49
src/test/java/test/HolidayTest.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import com.nlf.calendar.util.HolidayUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class HolidayTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
Assert.assertEquals("2020-01-01 元旦节 2020-01-01",HolidayUtil.getHoliday("2020-01-01")+"");
|
||||||
|
|
||||||
|
// 将2020-01-01修改为春节
|
||||||
|
HolidayUtil.fix("202001011120200101");
|
||||||
|
Assert.assertEquals("2020-01-01 春节 2020-01-01",HolidayUtil.getHoliday("2020-01-01")+"");
|
||||||
|
|
||||||
|
// 追加2099-01-01为元旦节
|
||||||
|
HolidayUtil.fix("209901010120990101");
|
||||||
|
Assert.assertEquals("2099-01-01 元旦节 2099-01-01",HolidayUtil.getHoliday("2099-01-01")+"");
|
||||||
|
|
||||||
|
// 将2020-01-01修改为春节,并追加2099-01-01为元旦节
|
||||||
|
HolidayUtil.fix("202001011120200101209901010120990101");
|
||||||
|
Assert.assertEquals("2020-01-01 春节 2020-01-01",HolidayUtil.getHoliday("2020-01-01")+"");
|
||||||
|
Assert.assertEquals("2099-01-01 元旦节 2099-01-01",HolidayUtil.getHoliday("2099-01-01")+"");
|
||||||
|
|
||||||
|
// 更改节假日名称
|
||||||
|
String[] names = HolidayUtil.NAMES;
|
||||||
|
names[0] = "元旦";
|
||||||
|
names[1] = "大年初一";
|
||||||
|
|
||||||
|
HolidayUtil.fix(names, null);
|
||||||
|
Assert.assertEquals("2020-01-01 大年初一 2020-01-01",HolidayUtil.getHoliday("2020-01-01")+"");
|
||||||
|
Assert.assertEquals("2099-01-01 元旦 2099-01-01",HolidayUtil.getHoliday("2099-01-01")+"");
|
||||||
|
|
||||||
|
// 追加节假日名称和数据
|
||||||
|
names = new String[12];
|
||||||
|
for(int i=0,j=HolidayUtil.NAMES.length;i<j;i++){
|
||||||
|
names[i] = HolidayUtil.NAMES[i];
|
||||||
|
}
|
||||||
|
names[9] = "我的生日";
|
||||||
|
names[10] = "结婚纪念日";
|
||||||
|
names[11] = "她的生日";
|
||||||
|
|
||||||
|
HolidayUtil.fix(names, "20210529912021052920211111:12021111120211201;120211201");
|
||||||
|
Assert.assertEquals("2021-05-29 我的生日 2021-05-29",HolidayUtil.getHoliday("2021-05-29")+"");
|
||||||
|
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")+"");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,4 +20,27 @@ public class SolarTest {
|
|||||||
Assert.assertEquals("二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.getLunar().toFullString());
|
Assert.assertEquals("二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.getLunar().toFullString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNext(){
|
||||||
|
Solar date = new Solar(2020,1,23);
|
||||||
|
Assert.assertEquals("2020-01-24",date.next(1).toString());
|
||||||
|
// 仅工作日,跨越春节假期
|
||||||
|
Assert.assertEquals("2020-02-03",date.next(1,true).toString());
|
||||||
|
|
||||||
|
date = new Solar(2020,2,3);
|
||||||
|
Assert.assertEquals("2020-01-31",date.next(-3).toString());
|
||||||
|
// 仅工作日,跨越春节假期
|
||||||
|
Assert.assertEquals("2020-01-21",date.next(-3,true).toString());
|
||||||
|
|
||||||
|
date = new Solar(2020,2,9);
|
||||||
|
Assert.assertEquals("2020-02-15",date.next(6).toString());
|
||||||
|
// 仅工作日,跨越周末
|
||||||
|
Assert.assertEquals("2020-02-17",date.next(6,true).toString());
|
||||||
|
|
||||||
|
date = new Solar(2020,1,17);
|
||||||
|
Assert.assertEquals("2020-01-18",date.next(1).toString());
|
||||||
|
// 仅工作日,周日调休按上班算
|
||||||
|
Assert.assertEquals("2020-01-19",date.next(1,true).toString());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user