SysUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.its.op.utils;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.GregorianCalendar;
  6. public final class SysUtils
  7. {
  8. public static int toInt(String paramVal, int defVal) {
  9. int result = defVal;
  10. try {
  11. result = Integer.parseInt(paramVal);
  12. } catch(Exception e) {
  13. }
  14. return result;
  15. }
  16. public static float toFloat(String paramVal, float defVal) {
  17. float result = defVal;
  18. try {
  19. result = Float.parseFloat(paramVal);
  20. } catch(Exception e) {
  21. }
  22. return result;
  23. }
  24. public static double toDouble(String paramVal, double defVal) {
  25. double result = defVal;
  26. try {
  27. result = Double.parseDouble(paramVal);
  28. } catch(Exception e) {
  29. }
  30. return result;
  31. }
  32. public static String getSysTime()
  33. {
  34. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  35. Date dtLog = new Date();
  36. return sdfDate.format(dtLog);
  37. }
  38. public static String getSysTimeStr() {
  39. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  40. Date dtLog = new Date();
  41. return sdfDate.format(dtLog);
  42. }
  43. public static int gapTime(String startTm, String endTm, int calcType) {
  44. if (startTm == null || startTm.equals("") || startTm.length() != 14) {
  45. return -1;
  46. }
  47. if (endTm == null || endTm.equals("") || endTm.length() != 14) {
  48. return -1;
  49. }
  50. Date startDateTime, endDateTime;
  51. /*
  52. FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHHmmss", new Locale("ko_KR")); //Locale.getDefault());
  53. try {
  54. startDateTime = fastDateFormat.parse(startTm);
  55. endDateTime = fastDateFormat.parse(endTm);
  56. } catch (ParseException e) {
  57. e.printStackTrace();
  58. return -1;
  59. }
  60. */
  61. startDateTime = TimeUtils.stringToDate(startTm);
  62. endDateTime = TimeUtils.stringToDate(endTm);
  63. if (startDateTime == null || endDateTime == null) {
  64. return -1;
  65. }
  66. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  67. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  68. gcStartDateTime.setTime(startDateTime);
  69. gcEndDateTime.setTime(endDateTime);
  70. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  71. long hour = gap / 1000L / 60L / 60L;
  72. long min = gap / 1000L / 60L;
  73. long second = gap / 1000L;
  74. if (Calendar.HOUR == calcType)
  75. return (int)hour;
  76. if (Calendar.MINUTE == calcType)
  77. return (int)min;
  78. if (Calendar.SECOND == calcType)
  79. return (int)second;
  80. return -1;
  81. }
  82. public static String byteArrayToString(byte[] data) {
  83. StringBuilder sb = new StringBuilder(data.length);
  84. for (int ii = 0; ii < data.length; ++ ii) {
  85. if (data[ii] >= 0x21 && data[ii] <= 0x7E) {
  86. sb.append((char) data[ii]);
  87. }
  88. else {
  89. break;
  90. }
  91. }
  92. return sb.toString();
  93. }
  94. public static String byteArrayToHex(byte[] AData)
  95. {
  96. if ((AData == null) || (AData.length == 0)) {
  97. return "";
  98. }
  99. int ALen = AData.length;
  100. int line = ALen / 16;
  101. int pos;
  102. StringBuffer sb = new StringBuffer((ALen*3)+line);
  103. sb.append("\r\n");
  104. for (int ii = 0; ii < ALen; ii += 16)
  105. {
  106. for (int jj = 0; jj < 16; jj++) {
  107. pos = ii + jj;
  108. if (pos < ALen) {
  109. String hexNumber = "0" + Integer.toHexString(0xFF & AData[pos]).toUpperCase();
  110. sb.append(hexNumber.substring(hexNumber.length() - 2));
  111. sb.append(" ");
  112. }
  113. else {
  114. break;
  115. }
  116. }
  117. sb.append("\r\n");
  118. }
  119. return sb.toString();
  120. }
  121. public static int getBitValue(byte b, int pos) {
  122. int val = (b >> pos) & 0x01;
  123. return val;
  124. }
  125. public static long ipToLong(String ipAddress) {
  126. String[] ipAddressInArray = ipAddress.split("\\.");
  127. long result = 0;
  128. for (int i = 0; i < ipAddressInArray.length; i++) {
  129. int power = 3 - i;
  130. int ip = Integer.parseInt(ipAddressInArray[i]);
  131. result += ip * Math.pow(256, power);
  132. }
  133. return result;
  134. }
  135. public static long ipToLong2(String ipAddress) {
  136. long result = 0;
  137. String[] ipAddressInArray = ipAddress.split("\\.");
  138. for (int i = 3; i >= 0; i--) {
  139. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  140. //left shifting 24,16,8,0 and bitwise OR
  141. //1. 192 << 24
  142. //1. 168 << 16
  143. //1. 1 << 8
  144. //1. 2 << 0
  145. result |= ip << (i * 8);
  146. }
  147. return result;
  148. }
  149. public static String longToIp(long i) {
  150. return ((i >> 24) & 0xFF) +
  151. "." + ((i >> 16) & 0xFF) +
  152. "." + ((i >> 8) & 0xFF) +
  153. "." + (i & 0xFF);
  154. }
  155. public static String longToIp2(long ip) {
  156. StringBuilder sb = new StringBuilder(15);
  157. for (int i = 0; i < 4; i++) {
  158. // 1. 2
  159. // 2. 1
  160. // 3. 168
  161. // 4. 192
  162. sb.insert(0, Long.toString(ip & 0xff));
  163. if (i < 3) {
  164. sb.insert(0, '.');
  165. }
  166. // 1. 192.168.1.2
  167. // 2. 192.168.1
  168. // 3. 192.168
  169. // 4. 192
  170. ip = ip >> 8;
  171. }
  172. return sb.toString();
  173. }
  174. }