package cn.timer.api.controller.kqgl; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Set; /** * @author lal 2020-05-11 * */ public class ClockInTool { static SimpleDateFormat famt = new SimpleDateFormat("yyyy-MM-dd"); /*** * 去除String数组中的空值 */ public static String[] deleteArrayNull(String string[]) { String strArr[] = string; // step1: 定义一个list列表,并循环赋值 ArrayList<String> strList = new ArrayList<String>(); for (int i = 0; i < strArr.length; i++) { strList.add(strArr[i]); } // step2: 删除list列表中所有的空值 while (strList.remove(null)); while (strList.remove("")); // step3: 把list列表转换给一个新定义的中间数组,并赋值给它 String strArrLast[] = strList.toArray(new String[strList.size()]); return strArrLast; } /** * @param timeStr 修改的时间 * @param num 修改数字 * @param numtime * @return 1:增加一年、2:增加一天、3:减10天、4:增加一个月 */ public static String requires_extra_times(String timeStr, int num,int numtime,int daft) { DateFormat df; if(daft == 1) { df = new SimpleDateFormat("yyyy-MM-dd"); }else { df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } Date date = null; try { date = df.parse(timeStr); } catch (ParseException e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); cal.setTime(date);// 设置起时间 if(numtime == 1) { cal.add(Calendar.YEAR, num);// 增加一年 }else if(numtime == 2) { cal.add(Calendar.DATE, num);//增加一天 }else if(numtime == 3) { cal.add(Calendar.DATE, num);//减10天 }else if(numtime == 4){ cal.add(Calendar.MONTH, num);//增加一个月 }else if(numtime == 5) { cal.add(Calendar.HOUR, num); } return df.format(cal.getTime()); } /** * @param timeStr * @param addnumber * @return 通过java Calendar 实现时间+ 分钟 */ public static String addtime(String timeStr, String addnumber) { String str = null; try { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = df.parse(timeStr); // 时间累计 Calendar gc = new GregorianCalendar(); gc.setTime(date); gc.add(GregorianCalendar.MINUTE, Integer.parseInt(addnumber)); str = df.format(gc.getTime()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return str; } /** * 获取指定年月的第一天 * * @param year * @param month * @return */ public static String getFirstDayOfMonth1(int year, int month) { Calendar cal = Calendar.getInstance(); // 设置年份 cal.set(Calendar.YEAR, year); // 设置月份 cal.set(Calendar.MONTH, month - 1); // 获取某月最小天数 int firstDay = cal.getMinimum(Calendar.DATE); // 设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH, firstDay); // 格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } /** * 获取指定年月的最后一天 * * @param year * @param month * @return */ public static String getLastDayOfMonth1(int year, int month) { Calendar cal = Calendar.getInstance(); // 设置年份 cal.set(Calendar.YEAR, year); // 设置月份 cal.set(Calendar.MONTH, month - 1); // 获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DATE); // 设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); // 格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } // 数组容量的扩容,当空间(.length)不够的时候,增加一 public static String[] arrycopy(String[] ss) { // TODO Auto-generated method stub String[] ii = new String[ss.length + 1]; System.arraycopy(ss, 0, ii, 0, ss.length); return ii; } public static String[] replaceNull(String[] str) { // 用StringBuffer来存放数组中的非空元素,用“;”分隔 StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length; i++) { if ("null".equals(str[i])) { continue; } sb.append(str[i]); if (i != str.length - 1) { sb.append(";"); } } // 用String的split方法分割,得到数组 str = sb.toString().split(";"); return str; } /** * 删除数组中的指定值 或者数组中的元素包含指定值 * * @param filters 数组 * @param target 指定值 * @return */ public static String[] doChinFilters(String[] filters, String target) { String[] res = null; if (filters.length > 0) { List<String> tempList = Arrays.asList(filters); // Arrays.asList(filters) 迭代器实现类 不支持remove() 删除,多一步转化 List<String> arrList = new ArrayList<String>(tempList); Iterator<String> it = arrList.iterator(); while (it.hasNext()) { String x = it.next(); if (x.indexOf(target) != -1) { it.remove(); } } res = new String[arrList.size()]; arrList.toArray(res); } return res; } /** * 获取两个日期之间的所有日期 */ public static List<String> getDays(String startTime, String endTime) { // 返回的日期集合 List<String> days = new ArrayList<String>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date start = dateFormat.parse(startTime); Date end = dateFormat.parse(endTime); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(start); Calendar tempEnd = Calendar.getInstance(); tempEnd.setTime(end); tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束) while (tempStart.before(tempEnd)) { days.add(dateFormat.format(tempStart.getTime())); tempStart.add(Calendar.DAY_OF_YEAR, 1); } } catch (ParseException e) { e.printStackTrace(); } return days; } /** * 拼接字符串 */ public static String listToString(List<String> list) { if (list == null) { return null; } StringBuilder result = new StringBuilder(); boolean first = true; // 第一个前面不拼接"," for (String string : list) { if (first) { first = false; } else { result.append(","); } result.append(string); } return result.toString(); } /** * 使用java正则表达式去掉多余的.与0 * * @param s * @return string */ public static String replace(String s) { if (null != s && s.indexOf(".") > 0) { s = s.replaceAll("0+?$", "");// 去掉多余的0 s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉 } return s; } /** * 根据 年、月 获取对应的月份 的 天数 */ public static int getDaysByYearMonth(int year, int month) { Calendar a = Calendar.getInstance(); a.set(Calendar.YEAR, year); a.set(Calendar.MONTH, month - 1); a.set(Calendar.DATE, 1); a.roll(Calendar.DATE, -1); int maxDate = a.get(Calendar.DATE); return maxDate; } /** * 获取月份起始日期 * * @param date * @return * @throws ParseException */ public static String getMinMonthDate(String date) throws ParseException { Calendar calendar = Calendar.getInstance(); calendar.setTime(famt.parse(date)); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); return famt.format(calendar.getTime()); } /** * 获取月份最后日期 * * @param date * @return * @throws ParseException */ public static String getMaxMonthDate(String date) throws ParseException { Calendar calendar = Calendar.getInstance(); calendar.setTime(famt.parse(date)); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); return famt.format(calendar.getTime()); } /** * 对比两个字符串数组 * * @param t1 * @param t2 * @return */ public static <T> List<T> compare(T[] t1, T[] t2) { List<T> list1 = Arrays.asList(t1); // 将t1数组转成list数组 List<T> list2 = new ArrayList<T>();// 用来存放2个数组中不相同的元素 for (T t : t2) { if (!list1.contains(t)) { list2.add(t); } } return list2; } public static String[] array_unique(String[] ss) { Set<String> set = new HashSet<String>(Arrays.asList(ss)); return set.toArray(new String[set.size()]); // 或者return new HashSet<String>(Arrays.asList(ss)).toArray(new String[0]); } public static byte[] CreateBSCommBufferFromString(String sCmdParam, byte[] bytCmd) { try { if (sCmdParam.length() == 0) { return bytCmd; } byte[] bytText = sCmdParam.getBytes("UTF-8"); byte[] bytTextLen = int2byte(bytText.length + 1); bytCmd = new byte[4 + bytText.length + 1]; System.arraycopy(bytTextLen, 0, bytCmd, 0, bytTextLen.length); System.arraycopy(bytText, 0, bytCmd, 4, bytText.length); bytCmd[4 + bytText.length] = 0; return bytCmd; } catch (Exception e) { e.printStackTrace(); bytCmd = new byte[0]; return bytCmd; } } public static byte[] int2byte(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff); targets[1] = (byte) ((res >> 8) & 0xff); targets[2] = (byte) ((res >> 16) & 0xff); targets[3] = (byte) (res >>> 24); return targets; } public static byte[] ConcateByteArray(byte[] abytDest, byte[] abytSrc) { int len_dest = abytDest.length + abytSrc.length; if (abytSrc.length == 0) return abytDest; byte[] bytTmp = new byte[len_dest]; System.arraycopy(abytDest, 0, bytTmp, 0, abytDest.length); System.arraycopy(abytSrc, 0, bytTmp, abytDest.length, abytSrc.length); return bytTmp; } public static String dateToWeek2(String datetime) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; // String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"}; Calendar cal = Calendar.getInstance(); Date date; try { date = f.parse(datetime); cal.setTime(date); } catch (ParseException e) { e.printStackTrace(); } // 一周的第几天 int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } /** * * @param nowDate 要比较的时间 * @param startDate 开始时间 * @param endDate 结束时间 * @return true在时间段内,false不在时间段内 * @throws Exception */ public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate, String date_format) { SimpleDateFormat format = new SimpleDateFormat(date_format); Date now = null; Date start = null; Date end = null; try { now = format.parse(nowDate); start = format.parse(startDate); end = format.parse(endDate); } catch (ParseException e) { e.printStackTrace(); } long nowTime = now.getTime(); long startTime = start.getTime(); long endTime = end.getTime(); return nowTime >= startTime && nowTime <= endTime; } /** * 根据日期获取 星期 (2019-05-06 ——> 星期一) * * @param datetime * @return */ public static String dateToWeek(String datetime) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); // String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" }; Calendar cal = Calendar.getInstance(); Date date; try { date = f.parse(datetime); cal.setTime(date); } catch (ParseException e) { e.printStackTrace(); } // 一周的第几天 int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } /** * 2019-10-24T00:30:23.000Z 转化时间 */ public static String dealDateFormat(String oldDateStr, int num) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // yyyy-MM-dd'T'HH:mm:ss.SSSZ Date date = null; try { date = df.parse(oldDateStr); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK); Date date1 = null; try { date1 = df1.parse(date.toString()); } catch (ParseException e) { e.printStackTrace(); } DateFormat df2 = null; if (num == 1) { df2 = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"); } else if (num == 2) { df2 = new SimpleDateFormat("HH:mm"); } else if (num == 3) { df2 = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm"); } return df2.format(date1); } /** * 时间转换时间戳 */ public static String dateToStamp(String s) { String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = simpleDateFormat.parse(s); } catch (ParseException e) { e.printStackTrace(); } long ts = date.getTime(); res = String.valueOf(ts); return res; } /** * 时间戳转换时间 */ public static String stampToDate(String s){ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(s); Date date = new Date(lt); res = simpleDateFormat.format(date); return res; } }