java 判定是否是节假日、休息日,是否是调休休息日Holiday
时间: 2023-07-10 16:39:06 浏览: 183
Java中可以使用Jollyday和Holiday等第三方库来判断某一天是否是节假日、休息日和调休休息日。以下是使用Holiday库判断某一天是否是节假日、休息日和调休休息日的Java代码示例:
```java
import com.github.dreamhead.moco.HttpServer;
import com.github.dreamhead.moco.Runner;
import com.github.dreamhead.moco.dsl.Http;
import com.github.dreamhead.moco.runner.JsonRunner;
import org.joda.time.LocalDate;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import static com.github.dreamhead.moco.Moco.*;
import static com.github.dreamhead.moco.Runner.runner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HolidayCheckerTest {
private static final String API_BASE_URL = "http://localhost:12306";
private static final RestTemplate restTemplate = new RestTemplate();
private static Runner runner;
@BeforeClass
public static void setUp() {
HttpServer server = httpServer(12306);
server.request(by(uri("/holiday")))
.response(MocoJsonRunner.withJsonPath("{holiday_type: 1}"));
runner = runner(server);
runner.start();
}
@AfterClass
public static void tearDown() {
runner.stop();
}
@Test
public void testCheckHoliday() {
LocalDate date = new LocalDate(2021, 10, 1);
HolidayChecker holidayChecker = new HolidayChecker(API_BASE_URL, restTemplate);
HolidayType holidayType = holidayChecker.checkHoliday(date);
assert holidayType == HolidayType.HOLIDAY;
}
}
```
其中,HolidayChecker类的实现可以参考Jollyday和Holiday库的官方文档。这里使用了Moco库来模拟API接口,以便进行单元测试。在测试中,我们使用了期望的JSON响应结果来模拟API的返回值,然后使用RestTemplate来调用API接口,最后判断返回的结果是否符合预期。
阅读全文