StatisticsTime.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package com.its.app.utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. public class StatisticsTime {
  7. private final int processingTime_BEFORE = 0;
  8. //private final int processingTime_CURRENT = 1;
  9. /*
  10. * 모든 시각은 'YYYYMMDDHH24MISS' 형태로 사용한다.
  11. */
  12. private String prcsDayWeek; /* 요일 유형 */
  13. private String prcsDayQuater; /* 분기 */
  14. private String prcsCurrTime; /* 20170718153504 - 현재시각 */
  15. private String prcsCurrFiveMin; /* 20170718153500 - 현재 정주기 5분 시각 */
  16. private String prcsCurrFiveMinTo; /* 20170718153459 - 현재 정주기 5분 시각 - 1초 */
  17. private String prcsPrevFiveMin; /* 20170718153000 - 이전 정주기 5분 시각 */
  18. private String stat15MinTime;
  19. private String stat15MinFrom;
  20. private String stat15MinTo;
  21. private String statHourTime;
  22. private String statHourFrom;
  23. private String statHourTo;
  24. private String statDayFrom;
  25. private String statDayTo;
  26. private String statMonFrom;
  27. private String statMonTo;
  28. private String statYearFrom;
  29. private String statYearTo;
  30. private boolean isStat15Min = false;
  31. private boolean isStatHour = false;
  32. private boolean isStatDay = false;
  33. private boolean isStatMon = false;
  34. private boolean isStatYear = false;
  35. private boolean isPatternUpdate = false;
  36. private int analysisTime = processingTime_BEFORE; /* 이전주기시각(0)/현재주기시각(1) */
  37. private int month, hour, min;
  38. private boolean isProcessing = false;
  39. private Date startDate;
  40. private Date endDate;
  41. public StatisticsTime() {
  42. startDate = new Date();
  43. endDate = new Date();
  44. }
  45. public void setProcessing(boolean isProcessing) {
  46. this.isProcessing = isProcessing;
  47. if (this.isProcessing) {
  48. startDate = new Date();
  49. }
  50. else {
  51. endDate = new Date();
  52. }
  53. }
  54. public boolean IsProcessing() { return this.isProcessing; }
  55. public Date getStartDate() { return this.startDate; }
  56. public Date getEndDate() { return this.endDate; }
  57. public int getAnalysisTime() { return this.analysisTime; }
  58. public void setAnalysisTime(int analysisTime) { this.analysisTime = analysisTime; }
  59. public boolean isStat15Min() { return this.isStat15Min; }
  60. public boolean isStatHour() { return this.isStatHour; }
  61. public boolean isStatDay() { return this.isStatDay; }
  62. public boolean isStatMon() { return this.isStatMon; }
  63. public boolean isStatYear() { return this.isStatYear; }
  64. public boolean isPatternUpdate() { return this.isPatternUpdate; }
  65. public String getStat15MinTime() {
  66. if (this.analysisTime == processingTime_BEFORE)
  67. return this.stat15MinFrom;
  68. return this.stat15MinTime;
  69. }
  70. public String getStat15MinFrom() { return this.stat15MinFrom; }
  71. public String getStat15MinTo() { return this.stat15MinTo; }
  72. public String getStatHourTime() {
  73. if (this.analysisTime == processingTime_BEFORE)
  74. return this.statHourFrom;
  75. return this.statHourTime;
  76. }
  77. public String getStatHourFrom() { return this.statHourFrom; }
  78. public String getStatHourTo() { return this.statHourTo; }
  79. public String getStatDayFrom() { return this.statDayFrom; }
  80. public String getStatDayTo() { return this.statDayTo; }
  81. public String getStatMonFrom() { return this.statMonFrom; }
  82. public String getStatMonTo() { return this.statMonTo; }
  83. public String getStatYearFrom() { return this.statYearFrom; }
  84. public String getStatYearTo() { return this.statYearTo; }
  85. public String getPrcsDayWeek() { return this.prcsDayWeek; }
  86. public String getPrcsDayQuater() { return this.prcsDayQuater; }
  87. public String getCurrTime() { return this.prcsCurrTime; }
  88. public String getPrcsFiveMin() {
  89. /*
  90. * 정주기 5분 가공시각으로 DB에 저장하는 시각, 현재시각 이전정주기 5분 시각을 사용한다.
  91. * 만일, 현재정주기 5분 시각을 사용하려 한다면 prcsCurrFiveMin 를 사용한다.
  92. */
  93. if (this.analysisTime == processingTime_BEFORE)
  94. return this.prcsPrevFiveMin;
  95. return this.prcsCurrFiveMin;
  96. }
  97. public String getCurrFiveMin() { return this.prcsCurrFiveMin; }
  98. public String getPrcsFiveMinFrom() { return this.prcsPrevFiveMin; }
  99. public String getPrcsFiveMinTo() { return this.prcsCurrFiveMinTo; }
  100. public String calDate(String paramTm, int addSec/*초단위*/) {
  101. String result = paramTm;
  102. SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  103. try {
  104. Date to = new Date();
  105. addSec = addSec * 1000; /* mili-second로 변환 */
  106. to.setTime(transFormat.parse(paramTm).getTime() + addSec);
  107. result = TimeUtils.dateToString(to, "yyyyMMddHHmmss");
  108. } catch (ParseException e) {
  109. e.printStackTrace();
  110. }
  111. return result;
  112. }
  113. public String calDateFormat(String paramFmt, String paramTm, int addSec) {
  114. String result = paramTm;
  115. SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  116. try {
  117. Date to = new Date();
  118. addSec = addSec * 1000; /* mili-second로 변환 */
  119. to.setTime(transFormat.parse(paramTm).getTime() + addSec);
  120. result = TimeUtils.dateToString(to, paramFmt);
  121. } catch (ParseException e) {
  122. e.printStackTrace();
  123. }
  124. return result;
  125. }
  126. public String calLastDayFormat(String paramFmt, String paramTm) {
  127. String result = paramTm;
  128. SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  129. try {
  130. Date to = new Date();
  131. to.setTime(transFormat.parse(paramTm).getTime());
  132. Calendar cal = Calendar.getInstance();
  133. cal.setTime(to);
  134. int day = cal.getActualMaximum(Calendar.DATE);
  135. cal.set(Calendar.DAY_OF_MONTH, day);
  136. return new SimpleDateFormat(paramFmt).format(cal.getTime());
  137. } catch (ParseException e) {
  138. e.printStackTrace();
  139. }
  140. return result;
  141. }
  142. public void setPrcsTimeInfo() {
  143. Calendar cal = Calendar.getInstance();
  144. try {
  145. cal.setTime(TimeUtils.stringToDate(this.prcsCurrTime));
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. /* 년월일시분초는 현재시각 기준으로 계산 */
  150. //this.year = cal.get(Calendar.YEAR);
  151. this.month = cal.get(Calendar.MONTH) + 1;
  152. //this.day = cal.get(Calendar.DAY_OF_MONTH);
  153. this.hour = cal.get(Calendar.HOUR_OF_DAY);
  154. this.min = cal.get(Calendar.MINUTE);
  155. //this.sec = cal.get(Calendar.SECOND);
  156. /* 요일, 사사분기는 가공시각을 기준으로 계산 */
  157. try {
  158. cal.setTime(TimeUtils.stringToDate(getPrcsFiveMin()));
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. }
  162. //DAY_OF_WEEK 리턴값이 일요일(1), 월요일(2), 화요일(3) ~~ 토요일(7)을 반환합니다.
  163. this.prcsDayWeek = String.valueOf(cal.get(Calendar.DAY_OF_WEEK));
  164. this.prcsDayQuater = String.valueOf((this.month/3)+1);
  165. }
  166. public void initValue() {
  167. this.prcsDayWeek = ""; /* 요일 유형 */
  168. this.prcsDayQuater = ""; /* 분기 */
  169. this.prcsCurrTime = ""; /* 20170718153504 - 현재시각 */
  170. this.prcsCurrFiveMin = ""; /* 20170718153500 - 현재 정주기 5분 시각 */
  171. this.prcsCurrFiveMinTo = ""; /* 20170718153459 - 현재 정주기 5분 시각 - 1초 */
  172. this.prcsPrevFiveMin = ""; /* 20170718153000 - 이전 정주기 5분 시각 */
  173. this.stat15MinTime = "";
  174. this.stat15MinFrom = "";
  175. this.stat15MinTo = "";
  176. this.statHourTime = "";
  177. this.statHourFrom = "";
  178. this.statHourTo = "";
  179. this.statDayFrom = "";
  180. this.statDayTo = "";
  181. this.statMonFrom = "";
  182. this.statMonTo = "";
  183. this.isStat15Min = false;
  184. this.isStatHour = false;
  185. this.isStatDay = false;
  186. this.isStatMon = false;
  187. this.isStatYear = false;
  188. this.isPatternUpdate = false;
  189. }
  190. public void init() {
  191. initValue();
  192. /*
  193. * 시간대 계산은 아직 안함(추후 시간대 계산이 필요하면 여기서 하면 됨)
  194. */
  195. this.prcsCurrTime = TimeUtils.getCurrentTimeString();
  196. Date dt = null;
  197. try {
  198. dt = TimeUtils.stringToDate(this.prcsCurrTime);
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. }
  202. this.prcsCurrFiveMin = TimeUtils.getFiveMinString(dt);
  203. this.prcsCurrFiveMinTo = calDate(this.prcsCurrFiveMin, -1); /* 현재 정주기 시각에서 -1초 */
  204. this.prcsPrevFiveMin = calDate(this.prcsCurrFiveMin, -60*5); /* 20170718153000 - 이전 정주기 5분 시각 */
  205. setPrcsTimeInfo();
  206. /*
  207. * 5분 정주기 가공이 끝나고 15분 주기일 경우(00, 15, 30, 45 분 일경우 15분 통계 정보를 생성한다)
  208. */
  209. if (this.min % 15 == 0) {
  210. this.isStat15Min = true;
  211. }
  212. this.stat15MinFrom = calDate(this.prcsCurrFiveMin, -60*15);
  213. this.stat15MinTo = calDate(this.stat15MinFrom, (60*15)-1);
  214. this.stat15MinTime = calDate(this.stat15MinFrom, (60*15));
  215. /*
  216. * 매시 5분 가공이 끝나면 1시간 통계 정보를 생성한다.
  217. */
  218. if (this.min == 5) {
  219. this.isStatHour = true;
  220. }
  221. String statHour = calDateFormat("yyyyMMddHH", this.prcsCurrFiveMin, -60*60);
  222. this.statHourFrom = statHour + "0000";
  223. this.statHourTo = statHour + "5959";
  224. this.statHourTime = calDateFormat("yyyyMMddHH", this.prcsCurrFiveMin, 0) + "0000";
  225. /*
  226. * 00시 10분 가공이 끝나면 이전일의 통계 정보를 가공.
  227. */
  228. if (this.hour == 0 && this.min == 10) {
  229. this.isStatDay = true;
  230. }
  231. String statDay = calDateFormat("yyyyMMdd", this.prcsCurrFiveMin, -60*60*24);
  232. this.statDayFrom = statDay + "000000";
  233. this.statDayTo = statDay + "235959";
  234. /*
  235. * 02시 10분 가공이 끝나면 이전일의 월통계 정보를 누적 가공.
  236. */
  237. if (this.hour == 2 && this.min == 10) {
  238. this.isStatMon = true;
  239. }
  240. this.statMonFrom = calDateFormat("yyyyMM", this.prcsCurrFiveMin, -60*60*24) + "01000000";
  241. this.statMonTo = calLastDayFormat("yyyyMMdd", this.statMonFrom) + "235959";
  242. /*
  243. * 02시 35분 가공이 끝나면 이전일의 연통계 정보를 누적 가공.
  244. */
  245. if (this.hour == 2 && this.min == 35) {
  246. this.isStatYear = true;
  247. }
  248. this.statYearFrom = calDateFormat("yyyy", this.prcsCurrFiveMin, -60*60*24) + "0101000000";
  249. this.statYearTo = calLastDayFormat("yyyy", this.statMonFrom) + "1231235959";
  250. /*
  251. * 패턴업데이트 시각 체크: 매일 새벽 3시 20분
  252. */
  253. if (this.hour == 3 && this.min == 20) {
  254. this.isPatternUpdate = true;
  255. }
  256. }
  257. }