Tools.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.ruoyi.biz.tools;
  2. import cn.hutool.json.JSONArray;
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.LocalTime;
  8. import java.time.ZoneOffset;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. /**
  14. * Tool$
  15. *
  16. * @author wukai
  17. * @date 2024/5/12 02:18
  18. */
  19. public class Tools {
  20. public static void main(String[] args) {
  21. int p = 12;
  22. LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
  23. System.err.println(ldt.plusHours(2 * p));
  24. }
  25. /**
  26. * 计算稼动率
  27. * a/(a+b)*100 保留两位小数
  28. *
  29. * @param a 开机时间
  30. * @param b 关机时间
  31. * @return 稼动率
  32. */
  33. public static BigDecimal calcPercent(BigDecimal a, BigDecimal b) {
  34. if (a != null && a.intValue() != 0) {
  35. return a.multiply(BigDecimal.valueOf(100)).divide(a.add(b), 2, RoundingMode.HALF_UP);
  36. } else {
  37. return BigDecimal.ZERO;
  38. }
  39. }
  40. public static Map<String, Object> json2Map(JSONArray values, String table) {
  41. Map<String, Object> dataMap = new HashMap<>(16);
  42. for (int i = 0; i < values.size(); i++) {
  43. JSONArray d = values.getJSONArray(i);
  44. String key = d.getStr(0).replace(table + ".", "");
  45. switch (d.getStr(2)) {
  46. case "FLOAT":
  47. dataMap.put(key, d.getFloat(1));
  48. break;
  49. case "BOOLEAN":
  50. dataMap.put(key, d.getBool(1));
  51. break;
  52. case "INT32":
  53. case "INT64":
  54. dataMap.put(key, d.getInt(1));
  55. break;
  56. }
  57. }
  58. return dataMap;
  59. }
  60. /**
  61. * 当前的整点时间,0分0秒0毫秒
  62. *
  63. * @return LocalDateTime
  64. */
  65. public static LocalDateTime currWholeTime() {
  66. return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
  67. }
  68. /**
  69. * 按照给定日期,按2小时分段,组成where条件
  70. *
  71. * @param date yyyy-mm-dd
  72. * @return long[][]
  73. */
  74. public static List<String> getTimesWhere(String date) {
  75. List<String> resultList = new ArrayList<>();
  76. LocalDate localDate = LocalDate.parse(date);
  77. //获取今天0点
  78. LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
  79. LocalDateTime start = ldt;
  80. LocalDateTime end = start.plusHours(2);
  81. do {
  82. //-1秒 把上一轮最后一条数据取出来
  83. start = start.minusSeconds(1);
  84. Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  85. Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  86. System.err.println(start + "\t" + end);
  87. String where = String.format("time>%s and time <=%s", startTime, endTime);
  88. resultList.add(where);
  89. start = end;
  90. end = start.plusHours(2);
  91. } while (!ldt.plusDays(1).isBefore(end));
  92. return resultList;
  93. }
  94. /**
  95. * 获取指定日期的时间分段
  96. * map.put("period", period + "");
  97. * map.put("start", startTime);
  98. * map.put("end", endTime);
  99. *
  100. * @param date 字符串格式的 yyyy-mm-dd
  101. * @return list map
  102. */
  103. public static List<Map<String, Object>> timePeriod(String date) {
  104. LocalDate localDate = LocalDate.parse(date);
  105. return timePeriod(localDate);
  106. }
  107. /**
  108. * 获取指定日期的时间分段
  109. *
  110. * @param date LocalDate yyyy-mm-dd
  111. * @return LocalDateTime map.get("start") map.get("end")
  112. */
  113. public static List<Map<String, Object>> timePeriod(LocalDate date) {
  114. List<Map<String, Object>> result = new ArrayList<>();
  115. //获取当天0点
  116. LocalDateTime ldt = LocalDateTime.of(date, LocalTime.MIN);
  117. LocalDateTime start = ldt;
  118. LocalDateTime end = start.plusHours(2);
  119. do {
  120. //-1秒 把上一轮最后一条数据取出来
  121. start = start.minusSeconds(1);
  122. Map<String, Object> map = new HashMap<>(16);
  123. map.put("start", start);
  124. map.put("end", end);
  125. result.add(map);
  126. start = end;
  127. end = start.plusHours(2);
  128. } while (!ldt.plusDays(1).isBefore(end));
  129. return result;
  130. }
  131. /**
  132. * 获取当日 直到当前时间的上一个偶数时间
  133. *
  134. * @return LocalDateTime map.get("start") map.get("end")
  135. */
  136. public static List<Map<String, Object>> timePeriod() {
  137. List<Map<String, Object>> result = new ArrayList<>();
  138. //获取当天0点
  139. LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
  140. LocalDateTime start = ldt.withHour(0);
  141. LocalDateTime end = start.plusHours(2);
  142. LocalDateTime stop = ldt.minusHours(ldt.getHour() % 2);
  143. do {
  144. //-1秒 把上一轮最后一条数据取出来
  145. start = start.minusSeconds(1);
  146. Map<String, Object> map = new HashMap<>(16);
  147. map.put("start", start);
  148. map.put("end", end);
  149. result.add(map);
  150. start = end;
  151. end = start.plusHours(2);
  152. } while (!end.isAfter(stop));
  153. return result;
  154. }
  155. }