Tools.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.ruoyi.biz.tools;
  2. import cn.hutool.json.JSONArray;
  3. import javafx.util.Pair;
  4. import java.math.BigDecimal;
  5. import java.math.RoundingMode;
  6. import java.time.*;
  7. import java.util.*;
  8. /**
  9. * Tool$
  10. *
  11. * @author wukai
  12. * @date 2024/5/12 02:18
  13. */
  14. public class Tools {
  15. public static void main(String[] args) {
  16. int p = 12;
  17. LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
  18. System.err.println(ldt.plusHours(2 * p));
  19. }
  20. /**
  21. * BigDecimal转float保留两位小数
  22. *
  23. * @param v BigDecimal
  24. * @return float
  25. */
  26. public static float scale2(BigDecimal v) {
  27. return v.setScale(2, RoundingMode.HALF_UP).floatValue();
  28. }
  29. /**
  30. * 计算稼动率
  31. * a/(a+b)*100 保留两位小数
  32. *
  33. * @param a 开机时间
  34. * @param b 关机时间
  35. * @return 稼动率
  36. */
  37. public static BigDecimal calcPercent(BigDecimal a, BigDecimal b) {
  38. if (a != null && a.intValue() != 0) {
  39. return a.multiply(BigDecimal.valueOf(100)).divide(a.add(b), 2, RoundingMode.HALF_UP);
  40. } else {
  41. return BigDecimal.ZERO;
  42. }
  43. }
  44. public static Map<String, Object> json2Map(JSONArray values, String table) {
  45. Map<String, Object> dataMap = new HashMap<>(16);
  46. for (int i = 0; i < values.size(); i++) {
  47. JSONArray d = values.getJSONArray(i);
  48. String key = d.getStr(0).replace(table + ".", "");
  49. switch (d.getStr(2)) {
  50. case "FLOAT":
  51. dataMap.put(key, d.getFloat(1));
  52. break;
  53. case "BOOLEAN":
  54. dataMap.put(key, d.getBool(1));
  55. break;
  56. case "INT32":
  57. case "INT64":
  58. dataMap.put(key, d.getInt(1));
  59. break;
  60. }
  61. }
  62. return dataMap;
  63. }
  64. /**
  65. * 当前的整点时间,0分0秒0毫秒
  66. *
  67. * @return LocalDateTime
  68. */
  69. public static LocalDateTime currWholeTime() {
  70. return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
  71. }
  72. /**
  73. * 按照给定日期,按2小时分段,组成where条件
  74. *
  75. * @param date yyyy-mm-dd
  76. * @return long[][]
  77. */
  78. public static List<String> getTimesWhere(String date) {
  79. List<String> resultList = new ArrayList<>();
  80. LocalDate localDate = LocalDate.parse(date);
  81. //获取今天0点
  82. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  83. LocalDateTime start = ldt;
  84. LocalDateTime end = start.plusHours(2);
  85. do {
  86. //-1秒 把上一轮最后一条数据取出来
  87. start = start.minusSeconds(1);
  88. Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  89. Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  90. System.err.println(start + "\t" + end);
  91. String where = String.format("time>%s and time <=%s", startTime, endTime);
  92. resultList.add(where);
  93. start = end;
  94. end = start.plusHours(2);
  95. } while (!ldt.plusDays(1).isBefore(end));
  96. return resultList;
  97. }
  98. /**
  99. * 获取指定日期的时间分段
  100. * map.put("period", period + "");
  101. * map.put("start", startTime);
  102. * map.put("end", endTime);
  103. *
  104. * @param date 字符串格式的 yyyy-mm-dd
  105. * @return list map
  106. */
  107. public static List<Map<String, Object>> timePeriod(String date) {
  108. LocalDate localDate = LocalDate.parse(date);
  109. return timePeriod(localDate);
  110. }
  111. /**
  112. * 获取指定日期的时间分段
  113. * 每小时一条
  114. *
  115. * @param date LocalDate yyyy-mm-dd
  116. * @return LocalDateTime map.get("start") map.get("end")
  117. */
  118. public static List<Map<String, Object>> timePeriod(LocalDate date) {
  119. List<Map<String, Object>> result = new ArrayList<>();
  120. //获取当天0点
  121. LocalDateTime ldt = LocalDateTime.of(date, LocalTime.MIN);
  122. LocalDateTime start = ldt;
  123. LocalDateTime end = start.plusHours(1);
  124. do {
  125. //-1秒 把上一轮最后一条数据取出来
  126. start = start.minusSeconds(1);
  127. Map<String, Object> map = new HashMap<>(16);
  128. map.put("start", start);
  129. map.put("end", end);
  130. result.add(map);
  131. start = end;
  132. end = start.plusHours(1);
  133. } while (!ldt.plusDays(1).isBefore(end));
  134. return result;
  135. }
  136. /**
  137. * 获取当日 直到当前时间的上一个偶数时间
  138. *
  139. * @return LocalDateTime map.get("start") map.get("end")
  140. */
  141. public static List<Map<String, Object>> timePeriod() {
  142. List<Map<String, Object>> result = new ArrayList<>();
  143. //获取当天0点
  144. LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
  145. LocalDateTime start = ldt.withHour(0);
  146. LocalDateTime end = start.plusHours(2);
  147. LocalDateTime stop = ldt.minusHours(ldt.getHour() % 2);
  148. do {
  149. //-1秒 把上一轮最后一条数据取出来
  150. start = start.minusSeconds(1);
  151. Map<String, Object> map = new HashMap<>(16);
  152. map.put("start", start);
  153. map.put("end", end);
  154. result.add(map);
  155. start = end;
  156. end = start.plusHours(2);
  157. } while (!end.isAfter(stop));
  158. return result;
  159. }
  160. /**
  161. * 获取统计时间 当天7天至第二天7点(不含)
  162. *
  163. * @param date 字符串类型的时间
  164. * @return 开始结束时间对
  165. */
  166. public static Pair<Date, Date> calcDay(String date) {
  167. LocalDate localDate = LocalDate.parse(date);
  168. return calcDay(localDate);
  169. }
  170. /**
  171. * 获取统计时间 当天7天至第二天7点(不含)
  172. *
  173. * @param localDate 日期
  174. * @return 开始结束时间对
  175. */
  176. public static Pair<Date, Date> calcDay(LocalDate localDate) {
  177. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  178. LocalDateTime start = ldt.plusHours(7);
  179. LocalDateTime end = start.plusHours(23);
  180. Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
  181. Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
  182. return new Pair<>(sTime, eTime);
  183. }
  184. /**
  185. * 获取A班开始结束时间
  186. *
  187. * @param localDate 日期
  188. * @return A班时间
  189. */
  190. public static Pair<Date, Date> teamA(LocalDate localDate) {
  191. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  192. LocalDateTime start = ldt.plusHours(7);
  193. LocalDateTime end = start.plusHours(11).minusSeconds(1);
  194. Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
  195. Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
  196. return new Pair<>(sTime, eTime);
  197. }
  198. /**
  199. * 获取B班开始结束时间
  200. *
  201. * @param localDate 日期
  202. * @return A班时间
  203. */
  204. public static Pair<Date, Date> teamB(LocalDate localDate) {
  205. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  206. LocalDateTime start = ldt.plusHours(19);
  207. LocalDateTime end = start.plusHours(11).minusSeconds(1);
  208. Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
  209. Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
  210. return new Pair<>(sTime, eTime);
  211. }
  212. }