SysUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.its.app.utils;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.GregorianCalendar;
  8. public final class SysUtils
  9. {
  10. public static int toInt(String paramVal, int defVal) {
  11. int result = defVal;
  12. try {
  13. result = Integer.parseInt(paramVal);
  14. } catch(Exception e) {
  15. }
  16. return result;
  17. }
  18. public static float toFloat(String paramVal, float defVal) {
  19. float result = defVal;
  20. try {
  21. result = Float.parseFloat(paramVal);
  22. } catch(Exception e) {
  23. }
  24. return result;
  25. }
  26. public static double toDouble(String paramVal, double defVal) {
  27. double result = defVal;
  28. try {
  29. result = Double.parseDouble(paramVal);
  30. } catch(Exception e) {
  31. }
  32. return result;
  33. }
  34. public static String getSysTime()
  35. {
  36. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  37. Date dtLog = new Date();
  38. return sdfDate.format(dtLog);
  39. }
  40. public static String getSysTimeStr() {
  41. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  42. Date dtLog = new Date();
  43. return sdfDate.format(dtLog);
  44. }
  45. public static int gapTime(String startTm, String endTm, int calcType) {
  46. if (startTm == null || startTm.equals("") || startTm.length() != 14) {
  47. return -1;
  48. }
  49. if (endTm == null || endTm.equals("") || endTm.length() != 14) {
  50. return -1;
  51. }
  52. Date startDateTime, endDateTime;
  53. /*
  54. FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHHmmss", new Locale("ko_KR")); //Locale.getDefault());
  55. try {
  56. startDateTime = fastDateFormat.parse(startTm);
  57. endDateTime = fastDateFormat.parse(endTm);
  58. } catch (ParseException e) {
  59. e.printStackTrace();
  60. return -1;
  61. }
  62. */
  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 String byteArrayToString(byte[] data) {
  85. StringBuilder sb = new StringBuilder(data.length);
  86. for (int ii = 0; ii < data.length; ++ ii) {
  87. if (data[ii] >= 0x21 && data[ii] <= 0x7E) {
  88. sb.append((char) data[ii]);
  89. }
  90. else {
  91. break;
  92. }
  93. }
  94. return sb.toString();
  95. }
  96. public static String byteArrayToHex(byte[] AData)
  97. {
  98. if ((AData == null) || (AData.length == 0)) {
  99. return "";
  100. }
  101. int ALen = AData.length;
  102. int line = ALen / 16;
  103. int pos;
  104. StringBuffer sb = new StringBuffer((ALen*3)+line);
  105. sb.append("\r\n");
  106. for (int ii = 0; ii < ALen; ii += 16)
  107. {
  108. for (int jj = 0; jj < 16; jj++) {
  109. pos = ii + jj;
  110. if (pos < ALen) {
  111. String hexNumber = "0" + Integer.toHexString(0xFF & AData[pos]).toUpperCase();
  112. sb.append(hexNumber.substring(hexNumber.length() - 2));
  113. sb.append(" ");
  114. }
  115. else {
  116. break;
  117. }
  118. }
  119. sb.append("\r\n");
  120. }
  121. return sb.toString();
  122. }
  123. public static int getBitValue(byte b, int pos) {
  124. int val = (b >> pos) & 0x01;
  125. return val;
  126. }
  127. public static long ipToLong(String ipAddress) {
  128. String[] ipAddressInArray = ipAddress.split("\\.");
  129. long result = 0;
  130. for (int i = 0; i < ipAddressInArray.length; i++) {
  131. int power = 3 - i;
  132. int ip = Integer.parseInt(ipAddressInArray[i]);
  133. result += ip * Math.pow(256, power);
  134. }
  135. return result;
  136. }
  137. public static long ipToLong2(String ipAddress) {
  138. long result = 0;
  139. String[] ipAddressInArray = ipAddress.split("\\.");
  140. for (int i = 3; i >= 0; i--) {
  141. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  142. //left shifting 24,16,8,0 and bitwise OR
  143. //1. 192 << 24
  144. //1. 168 << 16
  145. //1. 1 << 8
  146. //1. 2 << 0
  147. result |= ip << (i * 8);
  148. }
  149. return result;
  150. }
  151. public static String longToIp(long i) {
  152. return ((i >> 24) & 0xFF) +
  153. "." + ((i >> 16) & 0xFF) +
  154. "." + ((i >> 8) & 0xFF) +
  155. "." + (i & 0xFF);
  156. }
  157. public static String longToIp2(long ip) {
  158. StringBuilder sb = new StringBuilder(15);
  159. for (int i = 0; i < 4; i++) {
  160. // 1. 2
  161. // 2. 1
  162. // 3. 168
  163. // 4. 192
  164. sb.insert(0, Long.toString(ip & 0xff));
  165. if (i < 3) {
  166. sb.insert(0, '.');
  167. }
  168. // 1. 192.168.1.2
  169. // 2. 192.168.1
  170. // 3. 192.168
  171. // 4. 192
  172. ip = ip >> 8;
  173. }
  174. return sb.toString();
  175. }
  176. public static int bytesToInt(byte[] bytes) {
  177. return ByteBuffer.wrap(bytes).getInt();
  178. }
  179. public static int bytesToInt(byte[] bytes, int fromIdx, ByteOrder byteOrder) {
  180. if (byteOrder == ByteOrder.BIG_ENDIAN) {
  181. return (
  182. ((bytes[fromIdx+0] & 0xFF) << 24) |
  183. ((bytes[fromIdx+1] & 0xFF) << 16) |
  184. ((bytes[fromIdx+2] & 0xFF) << 8 ) |
  185. ((bytes[fromIdx+3] & 0xFF) << 0 )
  186. );
  187. }
  188. return (
  189. ((bytes[fromIdx+3] & 0xFF) << 24) |
  190. ((bytes[fromIdx+2] & 0xFF) << 16) |
  191. ((bytes[fromIdx+1] & 0xFF) << 8 ) |
  192. ((bytes[fromIdx+0] & 0xFF) << 0 )
  193. );
  194. }
  195. public static byte[] intToBytes(int value) {
  196. // BIG_ENDIAN
  197. return ByteBuffer.allocate(4).putInt(value).array();
  198. }
  199. }