1.某些业务场景需要获取全年哪些天是节假日还是非节假日 比如说出勤。
2.我介绍的这种方式是通过Api调用的方式,通过当前日期来查询是节假日还是非节假日
http://api.goseek.cn/Tools/holiday?date=" + 日期 data为0则是节假日,data为0 则是休息日 1为节假日
3.代码
package com.bos.test;
import com.alibaba.fastjson.JSONObject;
import com.bos.common.SplitTime;
import com.bos.util.SendRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
try {
getAllHolidayDate();
} catch (Exception e) {
}
}
/**
* 1.获取本年的第一天 获取本年最后一天
* 2.遍历第一天到最后一天的数据,通过调用Api接口的方式获取当前是否为工作日
*
* @throws Exception
*/
public static void getAllHolidayDate() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//获取本年第一天
Date startDate = SplitTime.getCurrYearFirst();
//获取本年最后一天
Date endDate = SplitTime.getCurrYearLast();
//遍历全年日期
List<Date> lists = SplitTime.dateSplit(startDate, endDate);
if (!lists.isEmpty()) {
for (Date date : lists) {
String currentDate = sdf.format(date);
System.out.println("当前日期---" + currentDate);
String url = "http://api.goseek.cn/Tools/holiday?date=" + currentDate;
JSONObject dateTypes = SendRequest.sendGet2(url);
String data = dateTypes.getString("data");
if (data.equals("0")) {
System.out.println("工作日");
} else {
System.out.println("非工作日");
}
//获取当前日期属于第几周
int weekType = SplitTime.getCurrentDateBelongToWhatWeek(currentDate);
System.out.println("当前日期属于第几周---" + weekType);
}
}
}
}
4.用来操作时间的类
package com.bos.common;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 功能描述: 用来操作时间的类
* @Author: tanghh18
* @Date: 2019/9/8 10:44
*/
public class SplitTime {
/**
* 用来计算俩个日期之前的天数
* @param startDate
* @param endDate
* @return
* @throws Exception
*/
public static List<Date> dateSplit(Date startDate, Date endDate)
throws Exception {
if (!startDate.before(endDate))
throw new Exception("开始时间应该在结束时间之后");
Long spi = endDate.getTime() - startDate.getTime();
// 相隔天数
Long step = spi / (24 * 60 * 60 * 1000);
List<Date> dateList = new ArrayList<Date>();
dateList.add(endDate);
for (int i = 1; i <= step; i++) {
// 比上一天减一
dateList.add(new Date(dateList.get(i - 1).getTime()
- (24 * 60 * 60 * 1000)));
}
return dateList;
}
/**
* 获取当年的第一天
* @return
*/
public static Date getCurrYearFirst(){
Calendar currCal=Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearFirst(currentYear);
}
/**
* 获取某年第一天日期
* @param year 年份
* @return Date
*/
public static Date getYearFirst(int year){
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
/**
* 获取当年的最后一天
* @return
*/
public static Date getCurrYearLast(){
Calendar currCal=Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearLast(currentYear);
}
/**
* 获取某年最后一天日期
* @param year 年份
* @return Date
*/
public static Date getYearLast(int year){
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = calendar.getTime();
return currYearLast;
}
/**
* 获取当前日期属于第几周
*/
public static int getCurrentDateBelongToWhatWeek(String currentDate){
int weekOfYear;
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(currentDate);
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
}catch (Exception e){
return 0;
}
return weekOfYear;
}
}
5. SendGet2
public static JSONObject sendGet2(String url) {
JSONObject jsonObject = null;
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
String urlName = url;
URL realUrl = new URL(urlName);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setConnectTimeout(10000);
// 建立实际的连接
conn.connect();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
jsonObject = JSON.parseObject(sb.toString());
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
// 使用finally块来关闭输入流
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
System.out.println("关闭流异常");
}
}
return jsonObject;
}