SysUtils.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. ***************************************************************************************************
  3. * Copyright �� 2009-2010 HANTE Information Co., Ltd. Seoul, Korea
  4. * All rights reserved.
  5. *
  6. * �ý��� ��ƿ��Ƽ Ŭ����
  7. *
  8. * Programmer(s): shjung@hanteinfo.com
  9. *
  10. * REVISION HISTORY
  11. * 1. [2010/12/22][VER-1.0][shjung] : created
  12. * 2.
  13. ***************************************************************************************************
  14. */
  15. package com.its.utils;
  16. import org.apache.commons.lang3.time.FastDateFormat;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import java.util.GregorianCalendar;
  22. import java.util.Locale;
  23. /**
  24. * A class to SysUtils
  25. *
  26. *
  27. */
  28. public final class SysUtils
  29. {
  30. private SysUtils()
  31. {
  32. }
  33. public static int toInt(String str, int defaultValue) {
  34. if (str == null) {
  35. return defaultValue;
  36. } else {
  37. try {
  38. return Integer.parseInt(str);
  39. } catch (NumberFormatException var3) {
  40. return defaultValue;
  41. }
  42. }
  43. }
  44. public static String getMethodName() {
  45. return Thread.currentThread().getStackTrace()[1].getMethodName();
  46. }
  47. public static int parseInteger(String paramVal, int defVal) {
  48. int result = defVal;
  49. try {
  50. result = Integer.parseInt(paramVal);
  51. } catch(Exception e) {
  52. }
  53. return result;
  54. }
  55. public static float parseFloat(String paramVal, float defVal) {
  56. float result = defVal;
  57. try {
  58. result = Float.parseFloat(paramVal);
  59. } catch(Exception e) {
  60. }
  61. return result;
  62. }
  63. public static double parseDouble(String paramVal, double defVal) {
  64. double result = defVal;
  65. try {
  66. result = Double.parseDouble(paramVal);
  67. } catch(Exception e) {
  68. }
  69. return result;
  70. }
  71. /**
  72. * 현재시각을 문자열로 얻는다.
  73. *
  74. */
  75. public static String getSysTimeStr() {
  76. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  77. java.util.Date dtLog = new java.util.Date();
  78. return sdfDate.format(dtLog);
  79. }
  80. public static String getSysTime()
  81. {
  82. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  83. java.util.Date dtLog = new java.util.Date();
  84. return sdfDate.format(dtLog);
  85. /*
  86. Calendar now = Calendar.getInstance();
  87. String year = "0000" + now.get(Calendar.YEAR);
  88. String mon = "00" + now.get(Calendar.MONTH) + 1;
  89. String day = "00" + now.get(Calendar.DAY_OF_MONTH);
  90. String hour = "00" + now.get(Calendar.HOUR_OF_DAY);
  91. String min = "00" + now.get(Calendar.MINUTE);
  92. String sec = "00" + now.get(Calendar.SECOND);
  93. year = year.substring(year.length() - 4, year.length());
  94. mon = mon.substring( mon.length() - 2, mon.length());
  95. day = day.substring( day.length() - 2, day.length());
  96. hour = hour.substring(hour.length() - 2, hour.length());
  97. min = min.substring( min.length() - 2, min.length());
  98. sec = sec.substring( sec.length() - 2, sec.length());
  99. return year+mon+day+hour+min+sec;
  100. */
  101. }
  102. public static int gapTime(String startTm, String endTm, int calcType) {
  103. if (startTm == null || startTm.equals("") || startTm.length() != 14) {
  104. return -1;
  105. }
  106. if (endTm == null || endTm.equals("") || endTm.length() != 14) {
  107. return -1;
  108. }
  109. FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHHmmss", new Locale("ko_KR")); //Locale.getDefault());
  110. Date startDateTime, endDateTime;
  111. try {
  112. startDateTime = fastDateFormat.parse(startTm);
  113. endDateTime = fastDateFormat.parse(endTm);
  114. } catch (ParseException e) {
  115. e.printStackTrace();
  116. return -1;
  117. }
  118. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  119. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  120. gcStartDateTime.setTime(startDateTime);
  121. gcEndDateTime.setTime(endDateTime);
  122. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  123. long hour = gap / 1000L / 60L / 60L;
  124. long min = gap / 1000L / 60L;
  125. long second = gap / 1000L;
  126. if (Calendar.HOUR == calcType)
  127. return (int)hour;
  128. if (Calendar.MINUTE == calcType)
  129. return (int)min;
  130. if (Calendar.SECOND == calcType)
  131. return (int)second;
  132. return -1;
  133. }
  134. public static long ipToLong(String ipAddress) {
  135. String[] ipAddressInArray = ipAddress.split("\\.");
  136. long result = 0;
  137. for (int i = 0; i < ipAddressInArray.length; i++) {
  138. int power = 3 - i;
  139. int ip = Integer.parseInt(ipAddressInArray[i]);
  140. result += ip * Math.pow(256, power);
  141. }
  142. return result;
  143. }
  144. public static long ipToLong2(String ipAddress) {
  145. long result = 0;
  146. String[] ipAddressInArray = ipAddress.split("\\.");
  147. for (int i = 3; i >= 0; i--) {
  148. long ip = Long.parseLong(ipAddressInArray[3 - i]);
  149. //left shifting 24,16,8,0 and bitwise OR
  150. //1. 192 << 24
  151. //1. 168 << 16
  152. //1. 1 << 8
  153. //1. 2 << 0
  154. result |= ip << (i * 8);
  155. }
  156. return result;
  157. }
  158. public static String longToIp(long i) {
  159. return ((i >> 24) & 0xFF) +
  160. "." + ((i >> 16) & 0xFF) +
  161. "." + ((i >> 8) & 0xFF) +
  162. "." + (i & 0xFF);
  163. }
  164. public static String longToIp2(long ip) {
  165. StringBuilder sb = new StringBuilder(15);
  166. for (int i = 0; i < 4; i++) {
  167. // 1. 2
  168. // 2. 1
  169. // 3. 168
  170. // 4. 192
  171. sb.insert(0, Long.toString(ip & 0xff));
  172. if (i < 3) {
  173. sb.insert(0, '.');
  174. }
  175. // 1. 192.168.1.2
  176. // 2. 192.168.1
  177. // 3. 192.168
  178. // 4. 192
  179. ip = ip >> 8;
  180. }
  181. return sb.toString();
  182. }
  183. public static String byteArrayToHex(byte[] AData)
  184. {
  185. if ((AData == null) || (AData.length == 0)) {
  186. return "";
  187. }
  188. int ALen = AData.length;
  189. int line = ALen / 16;
  190. int pos;
  191. StringBuffer sb = new StringBuffer((ALen*3)+line);
  192. sb.append("\r\n");
  193. for (int ii = 0; ii < ALen; ii += 16)
  194. {
  195. for (int jj = 0; jj < 16; jj++) {
  196. pos = ii + jj;
  197. if (pos < ALen) {
  198. String hexNumber = "0" + Integer.toHexString(0xFF & AData[pos]).toUpperCase();
  199. sb.append(hexNumber.substring(hexNumber.length() - 2));
  200. sb.append(" ");
  201. }
  202. else {
  203. break;
  204. }
  205. }
  206. sb.append("\r\n");
  207. }
  208. return sb.toString();
  209. }
  210. public static int getBitValue(byte b, int pos) {
  211. int val = (b >> pos) & 0x01;
  212. //int val = (b >> (7-pos)) & 0x01;
  213. return val;
  214. }
  215. public static String bytes2String(byte[] data) {
  216. StringBuilder sb = new StringBuilder(data.length);
  217. for (int ii = 0; ii < data.length; ++ ii) {
  218. if (data[ii] >= 0x21 && data[ii] <= 0x7E) {
  219. sb.append((char) data[ii]);
  220. }
  221. else {
  222. break; //FOR VDS Protocol
  223. }
  224. }
  225. return sb.toString();
  226. }
  227. public static short swapEndian(short x)
  228. {
  229. return (short)(x << 8 | x >> 8 & 0xFF);
  230. }
  231. public static int swapEndian(int x)
  232. {
  233. return swapEndian((short)x) << 16 | swapEndian((short)(x >> 16)) & 0xFFFF;
  234. }
  235. // byte[] byteArray = ByteBuffer.allocate(4).putInt(value).array();
  236. public static byte[] intToByteArray(int value) {
  237. byte[] byteArray = new byte[4];
  238. byteArray[0] = (byte)(value >> 24);
  239. byteArray[1] = (byte)(value >> 16);
  240. byteArray[2] = (byte)(value >> 8);
  241. byteArray[3] = (byte)(value);
  242. return byteArray;
  243. }
  244. public static int byteArrayToInt(byte bytes[]) {
  245. return ((((int)bytes[0] & 0xff) << 24) |
  246. (((int)bytes[1] & 0xff) << 16) |
  247. (((int)bytes[2] & 0xff) << 8) |
  248. (((int)bytes[3] & 0xff)));
  249. }
  250. public static byte[] floatToByteArray(float value) {
  251. int floatValue = Float.floatToIntBits(value);
  252. return intToByteArray(floatValue);
  253. }
  254. public static float byteArrayToFloat(byte bytes[]) {
  255. int value = byteArrayToInt(bytes);
  256. return Float.intBitsToFloat(value);
  257. }
  258. public static long toUnsigned(int value) {
  259. return ((long)value & 0xFFFFFFFFL);
  260. }
  261. }