mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.dg.core.util;
 
 
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.iceyyy.workday.WorkUtils;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
 
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>TestDate 此类用于:</p>
 * <p>@author:hujm</p>
 * <p>@date:2021年04月22日 17:43</p>
 * <p>@remark:</p>
 */
public class TestDate {
 
    public static void main(String[] args) {
        System.out.println(getJjr(2021, 4));
        System.out.println(getMonthWekDay(2021, 4));
        System.out.println(JJR(2021, 4));
 
    }
 
    /**
     * 获取周末和节假日
     *
     * @param year
     * @param month
     * @return
     */
    public static Set<String> JJR(int year, int month) {
        //获取所有的周末
        Set<String> monthWekDay = getMonthWekDay(year, month);
        //http://timor.tech/api/holiday api文档地址
        Map jjr = getJjr(year, month + 1);
        Integer code = (Integer) jjr.get("code");
        if (code != 0) {
            return monthWekDay;
        }
        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.get("holiday");
        Set<String> strings = holiday.keySet();
        for (String str : strings) {
            Map<String, Object> stringObjectMap = holiday.get(str);
            Integer wage = (Integer) stringObjectMap.get("wage");
            String date = (String) stringObjectMap.get("date");
            //筛选掉补班
            if (wage.equals(1)) {
                monthWekDay.remove(date);
            } else {
                monthWekDay.add(date);
            }
        }
        return monthWekDay;
    }
 
    /**
     * 获取节假日不含周末
     *
     * @param year
     * @param month
     * @return
     */
    private static Map getJjr(int year, int month) {
        String url = "http://timor.tech/api/holiday/year/";
        OkHttpClient client = new OkHttpClient();
        Response response;
        //解密数据
        String rsa = null;
        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        try {
            response = client.newCall(request).execute();
            rsa = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(rsa, Map.class);
    }
 
    /**
     * 获取节假日不含周末
     *
     * @return
     */
    public    Map getWeather(String code) {
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=9e0d819935da8a01de0e476ba8a9019e&city="+code;
        OkHttpClient client = new OkHttpClient();
        Response response;
        //解密数据
        String rsa = null;
        Request request = new Request.Builder()
                .url(url)
                .get()
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        try {
            response = client.newCall(request).execute();
            rsa = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.parseObject(rsa, Map.class);
    }
 
    /**
     * 获取周末  月从0开始
     *
     * @param year
     * @param mouth
     * @return
     */
    public static Set<String> getMonthWekDay(int year, int mouth) {
        Set<String> dateList = new HashSet<>();
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar(year, mouth, 1);
        Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
        endCalendar.add(Calendar.MONTH, 1);
        while (true) {
            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
            if (weekday == 1 || weekday == 7) {
                dateList.add(simdf.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
            if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
                break;
            }
        }
        return dateList;
    }
 
    /**
     * 判断该日期是否为工作日
     *
     * @param dateTime
     * @return
     */
    public Boolean  isWeekday(DateTime dateTime){
        TestDate td=new TestDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        Set<String> jjr = td.JJR(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH));
        String strdate = dateTime.toString("yyyy-MM-dd");
        for (String ans: jjr ) {
            if (ans.equals(strdate))
                return  false;
        }
        return  true;
    }
 
    /**
     * 获取该日期的下个工作日是多少
     *
     * @param dateTime
     * @return
     */
    public  DateTime nextWeekDay(DateTime dateTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        while (true){
          calendar.add(calendar.DATE,1);
          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
          String dateTimeToday = sdf.format(calendar.getTime());
          if (!WorkUtils.isWorkendDay(dateTimeToday)){
              return new DateTime(calendar);//返回日期
          }
        }
 
    }
 
}