获取指定日期的节假日信息
相关pom依赖:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
HolidayTest:
package com.example.testdemo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* @Title: HolidayTest
* @Description: 验证日期是否为节假日或者周末
* @Auther:
* @Date: 2020/3/14 14:43
*
*/
public class HolidayTest {
/**
*
获取指定日期的节假日信息
接口地址:http://timor.tech/api/holiday/info/$date
@params $date: 指定日期的字符串,格式 ‘2018-02-23’。可以省略,则默认服务器的当前时间。
@return json: 如果不是节假日,holiday字段为null。
{
"code": 0, // 0服务正常。-1服务出错
"type": {
"type": enum(0, 1, 2, 3), // 节假日类型,分别表示 工作日、周末、节日、调休。
"name": "周六", // 节假日类型中文名,可能值为 周一 至 周日、假期的名字、某某调休。
"week": enum(1 - 7) // 一周中的第几天。值为 1 - 7,分别表示 周一 至 周日。
},
"holiday": {
"holiday": false, // true表示是节假日,false表示是调休
"name": "国庆前调休", // 节假日的中文名。如果是调休,则是调休的中文名,例如'国庆前调休'
"wage": 1, // 薪资倍数,1表示是1倍工资
"after": false, // 只在调休下有该字段。true表示放完假后调休,false表示先调休再放假
"target": '国庆节' // 只在调休下有该字段。表示调休的节假日
}
}
*/
public static void main(String[] args) {
//获取当前日期
//LocalDate date = LocalDate.now();
//DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
//测试数据
String httpArg = "2019-12-31";
try {
Date date = f.parse(httpArg);
}catch (Exception e){
e.printStackTrace();
}
//String httpArg = date.format(formatter);
Calendar c = Calendar.getInstance();
//判断今天是否是工作日 周末 还是节假日
int num =1;
System.out.println("预计发货需"+num+"天");
//String httpArg="2020-03-16";//f.format(new Date());
//String httpArg="$date";// 默认服务器的当前时间
System.out.println("日期:" + httpArg);
String n = null;
// if("0".equals(n)){
// System.out.println("***1***:"+httpArg+"是工作日");
// }else {
// System.out.println("该日期是非工作日");
// }
Date sDate = null;
try{
//当前日期
sDate = f.parse(httpArg);
//if (num !=0) {
for (int i = 0; i < num; i++) {
c.setTime(sDate);
c.add(Calendar.DAY_OF_MONTH, 1);
sDate = c.getTime();
System.out.println("#####Date+1##### " + f.format(sDate));
httpArg = f.format(sDate);
n = request(httpArg);
if (!"0".equals(n)) {
num = num + 1;
//System.out.println("***1***:"+httpArg+"是工作日");
}
}
// }
//sDate = f.parse(httpArg);
//long year = sDate.getYear()+1900;
//SimpleDateFormat f1=new SimpleDateFormat("MM月dd日");
//System.out.println("预计"+f1.format(sDate)+"天发货");
}catch (Exception e)