CommonUtils.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.mes.util;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.concurrent.TimeUnit;
  6. public class CommonUtils {
  7. public static String getDateByBefor(int ngTime) {
  8. try{
  9. Calendar beforeTime = Calendar.getInstance();
  10. beforeTime.add(Calendar.MINUTE, -ngTime);// 5分钟之前的时间
  11. Date beforeD = beforeTime.getTime();
  12. String before5 = new SimpleDateFormat("yyyyMMddHHmmss").format(beforeD);
  13. return before5;
  14. }catch (Exception e){
  15. return null;
  16. }
  17. }
  18. public static long jsSecond(String timeStr1,String timeStr2){
  19. // String timeStr1 = "2023-01-01 12:10:00";
  20. // String timeStr2 = "2023-01-01 12:05:00";
  21. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  22. try {
  23. Date date1 = sdf.parse(timeStr1);
  24. Date date2 = sdf.parse(timeStr2);
  25. long diffInMillies = Math.abs(date2.getTime() - date1.getTime());
  26. long seconds = TimeUnit.MILLISECONDS.toSeconds(diffInMillies);
  27. return seconds;
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. return 0;
  31. }
  32. }
  33. /**
  34. * 判段工件码是否符合要求
  35. * @param sn 工件码
  36. * @param spec 规格
  37. * @param specStart 规格开始位置
  38. * @param length 码长度
  39. * @return
  40. */
  41. public static Boolean checkSnByModel(String sn, String spec,Integer specStart, Integer length) {
  42. if(sn.length()!=length){
  43. return false;
  44. }
  45. Integer start=specStart - 1;
  46. Integer end=start + spec.length();
  47. if(length > end){
  48. String newSpec = sn.substring(start,end);
  49. if(newSpec.equals(spec)){
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. }