SysUtils.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package com.tsi.app.common.utils;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.GregorianCalendar;
  5. public class SysUtils {
  6. public static int toInt(String paramVal, int defVal) {
  7. int result = defVal;
  8. try {
  9. result = Integer.parseInt(paramVal);
  10. } catch (Exception var4) {
  11. }
  12. return result;
  13. }
  14. public static float toFloat(String paramVal, float defVal) {
  15. float result = defVal;
  16. try {
  17. result = Float.parseFloat(paramVal);
  18. } catch (Exception var4) {
  19. }
  20. return result;
  21. }
  22. public static double toDouble(String paramVal, double defVal) {
  23. double result = defVal;
  24. try {
  25. result = Double.parseDouble(paramVal);
  26. } catch (Exception var6) {
  27. }
  28. return result;
  29. }
  30. public static String getSysTime() {
  31. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  32. Date dtLog = new Date();
  33. return sdfDate.format(dtLog);
  34. }
  35. public static String getSysTimeStr() {
  36. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  37. Date dtLog = new Date();
  38. return sdfDate.format(dtLog);
  39. }
  40. public static int gapTime(String startTm, String endTm, int calcType) {
  41. if (startTm != null && !startTm.equals("") && startTm.length() == 14) {
  42. if (endTm != null && !endTm.equals("") && endTm.length() == 14) {
  43. Date startDateTime = TimeUtils.stringToDate(startTm);
  44. Date endDateTime = TimeUtils.stringToDate(endTm);
  45. if (startDateTime != null && endDateTime != null) {
  46. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  47. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  48. gcStartDateTime.setTime(startDateTime);
  49. gcEndDateTime.setTime(endDateTime);
  50. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  51. long hour = gap / 1000L / 60L / 60L;
  52. long min = gap / 1000L / 60L;
  53. long second = gap / 1000L;
  54. if (10 == calcType) {
  55. return (int)hour;
  56. } else if (12 == calcType) {
  57. return (int)min;
  58. } else {
  59. return 13 == calcType ? (int)second : -1;
  60. }
  61. } else {
  62. return -1;
  63. }
  64. } else {
  65. return -1;
  66. }
  67. } else {
  68. return -1;
  69. }
  70. }
  71. public static String byteArrayToString(byte[] data) {
  72. StringBuilder sb = new StringBuilder(data.length);
  73. for(int ii = 0; ii < data.length && data[ii] >= 33 && data[ii] <= 126; ++ii) {
  74. sb.append((char)data[ii]);
  75. }
  76. return sb.toString();
  77. }
  78. public static String byteArrayToHex(byte[] AData) {
  79. if (AData != null && AData.length != 0) {
  80. int ALen = AData.length;
  81. int line = ALen / 16;
  82. StringBuffer sb = new StringBuffer(ALen * 3 + line);
  83. sb.append("\r\n");
  84. for(int ii = 0; ii < ALen; ii += 16) {
  85. for(int jj = 0; jj < 16; ++jj) {
  86. int pos = ii + jj;
  87. if (pos >= ALen) {
  88. break;
  89. }
  90. String hexNumber = "0" + Integer.toHexString(255 & AData[pos]).toUpperCase();
  91. sb.append(hexNumber.substring(hexNumber.length() - 2));
  92. sb.append(" ");
  93. }
  94. sb.append("\r\n");
  95. }
  96. return sb.toString();
  97. } else {
  98. return "";
  99. }
  100. }
  101. public static int getBitValue(byte b, int pos) {
  102. int val = b >> pos & 1;
  103. return val;
  104. }
  105. public static long ipToLong(String ipAddress) {
  106. String[] ipAddressInArray = ipAddress.split("\\.");
  107. long result = 0L;
  108. for(int i = 0; i < ipAddressInArray.length; ++i) {
  109. int power = 3 - i;
  110. int ip = Integer.parseInt(ipAddressInArray[i]);
  111. result = (long)((double)result + (double)ip * Math.pow(256.0D, (double)power));
  112. }
  113. return result;
  114. }
  115. public static long ipToLong2(String ipAddress) {
  116. long result = 0L;
  117. String[] ipAddressInArray = ipAddress.split("\\.");
  118. for(int i = 3; i >= 0; --i) {
  119. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  120. result |= ip << i * 8;
  121. }
  122. return result;
  123. }
  124. public static String longToIp(long i) {
  125. return (i >> 24 & 255L) + "." + (i >> 16 & 255L) + "." + (i >> 8 & 255L) + "." + (i & 255L);
  126. }
  127. public static String longToIp2(long ip) {
  128. StringBuilder sb = new StringBuilder(15);
  129. for(int i = 0; i < 4; ++i) {
  130. sb.insert(0, Long.toString(ip & 255L));
  131. if (i < 3) {
  132. sb.insert(0, '.');
  133. }
  134. ip >>= 8;
  135. }
  136. return sb.toString();
  137. }
  138. }