package com.steerinfo.dil.util; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import java.util.regex.Pattern; /** * @ author :TXF * @ time :2021/8/25 11:25 */ public class DataChange { /** * 时间转换类 * 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位) * @param vueDate * @return */ public static Date dataToDate(Object vueDate){ if(vueDate instanceof Date){ return (Date) vueDate; } else { try { String str = String.valueOf(vueDate); if(judgeNumber(str) && str.length() == 13){ return new Date(Long.parseLong(str)); }else if(str.contains("-")){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(str); }else if(str.contains("/")){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); return sdf.parse(str); } }catch (Exception e){ System.out.println("时间解析错误!返回null"); return null; } } return null; } /** * 判断是否纯数字(不带小数点)仅供上面方法使用 * @param str * @return */ public static boolean judgeNumber(String str){ Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } /** * 数据转换成BigDecimal * @param data * @return */ public static BigDecimal dataToBigDecimal(Object data){ if (data != null){ if(data instanceof BigDecimal){ return (BigDecimal) data; }else{ String str = String.valueOf(data); BigDecimal decimal = null; if(!"".equals(str)){ try { decimal = new BigDecimal(str); } catch (Exception e) { System.out.println(data + ":数据解析失败!返回0"); return new BigDecimal(0); } } return decimal; } } return new BigDecimal(0); } /** * 将时间截取到天 为字符串类型 用于前端只显示到天 * @param date 传入时间 * @return */ public static String dateToDayDate(Object date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date changeDate = null; if(date == null) return null; try{ changeDate = (Date) date; }catch (Exception e){ e.printStackTrace(); } return sdf.format(changeDate); } /** * 遍历从数据库中传进来的数据 * @param list 从数据库查询出来的list数据列表 * @param key map中多个需要转换的date参数 */ public static void changeDateToDayDate(List> list, String ...key){ //遍历List for (Map map : list) { for (String s : key) { //从map中取 date的值 并转换成字符串类型的日期 String stringDate = dateToDayDate(map.get(s)); if(stringDate.length() == 0){ break; }else { //修改map中的值 map.put(s, stringDate); } } } } /** * 遍历列表使只显示两位小数 * @param list * @param key */ public static void dataTo2Number(List> list, String ...key){ DecimalFormat df = new DecimalFormat("0.00"); //遍历List for (Map map : list) { for (String s : key) { //修改数据为带两位小数 try { BigDecimal oldDate = dataToBigDecimal(map.get(s)); String resultDeduction = df.format(oldDate.doubleValue()); map.put(s, resultDeduction); } catch (Exception e) { System.out.println("原料扣减量数据有误"); } } } } /** * 计算相差时间 日时分秒 * @param * @return */ public static String calculatedTimeDifference(Date time1, Date time2){ long t1 = time1.getTime(); long t2 = time2.getTime(); if(t1 > t2){ long temp = t1; t1 = t2; t2 = temp; } long between = t2 - t1; long day = between / (24 * 60 * 60 * 1000); long hour = (between / (60 * 60 * 1000) - day * 24); long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); return day + "天"+ + hour+ "时" + min + "分" + s + "秒"; } /** * 生成带时间的八位数顺序号 * @param start 前缀 * @param id 顺序号 主键Id * @return */ public static String generateEightDigitsNumber(String start, Integer id){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); StringBuilder sb = new StringBuilder(start + sdf.format(new Date())); sb.append( id < 10 ? "0000000" + id : id < 100 ? "000000" + id : id < 1000 ? "00000" + id : id < 10000 ? "0000" + id : id < 100000 ? "000" + id : id < 1000000 ? "00" + id : id < 10000000 ? "0" + id : id.toString() ); return sb.toString(); } /** * 根据时间段查询数据 支持只选择单个时间 * @Author TXF * @Date 2022/1/10 23:21 * @param startTime * @param endTime * @param map * @param sdf * @return **/ public static void queryDataByDate(String startTime, String endTime, Map map, SimpleDateFormat sdf){ if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) { map.put("startDate", sdf.format(new Date(Long.parseLong(startTime)))); map.put("endDate", sdf.format(new Date(Long.parseLong(endTime) + 86400000))); } else if (startTime != null && !"null".equals(startTime)) { map.put("oneDate", sdf.format(new Date(Long.parseLong(startTime)))); } else if (endTime != null && !"null".equals(endTime)) { map.put("oneDate", sdf.format(new Date(Long.parseLong(endTime)))); } else { map.put("oneDate", sdf.format(new Date())); } } /** * 只支持两个时间查询 * @Author TXF * @Date 2022/1/15 9:08 * @param startTime * @param endTime * @param sdf * @return **/ public static void queryDataByDateTime(String startTime, String endTime, Map map,SimpleDateFormat sdf){ SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) { map.put("startDate", sdf.format(new Date(Long.parseLong(startTime)))); map.put("endDate", sdf.format(new Date(Long.parseLong(endTime)))); } //如果开始时间和结束时间有且只有一个为空 则只查那天的数据 else if((startTime != null && !"null".equals(startTime)) || (endTime != null && !"null".equals(endTime))){ if(startTime != null && !"null".equals(startTime)){ queryDataByTwoDateSon(map, startTime, sdfDate); } if(endTime != null && !"null".equals(endTime)){ queryDataByTwoDateSon(map, endTime, sdfDate); } }else { //如果两者时间都为空,则查询当天数据 String nowDate = sdfDate.format(new Date()); map.put("oneDate", nowDate + " 00:00:00"); } } /** * 上面方法的儿子方法 如果只传入了一个时间 则查询那天的数据 * @Author TXF * @Date 2022/1/17 16:17 * @param map * @param time * @param sdfDate * @return **/ private static void queryDataByTwoDateSon(Map map, String time, SimpleDateFormat sdfDate){ Date date1 = new Date(Long.parseLong(time)); Date date2 = new Date(Long.parseLong(time) + 86400000); String dayStartTime = sdfDate.format(date1); String dayEndTime = sdfDate.format(date2); map.put("startDate", dayStartTime + " 00:00:00"); map.put("endDate", dayEndTime + " 00:00:00"); } }