123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- 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<String, Object> json2Map(JSONArray values, String table) {
- Map<String, Object> 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<String> getTimesWhere(String date) {
- List<String> 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<Map<String, Object>> 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<Map<String, Object>> timePeriod(LocalDate date) {
- List<Map<String, Object>> 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<String, Object> 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<Map<String, Object>> timePeriod() {
- List<Map<String, Object>> 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<String, Object> 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<Date, Date> calcDay(String date) {
- LocalDate localDate = LocalDate.parse(date);
- return calcDay(localDate);
- }
- /**
- * 获取统计时间 当天7天至第二天7点(不含)
- *
- * @param localDate 日期
- * @return 开始结束时间对
- */
- public static Pair<Date, Date> 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<Date, Date> 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<Date, Date> 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);
- }
- }
|