@ -3,92 +3,188 @@ import java.io.BufferedReader;
import java.io.InputStreamReader ;
import java.net.URL ;
import java.net.URLConnection ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.* ;
import com.alibaba.fastjson.JSONObject ;
import com.ccsens.util.cron.CronConstant ;
public class DateTest {
public static String sendGet ( String url , String param ) {
String result = "" ;
BufferedReader in = null ;
try {
String urlNameString = url + "/" + param ;
URL realUrl = new URL ( urlNameString ) ;
URLConnection connection = realUrl . openConnection ( ) ;
connection . setRequestProperty ( "accept" , "*/*" ) ;
connection . setRequestProperty ( "connection" , "Keep-Alive" ) ;
connection . setRequestProperty ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" ) ;
connection . connect ( ) ;
in = new BufferedReader ( new InputStreamReader ( connection . getInputStream ( ) ) ) ;
String line ;
int cot = 0 ;
while ( ( line = in . readLine ( ) ) ! = null ) {
result + = line ;
}
} catch ( Exception e ) {
System . out . println ( "发送GET请求出现异常!" + e ) ;
e . printStackTrace ( ) ;
}
finally {
try {
if ( in ! = null ) {
in . close ( ) ;
}
} catch ( Exception e2 ) {
e2 . printStackTrace ( ) ;
}
//
// public static String sendGet(String url, String param) {
// String result = "";
// BufferedReader in = null;
// try {
// String urlNameString = url + "/" + param;
// URL realUrl = new URL(urlNameString);
// URLConnection connection = realUrl.openConnection();
// connection.setRequestProperty("accept", "*/*");
// connection.setRequestProperty("connection", "Keep-Alive");
// connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// connection.connect();
// in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// String line;
// int cot = 0;
// while ((line = in.readLine()) != null) {
// result += line;
// }
// } catch (Exception e) {
// System.out.println("发送GET请求出现异常!" + e);
// e.printStackTrace();
// }
// finally {
// try {
// if (in != null) {
// in.close();
// }
// } catch (Exception e2) {
// e2.printStackTrace();
// }
// }
// return result;
// }
//
// static Map totalMap = new TreeMap();
//
// public static void main(String[] args) throws Exception {
//
// int startYear = 2021;//开始的年份
// int year = 8; //统计的年份
// loop("2021", "8");
//// for(int i = 0; i <= (year - 1); i++){
////// for(int j = 1; j <= 12; j++) {
//// loop(startYear + i + "", j + "");
////// }
//// }
//
// Iterator it = totalMap.keySet().iterator();
// while(it.hasNext()) {
// String temp = (String) it.next();
// System.out.println(temp + " " + totalMap.get(temp));
// }
//
// }
//
// public static void loop(String year, String month) throws Exception {
// String s = DateTest.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");
//
// String a = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776";
// long a1 = System.currentTimeMillis();
// RestTemplateUtil.getForEntity(a,new HashMap<>(),StringBuilder.class);
// System.out.println(System.currentTimeMillis() - a1);
//
// s = s.substring(s.indexOf("("));
// s = s.substring(1, s.length() - 2);
// Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);
//
// List list = (List) map.get("data");
// Map data = (Map) list.get(0);
// List<Map> almanac = (List<Map>) data.get("almanac");
// if(almanac == null || almanac.size() == 0) {
// return;
// }
//
// for(int i = 1; i < almanac.size(); i++) {
// String key = ((String)almanac.get(i).get("oDate")).substring(0, 10);
// String status = (String)almanac.get(i - 1).get("status");
// if("1".equals(status) || "2".equals(status)) {
// totalMap.put(key, status);
// }
// }
//
// }
/ *
* 实现思路
* 1 . 判断指定日期是否掉是双休日
* 2 . 在通过获取指定日期的 月和日 判断是否是节假
* 3 . 判断是否有节假日 双休加班情况
* 4 . 如果上面的你已经了解 , 那么得到工作日就简单了 , 不写了 , 自己通过需求加上
* /
public void isOverDay ( ) { // 指定时间判断是否是双休日
String dates = "2013-05-13" ; // 这里可以自定义
DateFormat df = new SimpleDateFormat ( "yy-MM-dd" ) ;
Date d ;
try {
d = df . parse ( "" ) ;
if ( d . getDay ( ) = = 0 | | d . getDay ( ) = = 6 ) {
System . out . println ( "日期:[" + dates + "] 是双休日" ) ;
} else {
System . out . println ( "日期:[" + dates + "] 不是双休日" ) ;
}
return result ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
static Map totalMap = new TreeMap ( ) ;
public static void main ( String [ ] args ) throws Exception {
int startYear = 2021 ; //开始的年份
int year = 8 ; //统计的年份
loop ( "2021" , "8" ) ;
// for(int i = 0; i <= (year - 1); i++){
//// for(int j = 1; j <= 12; j++) {
// loop(startYear + i + "", j + "");
//// }
// }
public static void isHodliDays ( List < CronConstant . TaskDate > taskDates , Date startDate , Date endDate ) { // 判断是否滴节假日,是否有节假日加班
DateFormat df = new SimpleDateFormat ( "MM-dd" ) ;
//中国法定节假日期
String isHoliday = "01-01,01-02,01-03,02-11,02-12,02-13,02-14,02-15,02-16,"
+ "02-17,04-03,04-04,04-05,05-01,05-02,05-03,05-04,05-05,06-12,06-13,"
+ "06-14,09-19,09-20,09-21,10-01,10-02,10-03,10-04,10-05,10-06,10-07" ;
// 节假前后加班日期
String overDay = "02-07,02-20,04-25,05-08,09-18,09-26,10-09" ;
Calendar min = Calendar . getInstance ( ) ;
Calendar max = Calendar . getInstance ( ) ;
min . setTime ( startDate ) ;
max . setTime ( endDate ) ;
while ( min . before ( max ) ) {
int i = max . get ( Calendar . DAY_OF_WEEK ) ;
//是调休 || (不是周末 && 不是节假日)
if ( overDay . contains ( df . format ( max . getTime ( ) ) ) | | ( ( i ! = 1 & & i ! = 7 ) & & ! isHoliday . contains ( df . format ( max . getTime ( ) ) ) ) ) {
System . out . println ( df . format ( max . getTime ( ) ) + "工作日" ) ;
Iterator it = totalMap . keySet ( ) . iterator ( ) ;
while ( it . hasNext ( ) ) {
String temp = ( String ) it . next ( ) ;
System . out . println ( temp + " " + totalMap . get ( temp ) ) ;
CronConstant . TaskDate taskDate = new CronConstant . TaskDate ( ) ;
taskDate . setStartDate ( DateUtil . getYMD ( max . getTime ( ) ) ) ;
max . add ( Calendar . DATE , - 1 ) ;
taskDate . setEndDate ( DateUtil . getYMD ( max . getTime ( ) ) ) ;
taskDates . add ( taskDate ) ;
break ;
}
max . add ( Calendar . DATE , - 1 ) ;
}
}
public static void loop ( String year , String month ) throws Exception {
String s = DateTest . sendGet ( "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + year + "%E5%B9%B4" + month + "%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776" , "" ) ;
public static void main ( String [ ] args ) throws Exception {
List < CronConstant . TaskDate > taskDates = new ArrayList < > ( ) ;
Date s = new Date ( 1628148817000L ) ;
Date e = new Date ( 1633851217000L ) ;
String a = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=" + year + "%E5%B9%B4" + month + "%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776" ;
long a1 = System . currentTimeMillis ( ) ;
RestTemplateUtil . getForEntity ( a , new HashMap < > ( ) , StringBuilder . class ) ;
System . out . println ( System . currentTimeMillis ( ) - a1 ) ;
DateFormat df = new SimpleDateFormat ( "MM-dd" ) ;
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTime ( s ) ;
while ( calendar . getTime ( ) . getTime ( ) < e . getTime ( ) ) {
// //获取月开始时间
// Date start = calendar.getTime();
// //获取月结束时间
// calendar.add(Calendar.MONTH, 1);
// calendar.set(Calendar.DAY_OF_MONTH, 0);
// Date end = calendar.getTime().getTime() > e.getTime() ? e : calendar.getTime();
// //获取当月最后一个工作日
// isHodliDays(taskDates,start,end);
// //加一个月(为下一个循环)
// calendar.add(Calendar.MONTH, 1);
// calendar.set(Calendar.DAY_OF_MONTH, 1);
s = s . substring ( s . indexOf ( "(" ) ) ;
s = s . substring ( 1 , s . length ( ) - 2 ) ;
Map < String , Object > map = ( Map < String , Object > ) JSONObject . parse ( s ) ;
List list = ( List ) map . get ( "data" ) ;
Map data = ( Map ) list . get ( 0 ) ;
List < Map > almanac = ( List < Map > ) data . get ( "almanac" ) ;
if ( almanac = = null | | almanac . size ( ) = = 0 ) {
return ;
}
System . out . println ( df . format ( calendar . getTime ( ) ) ) ;
Date start = calendar . getTime ( ) ;
calendar . add ( Calendar . WEEK_OF_YEAR , 1 ) ;
calendar . set ( Calendar . DAY_OF_WEEK , 1 ) ;
Date end = calendar . getTime ( ) ;
//获取当月最后一个工作日
isHodliDays ( taskDates , start , end ) ;
for ( int i = 1 ; i < almanac . size ( ) ; i + + ) {
String key = ( ( String ) almanac . get ( i ) . get ( "oDate" ) ) . substring ( 0 , 10 ) ;
String status = ( String ) almanac . get ( i - 1 ) . get ( "status" ) ;
if ( "1" . equals ( status ) | | "2" . equals ( status ) ) {
totalMap . put ( key , status ) ;
}
}
System . out . println ( df . format ( calendar . getTime ( ) ) ) ;
calendar . add ( Calendar . DAY_OF_MONTH , 1 ) ;
System . out . println ( df . format ( calendar . getTime ( ) ) ) ;
System . out . println ( "====================================" ) ;
// System.out.println(df.format(calendar.getTime()));
}
System . out . println ( taskDates ) ;
}
}