package com.ruoyi.biz.tools; import cn.hutool.json.JSONArray; import javafx.util.Pair; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.*; import java.util.*; /** * Tool$ * * @author wukai * @date 2024/5/12 02:18 */ public class Tools { public static void main(String[] args) { int p = 12; LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); System.err.println(ldt.plusHours(2 * p)); } /** * BigDecimal转float保留两位小数 * * @param v BigDecimal * @return float */ public static float scale2(BigDecimal v) { return v.setScale(2, RoundingMode.HALF_UP).floatValue(); } /** * 计算稼动率 * a/(a+b)*100 保留两位小数 * * @param a 开机时间 * @param b 关机时间 * @return 稼动率 */ public static BigDecimal calcPercent(BigDecimal a, BigDecimal b) { if (a != null && a.intValue() != 0) { return a.multiply(BigDecimal.valueOf(100)).divide(a.add(b), 2, RoundingMode.HALF_UP); } else { return BigDecimal.ZERO; } } public static Map json2Map(JSONArray values, String table) { Map dataMap = new HashMap<>(16); for (int i = 0; i < values.size(); i++) { JSONArray d = values.getJSONArray(i); String key = d.getStr(0).replace(table + ".", ""); switch (d.getStr(2)) { case "FLOAT": dataMap.put(key, d.getFloat(1)); break; case "BOOLEAN": dataMap.put(key, d.getBool(1)); break; case "INT32": case "INT64": dataMap.put(key, d.getInt(1)); break; } } return dataMap; } /** * 当前的整点时间,0分0秒0毫秒 * * @return LocalDateTime */ public static LocalDateTime currWholeTime() { return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); } /** * 按照给定日期,按2小时分段,组成where条件 * * @param date yyyy-mm-dd * @return long[][] */ public static List getTimesWhere(String date) { List resultList = new ArrayList<>(); LocalDate localDate = LocalDate.parse(date); //获取今天0点 LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN); LocalDateTime start = ldt; LocalDateTime end = start.plusHours(2); do { //-1秒 把上一轮最后一条数据取出来 start = start.minusSeconds(1); Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli(); Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli(); System.err.println(start + "\t" + end); String where = String.format("time>%s and time <=%s", startTime, endTime); resultList.add(where); start = end; end = start.plusHours(2); } while (!ldt.plusDays(1).isBefore(end)); return resultList; } /** * 获取指定日期的时间分段 * map.put("period", period + ""); * map.put("start", startTime); * map.put("end", endTime); * * @param date 字符串格式的 yyyy-mm-dd * @return list map */ public static List> timePeriod(String date) { LocalDate localDate = LocalDate.parse(date); return timePeriod(localDate); } /** * 获取指定日期的时间分段 * 每小时一条 * * @param date LocalDate yyyy-mm-dd * @return LocalDateTime map.get("start") map.get("end") */ public static List> timePeriod(LocalDate date) { List> result = new ArrayList<>(); //获取当天0点 LocalDateTime ldt = LocalDateTime.of(date, LocalTime.MIN); LocalDateTime start = ldt; LocalDateTime end = start.plusHours(1); do { //-1秒 把上一轮最后一条数据取出来 start = start.minusSeconds(1); Map map = new HashMap<>(16); map.put("start", start); map.put("end", end); result.add(map); start = end; end = start.plusHours(1); } while (!ldt.plusDays(1).isBefore(end)); return result; } /** * 获取当日 直到当前时间的上一个偶数时间 * * @return LocalDateTime map.get("start") map.get("end") */ public static List> timePeriod() { List> result = new ArrayList<>(); //获取当天0点 LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); LocalDateTime start = ldt.withHour(0); LocalDateTime end = start.plusHours(2); LocalDateTime stop = ldt.minusHours(ldt.getHour() % 2); do { //-1秒 把上一轮最后一条数据取出来 start = start.minusSeconds(1); Map map = new HashMap<>(16); map.put("start", start); map.put("end", end); result.add(map); start = end; end = start.plusHours(2); } while (!end.isAfter(stop)); return result; } /** * 获取统计时间 当天7天至第二天7点(不含) * * @param date 字符串类型的时间 * @return 开始结束时间对 */ public static Pair calcDay(String date) { LocalDate localDate = LocalDate.parse(date); return calcDay(localDate); } /** * 获取统计时间 当天7天至第二天7点(不含) * * @param localDate 日期 * @return 开始结束时间对 */ public static Pair calcDay(LocalDate localDate) { LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN); LocalDateTime start = ldt.plusHours(7); LocalDateTime end = start.plusHours(23); Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); return new Pair<>(sTime, eTime); } /** * 获取A班开始结束时间 * * @param localDate 日期 * @return A班时间 */ public static Pair teamA(LocalDate localDate) { LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN); LocalDateTime start = ldt.plusHours(7); LocalDateTime end = start.plusHours(11).minusSeconds(1); Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); return new Pair<>(sTime, eTime); } /** * 获取B班开始结束时间 * * @param localDate 日期 * @return A班时间 */ public static Pair teamB(LocalDate localDate) { LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN); LocalDateTime start = ldt.plusHours(19); LocalDateTime end = start.plusHours(11).minusSeconds(1); Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant()); Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant()); return new Pair<>(sTime, eTime); } }