Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package com.ibeetl.admin.console.util;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.enums.EvaluationLabelType;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service
public  class DateUtil {
 
    static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    final static Base64.Encoder encoder = Base64.getEncoder();
    static Base64.Decoder decoder = Base64.getDecoder();
 
 
    public static CoreUser getCurrentUser(CorePlatformService platformService){
        CoreUser user = platformService.getCurrentUser();
        return user;
    }
    public static String getNickName(String nickName){
        String text = null;
        try {
            text = new String(decoder.decode(nickName), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return text;
    }
    /**
     * java生成随机数字和字母组合10位数
     * @param length
     * @return
     */
    public static String getRandomNickname(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
 
    /**
     * AES对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。
     * 默认使用 :AES/ECB/PKCS5Padding
     * @param salt 必须是16位的
     * @param content  明文内容
     * @return 加密后的密码
     */
    public static String encrypt(String salt,String content){
        //构建
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, salt.getBytes());
        //加密
        String encrypt = aes.encryptBase64(content);
        return encrypt;
    }
 
    public static Date getDate(Date currentTime) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            ParsePosition pos = new ParsePosition(8);
            Date currentTime_2 = formatter.parse(dateString, pos);
            return currentTime_2;
         }
 
    public static List<Map<String,String>> query12WeekStartTimeAndEndTime(){
        List<Map<String, Date>> list = new ArrayList<>();
        for(int i = 0, h = 7; i <= 12; i++){
            list.add(getLastWeek(h * i));
        }
 
        List<Map<String, String>> result = new ArrayList<>();
        for(Map<String, Date> map : list){
            Map<String,String> resultMap = new HashMap<>();
            String startTime = sdf2.format(map.get("monday"))+" 00:00:00";
            String endTime = sdf2.format(map.get("sunday"))+" 23:59:59";
            resultMap.put("startTime",startTime);
            resultMap.put("endTime",endTime);
            result.add(resultMap);
        }
        return result;
    }
    public static Map<String, Date> getLastWeek(int days) {
        // TODO Auto-generated method stub
        Map<String, Date> map = new HashMap<String, Date>();
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        // 上周一的日期
        cal.add(Calendar.DATE, -(days + (n - 1)));
        Date monday = cal.getTime();
        map.put("monday", monday);
 
        cal.add(Calendar.DATE, 1);
        Date tuesday = cal.getTime();
        map.put("tuesday", tuesday);
 
        cal.add(Calendar.DATE, 1);
        Date wednesday = cal.getTime();
        map.put("wednesday", wednesday);
 
        cal.add(Calendar.DATE, 1);
        Date thursday = cal.getTime();
        map.put("thursday", thursday);
 
        cal.add(Calendar.DATE, 1);
        Date friday = cal.getTime();
        map.put("friday", friday);
 
        cal.add(Calendar.DATE, 1);
        Date saturday = cal.getTime();
        map.put("saturday", saturday);
 
        cal.add(Calendar.DATE, 1);
        Date sunday = cal.getTime();
        map.put("sunday", sunday);
        return map;
    }
 
 
    public static List<Map<String,String>> query12MonthStartTimeAndEndTime() {
        List<Map<String,String>> list = new ArrayList<>();
        String[] latest12Month = getLatest12Month();
        for(String s : latest12Month){
            Map<String, String> m = new HashMap<>();
            m.put("startTime", s + "-01 00:00:00");
            m.put("endTime", s + "-31 23:59:59");
            list.add(m);
        }
 
 
        for(int i=0;i<list.size();i++){
            for(int j=0;j<list.size()-i-1;j++){
                if(list.get(j).get("startTime").compareTo(list.get(j+1).get("startTime"))<0){
                    Map<String,String> r=list.get(j);
                    list.set(j, list.get(j+1));
                    list.set(j+1, r);
                }
            }
        }
        return list;
    }
 
    public static String fillZero(int i){
        String month = "";
        if(i<10){
            month = "0" + i;
        }else{
            month = String.valueOf(i);
        }
        return month;
    }
 
    public static String[] getLatest12Month(){
        String[] latest12Months = new String[12];
        Calendar cal = Calendar.getInstance();
        //要先+1,才能把本月的算进去
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
        for(int i=0; i<12; i++){
            //逐次往前推1个月
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
            latest12Months[11-i] = cal.get(Calendar.YEAR)+ "-" +fillZero(cal.get(Calendar.MONTH)+1);
        }
        return latest12Months;
    }
 
 
 
    public static List<Map<String,Object>> getMapList(List list,String entityType){
        List<Map<String,Object>> maplist =new ArrayList<>();
        for (Object t:list) {
            Map<String,Object> map= convertBeanToMap(t);
            maplist.add(map);
        }
        return maplist;
    }
    /**
     * 实体类转map
     * @param obj
     * @return
     */
    public static Map<String, Object> convertBeanToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(null==value){
                        map.put(key,"");
                    }else{
                        map.put(key,value);
                    }
                }
 
 
 
 
            }
        } catch (Exception e) {
            System.out.println("convertBean2Map Error {实体转map}"+e);
        }
        return map;
    }
 
    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate 较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
 
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
 
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    public static boolean isSorted(List<String> listOfStrings, int index) {
        if (index < 2) {
            return true;
        } else if (listOfStrings.get(index - 2).compareTo(listOfStrings.get(index - 1)) > 0) {
            System.out.println(listOfStrings.get(index - 2)+"============"+listOfStrings.get(index - 1));
            return false;
        } else {
            return isSorted(listOfStrings, index - 1);
        }
    }
 
    public static int getCaseByType(String type){
        if(EvaluationLabelType.ONE_TYPE.equals(type)){
            return 1;
        }else if(EvaluationLabelType.TWO_TYPE.equals(type)){
            return 2;
        }else if(EvaluationLabelType.THREE_TYPE.equals(type)){
            return 3;
        }else if(EvaluationLabelType.FOUR_TYPE.equals(type)){
            return 4;
        }else if(EvaluationLabelType.FIVE_TYPE.equals(type)){
            return 5;
        }else if(EvaluationLabelType.SIX_TYPE.equals(type)){
            return 6;
        }else if(EvaluationLabelType.SENVEN_TYPE.equals(type)){
            return 7;
        }else if(EvaluationLabelType.EIGHT_TYPE.equals(type)){
            return 8;
        }else if(EvaluationLabelType.NINE_TYPE.equals(type)){
            return 9;
        }else if(EvaluationLabelType.TEN_TYPE.equals(type)){
            return 10;
        }else if(EvaluationLabelType.ELE_TYPE.equals(type)){
            return 11;
        }else if(EvaluationLabelType.TELF_TYPE.equals(type)){
            return 12;
        }else if(EvaluationLabelType.THR_TYPE.equals(type)){
            return 13;
        }else if(EvaluationLabelType.FOR_TYPE.equals(type)){
            return 14;
        }else if(EvaluationLabelType.FIV_TYPE.equals(type)){
            return 15;
        }else if(EvaluationLabelType.SI_TYPE.equals(type)){
            return 16;
        }else if(EvaluationLabelType.SEV_TYPE.equals(type)){
            return 17;
        }else if(EvaluationLabelType.EIG_TYPE.equals(type)){
            return 18;
        }else if(EvaluationLabelType.NIG_TYPE.equals(type)){
            return 19;
        }
        return 0;
    }
 
    public static boolean isBase64(String str) {
        if (str == null || str.trim().length() == 0) {
            return false;
        } else {
            if (str.length() % 4 != 0) {
                return false;
            }
 
            char[] strChars = str.toCharArray();
            for (char c:strChars) {
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
                        || c == '+' || c == '/' || c == '=') {
                    continue;
                } else {
                    return false;
                }
            }
            return true;
        }
    }
 
    public  static String generate(String prefix,long datacenterId){
        Snowflake snowflake = IdUtil.createSnowflake(Convert.toLong(0), datacenterId);
        return prefix+snowflake.nextIdStr();
    }
 
   /* public static void main(String[] args) {
        String now = sdf1.format(new Date());
        System.out.println(now);
        int b = checkBig(now,"2019-12-31 14:00:00");
        System.out.println(b);
    }*/
    /**
     *
     * @param s1  时间字符串  注意时间格式要一样哦,不一样你比个毛线
     * @param s2 时间字符串
     * @return 1是s1大    2是s2大   0是俩个一样大   -1是出错了吧
     */
    public static int checkBig(String s1,String s2){
        try {
            if(s1.compareTo(s2) >0){
                return 1;
            }else if(s1.compareTo(s2) <0){
                return 2;
            }else{
                return 0;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        }
    }
 
    public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
 
    public static String dayForWeek(String pTime) throws Throwable {
 
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 
        Date tmpDate = format.parse(pTime);
 
        Calendar cal = Calendar.getInstance();
 
        String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
 
        try {
 
            cal.setTime(tmpDate);
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
 
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
 
        if (w < 0){
            w = 0;
        }
        return weekDays[w];
 
    }
 
    public static int getDayHour() throws Throwable {
        Calendar cal=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        int hour=cal.get(Calendar.HOUR_OF_DAY);
        return hour;
    }
 
}