package com.xzx.gc.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.xzx.gc.util.enums.EvaluationLabelType; 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.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Pattern; @Service public class DateUtil { public static List> query12WeekStartTimeAndEndTime(){ List> list = new ArrayList<>(); for(int i = 0, h = 7; i <= 12; i++){ list.add(getLastWeek(h * i)); } List> result = new ArrayList<>(); for(Map map : list){ Map resultMap = new HashMap<>(); String startTime = cn.hutool.core.date.DateUtil.formatDate(map.get("monday"))+" 00:00:00"; String endTime = cn.hutool.core.date.DateUtil.formatDate(map.get("sunday"))+" 23:59:59"; resultMap.put("startTime",startTime); resultMap.put("endTime",endTime); result.add(resultMap); } return result; } public static Map getLastWeek(int days) { Map map = new HashMap(); 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> query12MonthStartTimeAndEndTime() { List> list = new ArrayList<>(); String[] latest12Month = getLatest12Month(); for(String s : latest12Month){ Map 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 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 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 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) { e.printStackTrace(); return -1; } } 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 boolean isInteger(String s) { if (s != null && !"".equals(s.trim())){ return s.matches("^[0-9]*$"); } else{ return false; } } public static int getDayHour() { Calendar cal=Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); int hour=cal.get(Calendar.HOUR_OF_DAY); return hour; } }