CommonUitl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. package com.jeesite.modules.mes.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.jeesite.common.config.Global;
  4. import com.jeesite.common.lang.StringUtils;
  5. import com.jeesite.modules.mes.req.MesSearchDateReq;
  6. import com.jeesite.modules.mes.service.MesProductModelService;
  7. import okhttp3.*;
  8. import org.apache.tomcat.util.codec.binary.Base64;
  9. import org.beetl.core.util.ArrayUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import java.io.*;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.text.DateFormat;
  16. import java.text.DecimalFormat;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.time.LocalDate;
  20. import java.time.Year;
  21. import java.util.*;
  22. import java.util.concurrent.TimeUnit;
  23. public class CommonUitl {
  24. /**
  25. * 时长格式化
  26. * @param interval
  27. * @return
  28. */
  29. public static String formatDuration(Integer interval){
  30. if(interval <= 0){
  31. return "00:00:00";
  32. }
  33. int hours = interval / 3600;
  34. int minutes = (interval % 3600) / 60;
  35. int remainingSeconds = interval % 60;
  36. String str = "";
  37. if(hours >= 10){
  38. str += hours+":";
  39. }else{
  40. str += "0"+hours+":";
  41. }
  42. if(minutes >= 10){
  43. str += minutes+":";
  44. }else{
  45. str += "0"+minutes+":";
  46. }
  47. if(remainingSeconds >= 10){
  48. str += remainingSeconds;
  49. }else{
  50. str += "0"+remainingSeconds;
  51. }
  52. return str;
  53. }
  54. /**
  55. * 格式化工位号
  56. * @param oprno
  57. * @return
  58. */
  59. public static String formatOprno(String oprno){
  60. List<String> lists = new ArrayList<>();
  61. // lists.add("OP180"); // 只有OP180和OP240带ABC
  62. // lists.add("OP240");
  63. if(oprno.length() == 6){
  64. String ysoprno = oprno.substring(0,5).trim();
  65. if(!lists.contains(ysoprno)){
  66. oprno = ysoprno;
  67. }
  68. }
  69. return oprno;
  70. }
  71. /**
  72. * 格式化工位只获取其中数字
  73. * @param oprno
  74. * @return
  75. */
  76. public static String getOprnoNumber(String oprno){
  77. List<String> lists = new ArrayList<>();
  78. String ysoprno = oprno.substring(2,5).trim();
  79. if(!lists.contains(ysoprno)){
  80. oprno = ysoprno;
  81. }
  82. return oprno;
  83. }
  84. /**
  85. * 获取几天前的时间
  86. * @param days
  87. * @return
  88. */
  89. public static Date getPreviousDate(int days) {
  90. Calendar calendar = Calendar.getInstance();
  91. calendar.add(Calendar.DAY_OF_MONTH, -days);
  92. return calendar.getTime();
  93. }
  94. /**
  95. * 获取产品类型 151245P00000106200100023022500001
  96. * @param sn
  97. * @return
  98. */
  99. public static String getProductCateBySn(String sn){
  100. if(sn.length() < 19){
  101. return "";
  102. }
  103. return sn.substring(6,19);
  104. }
  105. /**
  106. * 根据序号获取工艺号
  107. * @param first 首位
  108. * @param number 序号
  109. * @return
  110. */
  111. public static String getCraftByNumber(String first,Integer number){
  112. return first+StringUtils.leftPad(String.valueOf(number),5,"0");
  113. }
  114. public static JSONObject doPost(String httpUrl, String param) {
  115. // System.out.println("httpUrl"+httpUrl);
  116. // System.out.println("param"+param);
  117. HttpURLConnection connection = null;
  118. InputStream is = null;
  119. OutputStream os = null;
  120. BufferedReader br = null;
  121. String result = null;
  122. try {
  123. URL url = new URL(httpUrl);
  124. connection = (HttpURLConnection) url.openConnection();
  125. connection.setRequestMethod("POST");
  126. connection.setConnectTimeout(15000);
  127. connection.setReadTimeout(15000);
  128. connection.setDoOutput(true);
  129. connection.setDoInput(true);
  130. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  131. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  132. os = connection.getOutputStream();
  133. if(!param.isEmpty()){
  134. os.write(param.getBytes());
  135. }
  136. if (connection.getResponseCode() == 200) {
  137. is = connection.getInputStream();
  138. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  139. StringBuffer sbf = new StringBuffer();
  140. String temp = null;
  141. while ((temp = br.readLine()) != null) {
  142. sbf.append(temp);
  143. sbf.append("\r\n");
  144. }
  145. result = sbf.toString();
  146. }
  147. } catch (MalformedURLException e) {
  148. // e.printStackTrace();
  149. } catch (IOException e) {
  150. // e.printStackTrace();
  151. } finally {
  152. if (null != br) {
  153. try {
  154. br.close();
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. }
  158. }
  159. if (null != os) {
  160. try {
  161. os.close();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. if (null != is) {
  167. try {
  168. is.close();
  169. } catch (IOException e) {
  170. e.printStackTrace();
  171. }
  172. }
  173. connection.disconnect();
  174. }
  175. System.out.println("result="+result);
  176. if(result.equalsIgnoreCase("false") || result.isEmpty()) {
  177. return null;
  178. }else {
  179. return JSONObject.parseObject(result);
  180. }
  181. }
  182. public static String Date2TimeStamp(Date date) {
  183. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  184. String format = sdf.format(date);
  185. try {
  186. String valueOf = String.valueOf(sdf.parse(format).getTime() / 1000);
  187. return valueOf;
  188. } catch (ParseException e) {
  189. e.printStackTrace();
  190. }
  191. return "";
  192. }
  193. public static String getToday() {
  194. try {
  195. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  196. Date date = new Date();
  197. String today = sdf.format(date);
  198. return today;
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. return "";
  203. }
  204. public static String TimeStamp2Date(String timestampString) {
  205. // String timestampString = "1556169710";
  206. String formats = "yyyy-MM-dd HH:mm:ss";
  207. Long timestamp = Long.parseLong(timestampString) * 1000;
  208. //日期格式字符串
  209. String dateStr = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
  210. return dateStr;
  211. }
  212. public static Date String2Date(String str){
  213. try{
  214. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
  215. Date date = sdf.parse(str); // 进行转换
  216. return date;
  217. }catch (Exception e){
  218. return null;
  219. }
  220. }
  221. public static Date String2DateTime(String str){
  222. try{
  223. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义日期格式
  224. Date date = sdf.parse(str); // 进行转换
  225. return date;
  226. }catch (Exception e){
  227. return null;
  228. }
  229. }
  230. public static Date String2DateTime2(String str){
  231. try{
  232. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); // 定义日期格式
  233. Date date = sdf.parse(str); // 进行转换
  234. return date;
  235. }catch (Exception e){
  236. return null;
  237. }
  238. }
  239. /**
  240. * 检验工件码的正确性
  241. * @param sn 工件码
  242. * @param cateSn 产品类型编码
  243. * @return
  244. */
  245. public static Boolean checkProductSn(String sn,String cateSn){
  246. switch (cateSn){
  247. case "ZHXT":
  248. if(sn.length() != 32 && sn.length() != 24){
  249. return false;
  250. }
  251. // 000020015305-0100101425011400038 0000+10位料号+7位供应商代码+6位生产日期+5位序列号
  252. // 2520200FF31006368EE00050 印尼版 24位 10位零件号+6位供应商代码+7位批次码+1位返工码
  253. List<String> xwds = new ArrayList<>();
  254. String xwd = ""; // 物料号
  255. String gys = ""; // 供应商代码
  256. if(sn.length() == 24){
  257. xwds.add("2520200FF3"); //印尼版
  258. // 物料号
  259. xwd = sn.substring(0,10);
  260. // 供应商代码
  261. gys = "100636";
  262. if(!sn.substring(10,16).equals(gys)){
  263. return false;
  264. }
  265. }else{
  266. xwds.add("000020015308-0"); // 国内长续航
  267. xwds.add("000020015305-0"); // 国内超长续航
  268. xwds.add("000020018302-0"); // 海外长续航
  269. xwds.add("000020018301-0"); // 海外超长续航
  270. xwds.add("000020017939-0"); // 中亚长续航
  271. xwds.add("000020017933-0"); // 中亚超长续航
  272. // 物料号
  273. xwd = sn.substring(0,14);
  274. // 供应商代码
  275. gys = "1001014";
  276. if(!sn.substring(14,21).equals(gys)){
  277. return false;
  278. }
  279. }
  280. if(!xwds.contains(xwd)){
  281. return false;
  282. }
  283. return true;
  284. }
  285. return false;
  286. }
  287. /**
  288. * 检验后面板工件码的正确性
  289. * @param sn 工件码
  290. * @return
  291. */
  292. public static Boolean checkProductSnHmb(String sn){
  293. if(sn == null){
  294. return false;
  295. }
  296. // 新规则 24 位(EVE 编码):F956210325195AI24A010001
  297. if(sn.length() == 24){
  298. return checkProductSnHmbEve(sn);
  299. }
  300. // 老规则 28 位已隐藏,仅保留 24 位新码校验
  301. /*
  302. // 老规则 28 位:0050010400435E6348245C120001
  303. if(sn.length() != 28){
  304. return false;
  305. }
  306. List<String> xwds = new ArrayList<>();
  307. xwds.add("0050010400435");
  308. String xwd = sn.substring(0,13);
  309. if(!xwds.contains(xwd)){
  310. return false;
  311. }
  312. // 供应商代码
  313. String gys = "34824";
  314. if(!sn.substring(15,20).equals(gys)){
  315. return false;
  316. }
  317. return true;
  318. */
  319. return false;
  320. }
  321. // 后梁码 EVE 编码规则(24位):
  322. // 供应商5位(F9562) + 物料代码8位(数字) + 日期4位(年A-Z、月A-L、日01-31) + 批次3位(产线字母+2位数字) + 流水4位(数字)
  323. private static final java.util.regex.Pattern HMB_EVE_PATTERN =
  324. java.util.regex.Pattern.compile("^F9562\\d{8}[A-Z][A-L](0[1-9]|[12]\\d|3[01])[A-Z]\\d{2}\\d{4}$");
  325. public static Boolean checkProductSnHmbEve(String sn){
  326. return sn != null && HMB_EVE_PATTERN.matcher(sn).matches();
  327. }
  328. /**
  329. * 检验前工序工件码的正确性
  330. * @param sn 工件码
  331. * @return
  332. */
  333. public static Boolean checkProductSnQgx(String sn){
  334. // 500104004355C230001
  335. if(sn.length() != 19){
  336. return false;
  337. }
  338. List<String> xwds = new ArrayList<>();
  339. xwds.add("50010400435");
  340. String xwd = sn.substring(0,11);
  341. if(!xwds.contains(xwd)){
  342. return false;
  343. }
  344. return true;
  345. }
  346. public static int getYearLastChar(){
  347. // 获取当前年份
  348. int currentYear = Year.now().getValue();
  349. // 转换为字符串,然后获取最后一个字符
  350. String lastDigit = String.valueOf(currentYear).substring(String.valueOf(currentYear).length() - 1);
  351. // 将字符串的最后一个数字转换回整数
  352. int lastDigitAsInt = Integer.parseInt(lastDigit);
  353. return lastDigitAsInt;
  354. }
  355. public static String getMonthChar(){
  356. // 获取当前日期
  357. LocalDate today = LocalDate.now();
  358. // 获取当前月份,LocalDate的getMonthValue方法直接返回月份数字,无需加1
  359. int month = today.getMonthValue();
  360. if(month == 10){
  361. return "A";
  362. }else if(month == 11){
  363. return "B";
  364. }else if(month == 12){
  365. return "C";
  366. }else{
  367. return ""+month;
  368. }
  369. }
  370. public static String getDayChar(){
  371. // 获取当前日期
  372. LocalDate today = LocalDate.now();
  373. // 获取今天是该月的第几天
  374. int dayOfMonth = today.getDayOfMonth();
  375. if(dayOfMonth >= 10){
  376. return dayOfMonth + "";
  377. }else{
  378. return "0"+dayOfMonth;
  379. }
  380. }
  381. // 检查底护板批次码是否正确
  382. public static boolean checkBatchSn(String batchSn,String sn) {
  383. if(batchSn.length() < 20 || sn.length() < 20){
  384. return false;
  385. }
  386. if(sn.length() != 24 && sn.length() != 32){
  387. return false;
  388. }
  389. String xwd = "";
  390. if(sn.length() == 24){
  391. xwd = sn.substring(0,10);
  392. }else{
  393. xwd = sn.substring(0,14);
  394. }
  395. String bts = batchSn.substring(0,14);
  396. switch (xwd){
  397. case "000020015308-0":// 国内长续航
  398. case "000020015305-0":// 国内超长续航
  399. case "000020017939-0":// 中亚长续航
  400. case "000020017933-0":// 中亚超长续航
  401. if(bts.equals("000010056457-0")){ //000010056457-0100101424122630087
  402. return true;
  403. }else{
  404. return false;
  405. }
  406. case "000020018302-0":// 海外长续航
  407. case "000020018301-0":// 海外超长续航
  408. case "2520200FF3":
  409. if(bts.equals("000010061509-0")){
  410. return true;
  411. }else{
  412. return false;
  413. }
  414. }
  415. return false;
  416. }
  417. public static String getBase64Decode(String str) {
  418. if (str == null || "".equals(str)) {
  419. return "";
  420. }
  421. try {
  422. byte[] bt2 = Base64.decodeBase64(str.getBytes("UTF-8"));
  423. str = new String(bt2,"UTF-8");
  424. } catch (UnsupportedEncodingException e) {
  425. e.printStackTrace();
  426. str = "";
  427. }
  428. return str;
  429. }
  430. public static double secondsToHours(double seconds){
  431. double hours = seconds / 3600;
  432. return hours;
  433. }
  434. public static String[] getTimeInterval(String strStartTime, String strStopTime) {
  435. String arrStr[] = new String[2];
  436. try {
  437. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  438. Date startTime = df.parse(strStartTime);
  439. Date stopTime = df.parse(strStopTime);
  440. DecimalFormat decimalFormat = new DecimalFormat("00");
  441. long diff = stopTime.getTime() - startTime.getTime();//得到的差值
  442. //logger.debug("------ " + diff);
  443. long hours = diff / (1000 * 60 * 60); //获取时
  444. long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60); //获取分钟
  445. long s = (diff / 1000 - hours * 60 * 60 - minutes * 60);//获取秒
  446. String countTime = "" + decimalFormat.format(hours) + ":" + decimalFormat.format(minutes) + ":" + decimalFormat.format(s);
  447. long second = (diff / 1000);//获取 共有多少秒
  448. String strTimeLenS = second + "";
  449. arrStr[0] = countTime;//00:12:50 时间格式
  450. arrStr[1] = strTimeLenS;//140 总秒数
  451. return arrStr;
  452. } catch (Exception ex) {
  453. // logger.error("getTimeInterval() 获取时间间隔 " + strStartTime + " "+strStopTime + ex.toString());
  454. arrStr[0] = "-1";//00:12:50 时间格式
  455. arrStr[1] = "-1";//140 总秒数
  456. return arrStr;
  457. }
  458. }
  459. public static long convertDateStringToTimestamp(String dateString, String format){
  460. try{
  461. SimpleDateFormat sdf = new SimpleDateFormat(format);
  462. Date date = sdf.parse(dateString);
  463. return date.getTime();
  464. }catch (Exception e){
  465. return 0;
  466. }
  467. }
  468. // 按产线读取允许跳过的工位配置(mes.pass.oprno.{产线小写}),未配置返回空串即不跳站
  469. public static String getPassOprnos(String lineSn){
  470. if(StringUtils.isEmpty(lineSn)){
  471. return "";
  472. }
  473. String v = Global.getConfig("mes.pass.oprno." + lineSn.trim().toLowerCase());
  474. return v == null ? "" : v;
  475. }
  476. // 逗号分隔精确匹配,避免 contains 子串误匹配(如 OP12 命中 OP120)
  477. public static boolean matchOprnos(String oprnos, String oprno){
  478. if(StringUtils.isEmpty(oprnos) || StringUtils.isEmpty(oprno)){
  479. return false;
  480. }
  481. for(String s : oprnos.split(",")){
  482. if(s.trim().equals(oprno)){
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. // 判断是否是单部件(配置 mes.dbj.oprno.{产线小写},XT 缺省回退 OP120)
  489. public static Boolean checkDbjOprno(String oprno,String lineSn){
  490. if(StringUtils.isEmpty(lineSn)){
  491. return false;
  492. }
  493. String cfg = Global.getConfig("mes.dbj.oprno." + lineSn.trim().toLowerCase());
  494. if(StringUtils.isEmpty(cfg) && "XT".equals(lineSn)){
  495. cfg = "OP120";
  496. }
  497. return matchOprnos(cfg, oprno);
  498. }
  499. // 按产线读取下线工位配置(mes.offline.oprno.{产线小写}),未配置返回空串;XT 未配置时回退 OP280
  500. public static String getOfflineOprno(String lineSn){
  501. if(StringUtils.isEmpty(lineSn)){
  502. return "";
  503. }
  504. String v = Global.getConfig("mes.offline.oprno." + lineSn.trim().toLowerCase());
  505. if(StringUtils.isEmpty(v) && "XT".equals(lineSn.trim())){
  506. return "OP280";
  507. }
  508. return v == null ? "" : v.trim();
  509. }
  510. // 判断某工位是否为该产线的下线工位(工位码归一后精确匹配)
  511. public static boolean isOfflineOprno(String oprno, String lineSn){
  512. String off = getOfflineOprno(lineSn);
  513. if(StringUtils.isEmpty(off) || StringUtils.isEmpty(oprno)){
  514. return false;
  515. }
  516. return formatOprno(oprno).equals(off);
  517. }
  518. /**
  519. * 表单形式post请求
  520. *
  521. * @paramurl 请求地址
  522. * @parammap post请求参数
  523. * @return请求结果
  524. */
  525. public static JSONObject doPostWithForm(Map<String, String> map, String url) {
  526. try{
  527. OkHttpClient client = new OkHttpClient().newBuilder()
  528. .connectTimeout(30, TimeUnit.SECONDS)
  529. .readTimeout(30, TimeUnit.SECONDS)
  530. .writeTimeout(30, TimeUnit.SECONDS)
  531. .build();
  532. //构建一个formBody builder
  533. FormBody.Builder builder = new FormBody.Builder();
  534. //循环form表单,将表单内容添加到form builder中
  535. for (Map.Entry<String, String> entry : map.entrySet()) {
  536. String key = entry.getKey();
  537. String value = entry.getValue();
  538. builder.add(key, value);
  539. }
  540. //构建formBody,将其传入Request请求中
  541. FormBody body = builder.build();
  542. Request request = new Request.Builder().url(url).post(body).build();
  543. Call call = client.newCall(request);
  544. //返回请求结果
  545. try (Response response = call.execute()) {
  546. String result = response.body().string();
  547. if(result.equalsIgnoreCase("false") || result.isEmpty()) {
  548. return null;
  549. }else {
  550. return JSONObject.parseObject(result);
  551. }
  552. } catch (IOException e) {
  553. e.printStackTrace();
  554. return null;
  555. }
  556. }catch (Exception e){
  557. e.printStackTrace();
  558. return null;
  559. }
  560. }
  561. /**
  562. * 判段工件码是否符合要求
  563. * @param sn 工件码
  564. * @param spec 规格
  565. * @param specStart 规格开始位置
  566. * @param length 码长度
  567. * @return
  568. */
  569. public static Boolean checkSnByModel(String sn, String spec,Integer specStart, Integer length) {
  570. if(sn.length()!=length){
  571. return false;
  572. }
  573. Integer start=specStart - 1;
  574. Integer end=start + spec.length();
  575. if(length > end){
  576. String newSpec = sn.substring(start,end);
  577. if(newSpec.equals(spec)){
  578. return true;
  579. }
  580. }
  581. return false;
  582. }
  583. //驭视科技CCD接口返回xml模板
  584. public static String getXml(String content){
  585. String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  586. xml += "<string xmlns=\"http://mesateapi.com/\">"+content+"</string>";
  587. return xml;
  588. }
  589. public static MesSearchDateReq getSearchDate(){
  590. String days = Global.getConfig("mes.search.days");
  591. if(StringUtils.isEmpty(days)){
  592. days = "7";
  593. }
  594. int day = Integer.parseInt(days);
  595. if(day < 1){ // 小于1使用默认值
  596. day = 7;
  597. }
  598. Date curDate = new Date();
  599. // 获取当前日期的Calendar实例
  600. Calendar calendar = Calendar.getInstance();
  601. // 将当前日期的Calendar回退day天
  602. calendar.add(Calendar.DAY_OF_MONTH, -day);
  603. // 获取3天前的Date
  604. Date sevenDaysAgo = calendar.getTime();
  605. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  606. String sevenDaysAgoStr = sdf.format(sevenDaysAgo)+" 00:00";
  607. String curDateStr = sdf.format(curDate)+" 23:59";
  608. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  609. try {
  610. sevenDaysAgo = dateFormat.parse(sevenDaysAgoStr);
  611. curDate = dateFormat.parse(curDateStr);
  612. } catch (ParseException e) {
  613. e.printStackTrace();
  614. }
  615. MesSearchDateReq map = new MesSearchDateReq();
  616. map.setStartDate(sevenDaysAgo);
  617. map.setEndtDate(curDate);
  618. return map;
  619. }
  620. public static int timeDifference(String dateStr1, String dateStr2){
  621. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  622. try {
  623. Date date1 = sdf.parse(dateStr1);
  624. Date date2 = sdf.parse(dateStr2);
  625. long diff = date2.getTime() - date1.getTime(); // 计算差异(毫秒)
  626. long diffMinutes = diff / (60 * 1000); // 转换为分钟
  627. // long diffHours = diff / (60 * 60 * 1000); // 转换为小时
  628. return Integer.parseInt(String.valueOf(diffMinutes));
  629. } catch (ParseException e) {
  630. e.printStackTrace();
  631. }
  632. return 0;
  633. }
  634. public static List<String> getBeforeDays(Integer maxday) {
  635. // 获取当前日期
  636. Date currentDate = new Date();
  637. // 创建Calendar实例
  638. Calendar calendar = Calendar.getInstance();
  639. calendar.setTime(currentDate);
  640. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  641. //当天月日
  642. List<String> list = new ArrayList<>();
  643. list.add(sdf.format(new Date()));
  644. // 遍历并输出前6天的日期
  645. for (int i = 0; i < (maxday - 1); i++) {
  646. calendar.add(Calendar.DAY_OF_MONTH, -1);
  647. Date previousDate = calendar.getTime();
  648. list.add(sdf.format(previousDate));
  649. }
  650. return list;
  651. }
  652. }