ItsUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.its.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.io.FileUtils;
  4. import javax.imageio.ImageIO;
  5. import javax.servlet.http.HttpServletRequest;
  6. import java.awt.image.BufferedImage;
  7. import java.io.*;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.*;
  14. @Slf4j
  15. public final class ItsUtils
  16. {
  17. // 요일유형
  18. public static final String UNKNOWN_WEEK = "DTW0";
  19. public static final String MONDAY = "DTW1";
  20. public static final String TUESDAY = "DTW2";
  21. public static final String WEDNESDAY = "DTW3";
  22. public static final String THURSDAY = "DTW4";
  23. public static final String FRIDAY = "DTW5";
  24. public static final String SATURDAY = "DTW6";
  25. public static final String SUNDAY = "DTW7";
  26. public static String getCurrFiveMinString() {
  27. Calendar cal = Calendar.getInstance();
  28. cal.setTime(new Date());
  29. cal.set(Calendar.SECOND, 0);
  30. cal.set(Calendar.MILLISECOND, 0);
  31. int min = cal.get(Calendar.MINUTE);
  32. cal.add(Calendar.MINUTE, -(min % 5));
  33. return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
  34. }
  35. public static Date getPrcsDt(String prcsDt) {
  36. SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  37. Date to = null;
  38. try {
  39. to = transFormat.parse(prcsDt);
  40. } catch (ParseException e) {
  41. log.error("getPrcsDt: ParseException");
  42. }
  43. return to;
  44. }
  45. public static String getMissSttsYn(String prcsDt) {
  46. if (prcsDt == null || prcsDt.length() != 14) {
  47. return "Y";
  48. }
  49. Date startDateTime = getPrcsDt(prcsDt);
  50. if (startDateTime == null) {
  51. return "Y";
  52. }
  53. Calendar currDateTime = Calendar.getInstance();
  54. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  55. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  56. gcStartDateTime.setTime(startDateTime);
  57. gcEndDateTime.setTime(currDateTime.getTime());
  58. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  59. long min = gap / 1000L / 60L;
  60. return min > 10 ? "Y" : "N";
  61. }
  62. public static String getRseCommStts(String prcsDt) {
  63. if (prcsDt == null || prcsDt.length() != 14) {
  64. return "CMS1";
  65. }
  66. Date startDateTime = getPrcsDt(prcsDt);
  67. if (startDateTime == null) {
  68. return "CMS1";
  69. }
  70. Calendar currDateTime = Calendar.getInstance();
  71. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  72. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  73. gcStartDateTime.setTime(startDateTime);
  74. gcEndDateTime.setTime(currDateTime.getTime());
  75. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  76. long min = gap / 1000L / 60L;
  77. return min > 30 ? "CMS1" : "CMS0";
  78. }
  79. public static String getServiceYn(String prcsDt) {
  80. if (prcsDt == null || prcsDt.length() != 14) {
  81. return "N";
  82. }
  83. Date startDateTime = getPrcsDt(prcsDt);
  84. if (startDateTime == null) {
  85. return "N";
  86. }
  87. Calendar currDateTime = Calendar.getInstance();
  88. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  89. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  90. gcStartDateTime.setTime(startDateTime);
  91. gcEndDateTime.setTime(currDateTime.getTime());
  92. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  93. long min = gap / 1000L / 60L;
  94. return min > 10 ? "N" : "Y";
  95. }
  96. public static String getMissYn(String prcsDt, String CMTR_GRAD_CD) {
  97. if (("LTC0").equals(CMTR_GRAD_CD) || ("0").equals(CMTR_GRAD_CD)) {
  98. return "Y";
  99. }
  100. if (prcsDt == null || prcsDt.length() != 14) {
  101. return "Y";
  102. }
  103. Date startDateTime = getPrcsDt(prcsDt);
  104. if (startDateTime == null) {
  105. return "Y";
  106. }
  107. Calendar currDateTime = Calendar.getInstance();
  108. GregorianCalendar gcStartDateTime = new GregorianCalendar();
  109. GregorianCalendar gcEndDateTime = new GregorianCalendar();
  110. gcStartDateTime.setTime(startDateTime);
  111. gcEndDateTime.setTime(currDateTime.getTime());
  112. long gap = gcEndDateTime.getTimeInMillis() - gcStartDateTime.getTimeInMillis();
  113. long min = gap / 1000L / 60L;
  114. return min > 10 ? "Y" : "N";
  115. }
  116. public static Date stringToDate(String paramTime) {
  117. SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  118. Date to = null;
  119. try {
  120. to = transFormat.parse(paramTime);
  121. } catch (ParseException e) {
  122. log.error("stringToDate: ParseException");
  123. }
  124. return to;
  125. }
  126. public static int getDiffMinutes(String fromDt, String toDt) {
  127. Date dtFrom = stringToDate(fromDt);
  128. Date dtTo = stringToDate(toDt);
  129. GregorianCalendar gcFromDt= new GregorianCalendar();
  130. GregorianCalendar gcToDt = new GregorianCalendar();
  131. gcFromDt.setTime(dtFrom);
  132. gcToDt.setTime(dtTo);
  133. long gap = gcToDt.getTimeInMillis() - gcFromDt.getTimeInMillis();
  134. long min = gap / 1000L / 60L;
  135. return (int)min;
  136. }
  137. // 0--6: Sunday = 0, C/C++ struct tm, tm_wday
  138. // 1--7: oracle, SELECT TO_CHAR(SYSDATE, 'D') FROM DUAL;, 주중의 일을 1~7로 표시(일요일이 1)
  139. // : java, Calendar cal; cal.get(Calendar.DAY_OF_WEEK, sunday=1, monday=2, ...)
  140. public static String getDayOfWeek() {
  141. Date dtNow = new Date();
  142. int week = getDayWeek(dtNow);
  143. return getDayOfWeek(week);
  144. }
  145. public static String getDayOfWeek(int week) {
  146. String sWeek = UNKNOWN_WEEK;
  147. switch(week)
  148. {
  149. case 1: sWeek = SUNDAY; break;
  150. case 2: sWeek = MONDAY; break;
  151. case 3: sWeek = TUESDAY; break;
  152. case 4: sWeek = WEDNESDAY; break;
  153. case 5: sWeek = THURSDAY; break;
  154. case 6: sWeek = FRIDAY; break;
  155. case 7: sWeek = SATURDAY; break;
  156. }
  157. return sWeek;
  158. }
  159. public static int getDayWeek(String paramDt) {
  160. return getDayWeek(stringToDate(paramDt));
  161. }
  162. public static int getDayWeek(Date paramDt) {
  163. Calendar cal = Calendar.getInstance();
  164. cal.setTime(paramDt);
  165. return cal.get(Calendar.DAY_OF_WEEK); /* DAY_OF_WEEK 리턴값이 일요일(1), 월요일(2), 화요일(3) ~~ 토요일(7)을 반환합니다. */
  166. }
  167. public static String getFromToday()
  168. {
  169. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
  170. Date dtNow = new Date();
  171. return sdfDate.format(dtNow)+"000000";
  172. }
  173. public static String getSysTime()
  174. {
  175. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMddHHmmss");
  176. Date dtNow = new Date();
  177. return sdfDate.format(dtNow);
  178. }
  179. public static String getSysTime(String format)
  180. {
  181. SimpleDateFormat sdfDate = new SimpleDateFormat(format);
  182. Date dtNow = new Date();
  183. return sdfDate.format(dtNow);
  184. }
  185. public static String getSysMonth()
  186. {
  187. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMM");
  188. Date dtNow = new Date();
  189. return sdfDate.format(dtNow);
  190. }
  191. public static String getSysDay()
  192. {
  193. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd");
  194. Date dtNow = new Date();
  195. return sdfDate.format(dtNow);
  196. }
  197. /**
  198. * 월 마지막날 가져오기 * @param yyyyMM
  199. * @return : yyyyMMdd
  200. */
  201. public static String getLastDayOfMonth(String yyyyMMdd) {
  202. String year = yyyyMMdd.substring(0, 4);
  203. String month = yyyyMMdd.substring(4, 6);
  204. Calendar cal = Calendar.getInstance();
  205. cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
  206. String lastDay = year + month + Integer.toString(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  207. return lastDay;
  208. }
  209. public static void saveByteArrayToFile(String filePath, byte[] byteArrayData) {
  210. try {
  211. FileUtils.writeByteArrayToFile(new File(filePath), byteArrayData);
  212. }
  213. catch (IOException e) {
  214. log.error("saveByteArrayToFile: IOException");
  215. }
  216. }
  217. public static byte[] convertBmpToPng(byte[] bmpArrayData) {
  218. ByteArrayOutputStream out = new ByteArrayOutputStream();
  219. ByteArrayInputStream inStream = new ByteArrayInputStream(bmpArrayData);
  220. try {
  221. BufferedImage newImage = ImageIO.read(inStream);
  222. ImageIO.write(newImage, "png", out);
  223. return out.toByteArray();
  224. }
  225. catch (IOException e) {
  226. log.error("saveByteArrayToFile: IOException");
  227. }
  228. finally {
  229. try {
  230. out.close();
  231. inStream.close();
  232. } catch (IOException e) {
  233. log.error("saveByteArrayToFile: out close IOException");
  234. }
  235. }
  236. return null;
  237. }
  238. public static String createUserDir(String userDir) {
  239. final String sysDir = System.getProperty("user.dir");
  240. String createdDir = sysDir + userDir;
  241. try {
  242. Path path = Paths.get(createdDir);
  243. //java.nio.file.Files;
  244. Files.createDirectories(path);
  245. }
  246. catch (IOException e) {
  247. log.error("createUserDir: IOException");
  248. }
  249. return createdDir;
  250. }
  251. public static synchronized void saveImageFile(BufferedImage image, String extension, String fullPath)
  252. {
  253. try (
  254. OutputStream out2 = new FileOutputStream(fullPath);
  255. ) {
  256. ImageIO.write(image, extension, out2);
  257. out2.close();
  258. //RenderedImage rendImage = image;
  259. //ImageIO.write(image, "bmp", new File(fullPath));
  260. }
  261. catch (IOException e) {
  262. log.error("saveImageFile: IOException");
  263. }
  264. }
  265. /*
  266. public static byte[] bitmapToByteArray( Bitmap bitmap ) {
  267. ByteArrayOutputStream stream = new ByteArrayOutputStream() ;
  268. bitmap.compress( Bitmap.CompressFormat.PNG, 100, stream) ;
  269. byte[] byteArray = stream.toByteArray() ;
  270. return byteArray ;
  271. }
  272. public static Bitmap byteArrayToBitmap(byte[] bytearr) {
  273. return BitmapFactory.decodeByteArray(bytearr, 0, bytearr.length);
  274. }*/
  275. /**
  276. * Bitmap bitmap;
  277. *
  278. * ByteBuffer buffer= ByteBuffer.Allocate(bitmap.ByteCount);
  279. * bitmap.copyPixelsToBuffer(buffer);
  280. * byte[] byteArray = buffer.ToArray<byte>();
  281. *
  282. * byte[] byteArray;
  283. * Bitmap bitmap;
  284. *
  285. * int width = 112;
  286. * int height = 112;
  287. *
  288. * bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  289. * ByteBuffer buffer = ByteBuffer.wrap(byteArray);
  290. * buffer.rewind();
  291. * bitmap.copyPixelsFromBuffer(buffer);
  292. */
  293. public static List<String> split(String value, String delim) {
  294. List<String> list = new ArrayList<String>();
  295. StringTokenizer stringTokenizer = new StringTokenizer(value, delim);
  296. while (stringTokenizer.hasMoreTokens()) {
  297. list.add(stringTokenizer.nextToken().trim());
  298. }
  299. return list;
  300. }
  301. public static int swapEndian(int x) {
  302. return swapEndian((short)x) << 16 | swapEndian((short)(x >> 16)) & 0xFFFF;
  303. }
  304. public static short swapEndian(short x) {
  305. return (short)(x << 8 | x >> 8 & 0xFF);
  306. }
  307. public static String getHttpServletRemoteIP(HttpServletRequest request) {
  308. if (request == null) {
  309. return "";
  310. }
  311. String ipAddress = request.getHeader("X-FORWARDED-FOR");
  312. // proxy 환경일 경우
  313. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  314. ipAddress = request.getHeader("Proxy-Client-IP");
  315. }
  316. // 웹로직 서버일 경우
  317. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  318. ipAddress = request.getHeader("WL-Proxy-Client-IP");
  319. }
  320. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  321. ipAddress = request.getHeader("HTTP_CLIENT_IP");
  322. }
  323. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  324. ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
  325. }
  326. // 기타
  327. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
  328. ipAddress = request.getRemoteAddr() ;
  329. }
  330. //-Djava.net.preferIPv4Stack=true
  331. if (ipAddress.equals("0:0:0:0:0:0:0:1")) //==> ipv6 <== default
  332. {
  333. ipAddress = "127.0.0.1"; //==> localhost
  334. }
  335. return ipAddress;
  336. }
  337. }