SysUtils.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.its.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. /*
  64. FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHHmmss", new Locale("ko_KR")); //Locale.getDefault());
  65. try {
  66. startDateTime = fastDateFormat.parse(startTm);
  67. endDateTime = fastDateFormat.parse(endTm);
  68. } catch (ParseException e) {
  69. e.printStackTrace();
  70. return -1;
  71. }
  72. */
  73. startDateTime = TimeUtils.stringToDate(startTm);
  74. endDateTime = TimeUtils.stringToDate(endTm);
  75. if (startDateTime == null || endDateTime == null) {
  76. return -1;
  77. }
  78. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  79. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  80. gcStartDateTime.setTime(startDateTime);
  81. gcEndDateTime.setTime(endDateTime);
  82. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  83. long hour = gap / 1000L / 60L / 60L;
  84. long min = gap / 1000L / 60L;
  85. long second = gap / 1000L;
  86. if (Calendar.HOUR == calcType)
  87. return (int)hour;
  88. if (Calendar.MINUTE == calcType)
  89. return (int)min;
  90. if (Calendar.SECOND == calcType)
  91. return (int)second;
  92. return -1;
  93. }
  94. public static byte[] stringToByteArray(String data) {
  95. return data.getBytes();
  96. }
  97. public static void copyStringToByteArray(byte[] dest, int length, String data) {
  98. byte[] byteData = data.getBytes();
  99. int size = dest.length;
  100. int ii;
  101. for (ii = 0; ii < byteData.length && ii < size; ii++) {
  102. dest[ii] = byteData[ii];
  103. }
  104. for (int jj = ii; jj < size; jj++) {
  105. dest[jj] = 0x00;
  106. }
  107. }
  108. public static String byteArrayToString(byte[] data) {
  109. StringBuilder sb = new StringBuilder(data.length);
  110. for (int ii = 0; ii < data.length; ++ ii) {
  111. if (data[ii] >= 0x21 && data[ii] <= 0x7E) {
  112. sb.append((char) data[ii]);
  113. }
  114. else {
  115. break;
  116. }
  117. }
  118. return sb.toString();
  119. }
  120. public static String byteArrayToHex(byte[] AData)
  121. {
  122. if ((AData == null) || (AData.length == 0)) {
  123. return "";
  124. }
  125. int ALen = AData.length;
  126. int line = ALen / 16;
  127. int pos;
  128. StringBuffer sb = new StringBuffer((ALen*3)+line);
  129. sb.append("\r\n");
  130. for (int ii = 0; ii < ALen; ii += 16)
  131. {
  132. for (int jj = 0; jj < 16; jj++) {
  133. pos = ii + jj;
  134. if (pos < ALen) {
  135. String hexNumber = "0" + Integer.toHexString(0xFF & AData[pos]).toUpperCase();
  136. sb.append(hexNumber.substring(hexNumber.length() - 2));
  137. sb.append(" ");
  138. }
  139. else {
  140. break;
  141. }
  142. }
  143. sb.append("\r\n");
  144. }
  145. return sb.toString();
  146. }
  147. public static int getBitValue(byte b, int pos) {
  148. int val = (b >> pos) & 0x01;
  149. return val;
  150. }
  151. public static long ipToLong(String ipAddress) {
  152. String[] ipAddressInArray = ipAddress.split("\\.");
  153. long result = 0;
  154. for (int i = 0; i < ipAddressInArray.length; i++) {
  155. int power = 3 - i;
  156. int ip = Integer.parseInt(ipAddressInArray[i]);
  157. result += ip * Math.pow(256, power);
  158. }
  159. return result;
  160. }
  161. public static long ipToLong2(String ipAddress) {
  162. long result = 0;
  163. String[] ipAddressInArray = ipAddress.split("\\.");
  164. for (int i = 3; i >= 0; i--) {
  165. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  166. //left shifting 24,16,8,0 and bitwise OR
  167. //1. 192 << 24
  168. //1. 168 << 16
  169. //1. 1 << 8
  170. //1. 2 << 0
  171. result |= ip << (i * 8);
  172. }
  173. return result;
  174. }
  175. public static String longToIp(long i) {
  176. return ((i >> 24) & 0xFF) +
  177. "." + ((i >> 16) & 0xFF) +
  178. "." + ((i >> 8) & 0xFF) +
  179. "." + (i & 0xFF);
  180. }
  181. public static String longToIp2(long ip) {
  182. StringBuilder sb = new StringBuilder(15);
  183. for (int i = 0; i < 4; i++) {
  184. // 1. 2
  185. // 2. 1
  186. // 3. 168
  187. // 4. 192
  188. sb.insert(0, Long.toString(ip & 0xff));
  189. if (i < 3) {
  190. sb.insert(0, '.');
  191. }
  192. // 1. 192.168.1.2
  193. // 2. 192.168.1
  194. // 3. 192.168
  195. // 4. 192
  196. ip = ip >> 8;
  197. }
  198. return sb.toString();
  199. }
  200. public static int bytesToInt(byte[] bytes) {
  201. return ByteBuffer.wrap(bytes).getInt();
  202. }
  203. public static int bytesToInt(byte[] bytes, int fromIdx, ByteOrder byteOrder) {
  204. if (byteOrder == ByteOrder.BIG_ENDIAN) {
  205. return (
  206. ((bytes[fromIdx+0] & 0xFF) << 24) |
  207. ((bytes[fromIdx+1] & 0xFF) << 16) |
  208. ((bytes[fromIdx+2] & 0xFF) << 8 ) |
  209. ((bytes[fromIdx+3] & 0xFF) << 0 )
  210. );
  211. }
  212. return (
  213. ((bytes[fromIdx+3] & 0xFF) << 24) |
  214. ((bytes[fromIdx+2] & 0xFF) << 16) |
  215. ((bytes[fromIdx+1] & 0xFF) << 8 ) |
  216. ((bytes[fromIdx+0] & 0xFF) << 0 )
  217. );
  218. }
  219. public static byte[] intToBytes(int value) {
  220. // BIG_ENDIAN
  221. return ByteBuffer.allocate(4).putInt(value).array();
  222. }
  223. public static int bytesToShort(byte[] bytes) {
  224. return ByteBuffer.wrap(bytes).getShort();
  225. }
  226. public static int bytesToShort(byte[] bytes, int fromIdx, ByteOrder byteOrder) {
  227. if (byteOrder == ByteOrder.BIG_ENDIAN) {
  228. return (
  229. ((bytes[fromIdx+0] & 0xFF) << 8) |
  230. ((bytes[fromIdx+1] & 0xFF) << 0 )
  231. );
  232. }
  233. return (
  234. ((bytes[fromIdx+1] & 0xFF) << 8 ) |
  235. ((bytes[fromIdx+0] & 0xFF) << 0 )
  236. );
  237. }
  238. public static byte[] shortToBytes(short value) {
  239. // BIG_ENDIAN
  240. return ByteBuffer.allocate(2).putShort(value).array();
  241. }
  242. }