SysUtils.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.sig.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.GregorianCalendar;
  9. @Slf4j
  10. public final class SysUtils
  11. {
  12. public static int toInt(String paramVal, int defVal) {
  13. int result = defVal;
  14. try {
  15. result = Integer.parseInt(paramVal);
  16. } catch(NumberFormatException e) {
  17. log.error("toInt: NumberFormatException");
  18. }
  19. return result;
  20. }
  21. public static float toFloat(String paramVal, float defVal) {
  22. float result = defVal;
  23. try {
  24. result = Float.parseFloat(paramVal);
  25. } catch(NumberFormatException e) {
  26. log.error("toFloat: NumberFormatException");
  27. }
  28. return result;
  29. }
  30. public static double toDouble(String paramVal, double defVal) {
  31. double result = defVal;
  32. try {
  33. result = Double.parseDouble(paramVal);
  34. } catch(NumberFormatException e) {
  35. log.error("toDouble: NumberFormatException");
  36. }
  37. return result;
  38. }
  39. public static String getSysTime()
  40. {
  41. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  42. Date dtLog = new Date();
  43. return sdfDate.format(dtLog);
  44. }
  45. public static String getSysTimeStr() {
  46. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  47. Date dtLog = new Date();
  48. return sdfDate.format(dtLog);
  49. }
  50. public static String getSysTimeStrMMDD() {
  51. SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd HH:mm:ss");
  52. Date dtLog = new Date();
  53. return sdfDate.format(dtLog);
  54. }
  55. public static int gapTime(String startTm, String endTm, int calcType) {
  56. if (startTm == null || startTm.equals("") || startTm.length() != 14) {
  57. return -1;
  58. }
  59. if (endTm == null || endTm.equals("") || endTm.length() != 14) {
  60. return -1;
  61. }
  62. Date startDateTime, endDateTime;
  63. startDateTime = TimeUtils.stringToDate(startTm);
  64. endDateTime = TimeUtils.stringToDate(endTm);
  65. if (startDateTime == null || endDateTime == null) {
  66. return -1;
  67. }
  68. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  69. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  70. gcStartDateTime.setTime(startDateTime);
  71. gcEndDateTime.setTime(endDateTime);
  72. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  73. long hour = gap / 1000L / 60L / 60L;
  74. long min = gap / 1000L / 60L;
  75. long second = gap / 1000L;
  76. if (Calendar.HOUR == calcType)
  77. return (int)hour;
  78. if (Calendar.MINUTE == calcType)
  79. return (int)min;
  80. if (Calendar.SECOND == calcType)
  81. return (int)second;
  82. return -1;
  83. }
  84. public static byte[] stringToByteArray(String data) {
  85. return data.getBytes();
  86. }
  87. public static void copyStringToByteArray(byte[] dest, int length, String data) {
  88. byte[] byteData = data.getBytes();
  89. int size = dest.length;
  90. int ii;
  91. for (ii = 0; ii < byteData.length && ii < size; ii++) {
  92. dest[ii] = byteData[ii];
  93. }
  94. for (int jj = ii; jj < size; jj++) {
  95. dest[jj] = 0x00;
  96. }
  97. }
  98. public static String byteArrayToString(byte[] data) {
  99. StringBuilder sb = new StringBuilder(data.length);
  100. for (int ii = 0; ii < data.length; ++ ii) {
  101. if (data[ii] >= 0x21 && data[ii] <= 0x7E) {
  102. sb.append((char) data[ii]);
  103. }
  104. else {
  105. break;
  106. }
  107. }
  108. return sb.toString();
  109. }
  110. public static String byteArrayToHex(byte[] AData)
  111. {
  112. if ((AData == null) || (AData.length == 0)) {
  113. return "";
  114. }
  115. int ALen = AData.length;
  116. int line = ALen / 16;
  117. int pos;
  118. StringBuffer sb = new StringBuffer((ALen*3)+line);
  119. sb.append("\r\n");
  120. for (int ii = 0; ii < ALen; ii += 16)
  121. {
  122. for (int jj = 0; jj < 16; jj++) {
  123. pos = ii + jj;
  124. if (pos < ALen) {
  125. String hexNumber = "0" + Integer.toHexString(0xFF & AData[pos]).toUpperCase();
  126. sb.append(hexNumber.substring(hexNumber.length() - 2));
  127. sb.append(" ");
  128. }
  129. else {
  130. break;
  131. }
  132. }
  133. sb.append("\r\n");
  134. }
  135. return sb.toString();
  136. }
  137. public static int getBitValue(byte b, int pos) {
  138. int val = (b >> pos) & 0x01;
  139. return val;
  140. }
  141. public static long ipToLong(String ipAddress) {
  142. String[] ipAddressInArray = ipAddress.split("\\.");
  143. long result = 0;
  144. for (int i = 0; i < ipAddressInArray.length; i++) {
  145. int power = 3 - i;
  146. int ip = Integer.parseInt(ipAddressInArray[i]);
  147. result += ip * Math.pow(256, power);
  148. }
  149. return result;
  150. }
  151. public static long ipToLong2(String ipAddress) {
  152. long result = 0;
  153. String[] ipAddressInArray = ipAddress.split("\\.");
  154. for (int i = 3; i >= 0; i--) {
  155. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  156. //left shifting 24,16,8,0 and bitwise OR
  157. //1. 192 << 24
  158. //1. 168 << 16
  159. //1. 1 << 8
  160. //1. 2 << 0
  161. result |= ip << (i * 8);
  162. }
  163. return result;
  164. }
  165. public static String longToIp(long i) {
  166. return ((i >> 24) & 0xFF) +
  167. "." + ((i >> 16) & 0xFF) +
  168. "." + ((i >> 8) & 0xFF) +
  169. "." + (i & 0xFF);
  170. }
  171. public static String longToIp2(long ip) {
  172. StringBuilder sb = new StringBuilder(15);
  173. for (int i = 0; i < 4; i++) {
  174. // 1. 2
  175. // 2. 1
  176. // 3. 168
  177. // 4. 192
  178. sb.insert(0, Long.toString(ip & 0xff));
  179. if (i < 3) {
  180. sb.insert(0, '.');
  181. }
  182. // 1. 192.168.1.2
  183. // 2. 192.168.1
  184. // 3. 192.168
  185. // 4. 192
  186. ip = ip >> 8;
  187. }
  188. return sb.toString();
  189. }
  190. public static int bytesToInt(byte[] bytes) {
  191. return ByteBuffer.wrap(bytes).getInt();
  192. }
  193. public static int bytesToInt(byte[] bytes, int fromIdx, ByteOrder byteOrder) {
  194. if (byteOrder == ByteOrder.BIG_ENDIAN) {
  195. return (
  196. ((bytes[fromIdx+0] & 0xFF) << 24) |
  197. ((bytes[fromIdx+1] & 0xFF) << 16) |
  198. ((bytes[fromIdx+2] & 0xFF) << 8 ) |
  199. ((bytes[fromIdx+3] & 0xFF) << 0 )
  200. );
  201. }
  202. return (
  203. ((bytes[fromIdx+3] & 0xFF) << 24) |
  204. ((bytes[fromIdx+2] & 0xFF) << 16) |
  205. ((bytes[fromIdx+1] & 0xFF) << 8 ) |
  206. ((bytes[fromIdx+0] & 0xFF) << 0 )
  207. );
  208. }
  209. public static byte[] intToBytes(int value) {
  210. // BIG_ENDIAN
  211. return ByteBuffer.allocate(4).putInt(value).array();
  212. }
  213. public static int bytesToShort(byte[] bytes) {
  214. return ByteBuffer.wrap(bytes).getShort();
  215. }
  216. public static int bytesToShort(byte[] bytes, int fromIdx, ByteOrder byteOrder) {
  217. if (byteOrder == ByteOrder.BIG_ENDIAN) {
  218. return (
  219. ((bytes[fromIdx+0] & 0xFF) << 8) |
  220. ((bytes[fromIdx+1] & 0xFF) << 0 )
  221. );
  222. }
  223. return (
  224. ((bytes[fromIdx+1] & 0xFF) << 8 ) |
  225. ((bytes[fromIdx+0] & 0xFF) << 0 )
  226. );
  227. }
  228. public static byte[] shortToBytes(short value) {
  229. // BIG_ENDIAN
  230. return ByteBuffer.allocate(2).putShort(value).array();
  231. }
  232. }