PCMSUtil.java 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /**
  2. * @(#)file PCMSUtil.java
  3. * @(#)author
  4. * @(#)version 1.0
  5. * @(#)date 2003-05-23
  6. * @(#)since JDK1.3.1
  7. **
  8. * Copyright (c) 2002-2003 www.hwenc.com, Inc.
  9. * All rights reserved.
  10. * This software is the proprietary information of hwenc, Inc.
  11. */
  12. package kr.co.udapsoft.common.util;
  13. import java.math.BigDecimal;
  14. import java.text.DecimalFormat;
  15. import java.text.NumberFormat;
  16. import java.util.StringTokenizer;
  17. import java.util.Vector;
  18. import kr.co.hsnc.common.base.WAFLogger;
  19. import kr.co.hsnc.common.util.PatternUtil;
  20. import kr.co.hsnc.common.util.StringUtil;
  21. import kr.co.hsnc.common.util.ValueObject;
  22. import com.udapsoft.waf.common.util.DateUtil;
  23. /**
  24. * IPS에서 사용되는 유틸리티 메소드를 모아둔 클래스다.
  25. * Formatter클래스는 DateUtil.java의 facade 클래스이기도 하다.
  26. *
  27. * @author hanajava
  28. */
  29. public class PCMSUtil {
  30. //2004-07-01 추가
  31. private static final int DAY_OF_MONTH = 12;
  32. /** 기본 기간 설정
  33. * @return
  34. */
  35. public static ValueObject getDefaultPeriod() {
  36. return DateUtil.getDefaultPeriod();
  37. }
  38. /**
  39. * 현설일시 얻기(일자)
  40. * by hanajava
  41. * @param str
  42. * @return
  43. */
  44. public static String formatExplDate1(String str) {
  45. try {
  46. str = replace(str, "-", "");
  47. StringBuffer date = new StringBuffer(str.substring(0, 8));
  48. return date.insert(4, "-").insert(7, "-").toString();
  49. } catch (Exception e) {
  50. return "";
  51. }
  52. }
  53. /**
  54. * 현설일지 얻기(시간)
  55. * by hanajava
  56. * @param str
  57. * @return
  58. */
  59. public static String formatExplDate2(String str) {
  60. try {
  61. str = replace(str, "-", "");
  62. str = replace(str, ":", "");
  63. StringBuffer date = new StringBuffer(str.substring(8));
  64. return date.insert(2, ":").toString();
  65. } catch (Exception e) {
  66. return "";
  67. }
  68. }
  69. /**
  70. * 문자열중 대상문자열를 특정문자열로 변경한다.
  71. * ex) replace("hanajava", "hana", "java") -> "javajava"
  72. * @param line
  73. * @param oldStr 대상문자열
  74. * @param newStr 특정문자열
  75. * @return
  76. */
  77. /**
  78. public static String replace(String line, String oldStr, String newStr){
  79. /*if(line == null || line.length() == 0){
  80. return "";
  81. }
  82. int index = 0;
  83. while((index = line.indexOf(oldStr, index)) >= 0){
  84. line = line.substring(0, index) + newStr + line.substring(index+oldStr.length());
  85. index += newStr.length();
  86. }
  87. return line;
  88. return org.apache.commons.lang.StringUtils.replace (line, oldStr, newStr);
  89. }
  90. */
  91. /**
  92. * 문자열중 대상문자열를 특정문자열로 변경한다.
  93. * ex) replace("hanajava", "hana", "java") -> "javajava"
  94. * @param line
  95. * @param oldStr 대상문자열
  96. * @param newStr 특정문자열
  97. * @return
  98. */
  99. public static String replace(String line, String oldStr, String newStr) {
  100. if (line == null || line.length() == 0) {
  101. return "";
  102. }
  103. int index = 0;
  104. while ((index = line.indexOf(oldStr, index)) >= 0) {
  105. line = line.substring(0, index) + newStr
  106. + line.substring(index + oldStr.length());
  107. index += newStr.length();
  108. }
  109. return line;
  110. //return org.apache.commons.lang.StringUtils.replace (line, oldStr, newStr);
  111. }
  112. /**
  113. * html 에 스페이스( ) 추가
  114. * @param cnt
  115. * @return
  116. */
  117. public static String addSpace(String cnt) {
  118. return addSpace(Integer.parseInt(cnt));
  119. }
  120. /**
  121. * html 에 스페이스( ) 추가
  122. * @param cnt
  123. * @return
  124. */
  125. public static String addSpace(int cnt) {
  126. StringBuffer strBuf = new StringBuffer();
  127. if (cnt < 1)
  128. return "";
  129. for (int i = 0; i < cnt; i++)
  130. strBuf.append(" ");
  131. return strBuf.toString();
  132. }
  133. /**
  134. * 숫자를 한글로 변경한다.
  135. * 12345678 -> 일천이백삼십사만오천유백칠십팔
  136. * @param number
  137. * @return
  138. */
  139. public static String toHangle(long number) {
  140. String rtnStr = "";
  141. String numStr = Long.toString(number);
  142. String[] bigUnitArray = { "", "만", "억", "조" };
  143. String[] unitArray = { "", "십", "백", "천" };
  144. String[] hanArray = { "", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
  145. Vector splitedNum = new Vector();
  146. int pos = numStr.length();
  147. int index = 0;
  148. while (true) {
  149. if (pos - 4 > 0) {
  150. splitedNum.add(numStr.substring(pos - 4, pos));
  151. } else {
  152. splitedNum.add(numStr.substring(0, pos));
  153. break;
  154. }
  155. pos = pos - 4;
  156. index++;
  157. }
  158. int tmpLen = 0;
  159. int splietedNumSize = splitedNum.size();
  160. String tmp = "";
  161. for (int i = splietedNumSize - 1; i >= 0; i--) {
  162. tmp = (String) splitedNum.get(i);
  163. tmpLen = ((String) splitedNum.get(i)).length();
  164. for (int j = tmpLen; j > 0; j--) {
  165. String s = tmp.substring((tmpLen - j), (tmpLen - j) + 1);
  166. if (s.equals("0"))
  167. continue;
  168. /* by jetdoc */
  169. /*
  170. if( s.equals("1") && j != 1 && !(i == splietedNumSize-1 && j == tmpLen) ) {
  171. rtnStr += unitArray[j-1];
  172. }
  173. else {
  174. rtnStr += hanArray[Integer.parseInt(s)] + unitArray[j-1];
  175. }
  176. */
  177. if (s.equals("-")) {
  178. rtnStr += s;
  179. } else {
  180. rtnStr += hanArray[Integer.parseInt(s)] + unitArray[j - 1];
  181. }
  182. }
  183. if (!tmp.equals("-") && Integer.parseInt(tmp) != 0)
  184. rtnStr += bigUnitArray[i];
  185. }
  186. return rtnStr;
  187. }
  188. /**
  189. * 소수점을 적용하여 숫자를 한글로 변경한다. -2010.12.07 한지훈
  190. * 12345678.12 -> 일천이백삼십사만오천유백칠십팔.일이
  191. * @param number
  192. * @return
  193. */
  194. public static String toHangule_decimal(String amt) {
  195. String rtnStr = "";
  196. String rtnStr_decimal = "";
  197. String numStr = StringUtil.replace(amt,",","");
  198. String numStr_decimal = StringUtil.replace(amt,",","");
  199. String[] bigUnitArray = { "", "만", "억", "조" };
  200. String[] unitArray = { "", "십", "백", "천" };
  201. String[] hanArray = { "", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
  202. String[] hanArray_decimal = { "영", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구" };
  203. Vector splitedNum = new Vector();
  204. int decimal_pos = numStr.indexOf(".");
  205. if (decimal_pos == -1) {
  206. numStr = numStr.substring(0, numStr.length());
  207. }else {
  208. numStr = numStr.substring(0, decimal_pos);
  209. numStr_decimal = numStr_decimal.substring(decimal_pos+1, numStr_decimal.length());
  210. }
  211. int pos = numStr.length();
  212. int pos_decimal = numStr_decimal.length();
  213. int index = 0;
  214. while (true) {
  215. if (pos - 4 > 0) {
  216. splitedNum.add(numStr.substring(pos - 4, pos));
  217. } else {
  218. splitedNum.add(numStr.substring(0, pos));
  219. break;
  220. }
  221. pos = pos - 4;
  222. index++;
  223. }
  224. int tmpLen = 0;
  225. int splietedNumSize = splitedNum.size();
  226. String tmp = "";
  227. for (int i = splietedNumSize - 1; i >= 0; i--) {
  228. tmp = (String) splitedNum.get(i);
  229. tmpLen = ((String) splitedNum.get(i)).length();
  230. for (int j = tmpLen; j > 0; j--) {
  231. String s = tmp.substring((tmpLen - j), (tmpLen - j) + 1);
  232. if (s.equals("0"))
  233. continue;
  234. if (s.equals("-")) {
  235. rtnStr += s;
  236. } else {
  237. rtnStr += hanArray[Integer.parseInt(s)] + unitArray[j - 1];
  238. }
  239. }
  240. if (!tmp.equals("-") && Integer.parseInt(tmp) != 0)
  241. rtnStr += bigUnitArray[i];
  242. }
  243. if (decimal_pos != -1) {
  244. rtnStr_decimal = ".";
  245. for (int j = pos_decimal; j > 0; j--) {
  246. String m = numStr_decimal.substring((pos_decimal - j), (pos_decimal - j) + 1);
  247. rtnStr_decimal += hanArray_decimal[Integer.parseInt(m)];
  248. }
  249. }
  250. return rtnStr+rtnStr_decimal;
  251. }
  252. /** 1.1.1 ---> 01.01.01 로 변환
  253. * by jetdoc
  254. * @param str
  255. * @param len
  256. * @return
  257. */
  258. public static String lpad4Dot(String str, int len) {
  259. String[] strList = null;
  260. StringBuffer strBuf = new StringBuffer();
  261. strList = PatternUtil.split(str, ".");
  262. for (int i = 0; i < strList.length; i++) {
  263. strBuf.append(StringUtil.lpad(strList[i], len));
  264. if (i != strList.length - 1)
  265. strBuf.append(".");
  266. }
  267. return strBuf.toString();
  268. }
  269. public static float doRound(float num) {
  270. return doRound(num, 3);
  271. }
  272. public static double doRound(double num) {
  273. return doRound(num, 3);
  274. }
  275. /** 반올림 함수 ( JDK 1.4부터 지원 : JDK1.3을 위해 구현 )
  276. * by jetdoc
  277. * @param num
  278. * @param pos
  279. * @return
  280. */
  281. public static float doRound(float num, int pos) {
  282. double posV;
  283. if (pos == 0) {
  284. posV = 1;
  285. } else {
  286. posV = Math.pow(10, (double) pos);
  287. }
  288. return (float) (Math.round(num * posV) / posV);
  289. }
  290. /** 반올림 함수 ( JDK 1.4부터 지원 : JDK1.3을 위해 구현 )
  291. * by jetdoc
  292. * @param num
  293. * @param pos
  294. * @return
  295. */
  296. public static double doRound(double num, int pos) {
  297. double posV;
  298. if (pos == 0) {
  299. posV = 1;
  300. } else {
  301. posV = Math.pow(10, (double) pos);
  302. }
  303. return (Math.round(num * posV) / posV);
  304. }
  305. /**
  306. * 금액 0원일때 "" 리턴
  307. * @param cnt
  308. * @return
  309. */
  310. /*
  311. public static String zeroSpace(String str){
  312. if(str.equals("0")) str = "";
  313. return str;
  314. }
  315. */
  316. /******추가***************SSS*************************************************/
  317. /**
  318. * 이전달 구하기
  319. *
  320. * @param String
  321. * @return String
  322. * @exception
  323. */
  324. public static String getBeforeMonth(String yyyymm) {
  325. if (yyyymm.equals(""))
  326. return "";
  327. String pre_month = "";
  328. String s_month = "";
  329. String s_year = yyyymm.substring(0, 4);
  330. int i_month = Integer.parseInt(yyyymm.substring(4, 6));
  331. i_month--;
  332. if (i_month == 0) {
  333. i_month = 12;
  334. s_year = Integer.parseInt(s_year) - 1 + "";
  335. }
  336. if (i_month < 10)
  337. s_month = "0" + i_month;
  338. else
  339. s_month = i_month + "";
  340. pre_month = s_year + s_month;
  341. return pre_month;
  342. }
  343. /**
  344. *@param yyyymmdd
  345. *@return
  346. *기 능 : 하루 전 일자 가져오기
  347. *@author seafrog(pck)
  348. *@version 2006. 4. 23
  349. *@since JDK1.3
  350. *----------------------------------------------------------
  351. */
  352. public static String getBeforeDay(String yyyymmdd) {
  353. java.text.SimpleDateFormat formatt = new java.text.SimpleDateFormat(
  354. "yyyyMMdd");
  355. java.util.Calendar calenda = java.util.Calendar.getInstance();
  356. java.util.Date date = null;
  357. try {
  358. date = formatt.parse(yyyymmdd);
  359. calenda.setTime(date);
  360. calenda.add(java.util.Calendar.DAY_OF_YEAR, -1);
  361. yyyymmdd = formatt.format(calenda.getTime());
  362. } catch (Exception ex) {
  363. }
  364. return yyyymmdd;
  365. }
  366. /**
  367. * 다음달 구하기
  368. *
  369. * @param year : 년도 month : 달
  370. * @return String
  371. * @exception
  372. */
  373. public static String nextMonthofYear(String yyyymm) {
  374. String nextyear = "";
  375. String nextmonth = "";
  376. String nextyearmonth = "";
  377. if (yyyymm.length() == 6) {
  378. int year = Integer.parseInt(yyyymm.substring(0, 4));
  379. int month = Integer.parseInt(yyyymm.substring(4));
  380. if (month == 12) {
  381. nextmonth = "01";
  382. nextyear = String.valueOf(year + 1);
  383. } else {
  384. if ((month + 1) < 10) {
  385. nextmonth = "0" + (month + 1);
  386. } else {
  387. nextmonth = String.valueOf(month + 1);
  388. }
  389. nextyear = String.valueOf(year);
  390. }
  391. nextyearmonth = nextyear + nextmonth;
  392. return nextyearmonth;
  393. } else {
  394. return yyyymm;
  395. }
  396. }
  397. /**
  398. * xxx 구분자로 구분하여 토큰을 얻는다.
  399. * @param str 문자열
  400. * @param delim 구분자
  401. * @return 토큰
  402. */
  403. public static String[] getTokens(String str, String delim) {
  404. StringTokenizer st = new StringTokenizer(str, delim);
  405. int size = st.countTokens();
  406. String[] tokens = new String[size];
  407. for (int i = 0; st.hasMoreTokens(); i++) {
  408. tokens[i] = st.nextToken();
  409. }
  410. return tokens;
  411. }
  412. /******추가***************EEE*************************************************/
  413. /**
  414. * 금액 표시
  415. *
  416. * @param num 금액
  417. * @return String
  418. * @exception
  419. */
  420. public static String convType(double num) {
  421. return NumberFormat.getInstance().format(num);
  422. }
  423. public static String convType(long num) {
  424. return NumberFormat.getInstance().format(num);
  425. }
  426. public static String convType(int num) {
  427. return NumberFormat.getInstance().format(num);
  428. }
  429. public static String convType(String str) {
  430. return NumberFormat.getInstance().format(Double.parseDouble(str));
  431. }
  432. //double형 금액, 자리수 올림
  433. public static String convType(double num, int pos) {
  434. String str = "";
  435. num = doRound(num, pos);
  436. str = num + "";
  437. //WAFLogger.debug("str == [" + str + "]");
  438. //정수자리에 금액을 표시하는 ,를 추가한다.
  439. str = convType(str);
  440. str = strPoint(str, pos); //자리수조정..예: pos가 2인겨우 0.2 ===> 0.20으로
  441. //str = convType( str );
  442. return str;
  443. //return NumberFormat.getInstance().format(num);
  444. }
  445. /**
  446. * 0원인 경우 표시하지 않음
  447. * @param num
  448. * @param pos
  449. * @return
  450. */
  451. public static String convTypeNoZero(double num, int pos) {
  452. String str = "";
  453. num = doRound(num, pos);
  454. str = num + "";
  455. str = convType(str);
  456. str = strPoint(str, pos); //자리수조정..예: pos가 2인겨우 0.2 ===> 0.20으로
  457. if (num == 0) {
  458. str = "";
  459. }
  460. return str;
  461. }
  462. /****************************************************************
  463. --(2006-12-03 추가) 시작
  464. --강묵 2006-12-03 기본관리 평형/약정관리 데이터 입력시 오류 처리를 위한 류진환의 요청으로 추가
  465. ****************************************************************/
  466. public static String convType_house(double num, int pos) {
  467. String str = "";
  468. String pointRight = "";
  469. num = doRound(num, pos);
  470. str = num + "";
  471. if (str.indexOf(".") != -1) //소수점이 존재하는 경우
  472. {
  473. pointRight = str.substring(str.indexOf("."), str.length()); // 소수점을 포함해서 소수점 이하 부분을 저장한다.
  474. str = str.substring(0, str.indexOf(".")); // 소수점 이전 부분 즉 정수부분을 저장한다.
  475. str = convType(str) + pointRight; // 정수부분에 금액을 표시하는 ,를 추가하고 저장해놓은 소수점 이하부분을 더하여 저장한다.
  476. } else { // 소수점이 존재하지 않는 경우
  477. str = convType(str);
  478. }
  479. str = strPoint(str, pos); //자리수조정..예: pos가 2인겨우 0.2 ===> 0.20으로
  480. return str;
  481. }
  482. /****************************************************************
  483. --(2006-12-03 추가) 끝
  484. ****************************************************************/
  485. // 소수이하 절삭 함수
  486. public static String convert(double doubleObj, String pattern)
  487. throws Exception {
  488. DecimalFormat df = new DecimalFormat(pattern);
  489. return df.format(doubleObj).toString();
  490. }
  491. ///////////////////////////////////////////////////////
  492. //절삭(truncate) 메소드
  493. //@param data 입력 데이터. double 타입.
  494. //@param pos 절삭될 위치. int 타입
  495. //@return 절삭된 결과. double 타입.
  496. private static double[] lowUnits = { 1.0, 1.0E-1, 1.0E-2, 1.0E-3, 1.0E-4,
  497. 1.0E-5, 1.0E-6, 1.0E-7, 1.0E-8, 1.0E-9, 1.0E-10, 1.0E-11, 1.0E-12,
  498. 1.0E-13, 1.0E-14, 1.0E-15, 1.0E-16 };
  499. private static double[] highUnits = { 1.0, 1.0E1, 1.0E2, 1.0E3, 1.0E4,
  500. 1.0E5, 1.0E6, 1.0E7, 1.0E8, 1.0E9, 1.0E10, 1.0E11, 1.0E12, 1.0E13,
  501. 1.0E14, 1.0E15, 1.0E16 };
  502. public static double truncate(double data, int pos) {
  503. if (pos == 0)
  504. return Math.floor(data);
  505. else if (pos > 0 && pos <= highUnits.length)
  506. return lowUnits[pos] * Math.floor(data * highUnits[pos]);
  507. else if (pos < 0 && pos >= -lowUnits.length)
  508. return highUnits[-pos] * Math.floor(data * lowUnits[-pos]);
  509. else
  510. throw new RuntimeException(data
  511. + " cannot be truncated at the position " + pos);
  512. }
  513. ///////////////////////////////////////////////////////
  514. // 잘라내림(floor) 메소드
  515. // @param data 입력 데이터. double 타입.
  516. // @param pos 잘라내릴 위치. int 타입
  517. // @return 잘라내린 결과. double 타입.
  518. public static double floor(double data, int pos) {
  519. if (data >= 0.0) {
  520. if (pos == 0)
  521. return Math.floor(data);
  522. else if (pos > 0 && pos <= highUnits.length)
  523. return lowUnits[pos] * Math.floor(data * highUnits[pos]);
  524. else if (pos < 0 && pos >= -lowUnits.length)
  525. return highUnits[-pos] * Math.floor(data * lowUnits[-pos]);
  526. else
  527. throw new RuntimeException(data
  528. + " cannot be floored at the position " + pos);
  529. } else {
  530. if (pos == 0)
  531. return -Math.ceil(-data);
  532. else if (pos > 0 && pos <= highUnits.length)
  533. return lowUnits[pos] * -Math.ceil(-data * highUnits[pos]);
  534. else if (pos < 0 && pos >= -lowUnits.length)
  535. return highUnits[-pos] * -Math.ceil(-data * lowUnits[-pos]);
  536. else
  537. throw new RuntimeException(data
  538. + " cannot be truncated at the position " + pos);
  539. }
  540. }
  541. ///////////////////////////////////////////////////////
  542. // 잘라올림(ceil) 메소드
  543. // @param data 입력 데이터. double 타입.
  544. // @param pos 잘라올릴 위치. int 타입
  545. // @return 잘라올린 결과. double 타입.
  546. public static double ceil(double data, int pos) {
  547. if (data >= 0.0) {
  548. if (pos == 0)
  549. return Math.ceil(data);
  550. else if (pos > 0 && pos <= highUnits.length)
  551. return lowUnits[pos] * Math.ceil(data * highUnits[pos]);
  552. else if (pos < 0 && pos >= -lowUnits.length)
  553. return highUnits[-pos] * Math.ceil(data * lowUnits[-pos]);
  554. else
  555. throw new RuntimeException(data
  556. + " cannot be ceiled at the position " + pos);
  557. } else {
  558. if (pos == 0)
  559. return -Math.floor(-data);
  560. else if (pos > 0 && pos <= highUnits.length)
  561. return lowUnits[pos] * -Math.floor(-data * highUnits[pos]);
  562. else if (pos < 0 && pos >= -lowUnits.length)
  563. return highUnits[-pos] * -Math.floor(-data * lowUnits[-pos]);
  564. else
  565. throw new RuntimeException(data
  566. + " cannot be floored at the position " + pos);
  567. }
  568. }
  569. ///////////////////////////////////////////////////////
  570. // 반올림(round) 메소드
  571. // @param data 입력 데이터. double 타입.
  572. // @param pos 반올림할 위치. int 타입
  573. // @return 반올림된 결과. double 타입.
  574. public static double round(double data, int pos) {
  575. if (data >= 0.0) {
  576. if (pos == 0)
  577. return Math.floor(data + 0.5);
  578. else if (pos > 0 && pos <= highUnits.length)
  579. return lowUnits[pos] * Math.floor(data * highUnits[pos] + 0.5);
  580. else if (pos < 0 && pos >= -lowUnits.length)
  581. return highUnits[-pos]
  582. * Math.floor(data * lowUnits[-pos] + 0.5);
  583. else
  584. throw new RuntimeException(data
  585. + " cannot be floored at the position " + pos);
  586. } else {
  587. if (pos == 0)
  588. return -Math.floor(-data + 0.5);
  589. else if (pos > 0 && pos <= highUnits.length)
  590. return lowUnits[pos]
  591. * -Math.floor(-data * highUnits[pos] + 0.5);
  592. else if (pos < 0 && pos >= -lowUnits.length)
  593. return highUnits[-pos]
  594. * -Math.floor(-data * lowUnits[-pos] + 0.5);
  595. else
  596. throw new RuntimeException(data
  597. + " cannot be truncated at the position " + pos);
  598. }
  599. }
  600. public static String convertFloor(double doubleObj, int position)
  601. throws Exception {
  602. String temp = "";
  603. String tempTwoDigit = "";
  604. int inx = 0;
  605. String patternAttachedZero = "";
  606. String point = ".";
  607. if (position < 0)
  608. throw new Exception(" Position 을 0 이상으로 설정 하십시오 ");
  609. if (position == 0)
  610. point = ""; // 소수점이 포함되어 나타나는것 방지
  611. // doubleObj : 0, 0.xxx 인 경우 오류 수정 2010.08.27 김영수.
  612. temp = convert(doubleObj, "#0.0000000000000000000000000000000000");
  613. // BigDecimal 에서 표현하는 소수점 가장 끝자리까지 이므로
  614. //반올림되어 오류가 나는 소지를 최소화 한다.
  615. inx = temp.lastIndexOf(".");
  616. tempTwoDigit = temp.substring(0, ((inx + 1) + position));
  617. for (int i = 0; i < position; i++) {
  618. patternAttachedZero += "0";
  619. }
  620. return convert(Double.parseDouble(tempTwoDigit), "#,##0" + point
  621. + patternAttachedZero);
  622. }
  623. /**
  624. * @param doubleObj
  625. * @param position
  626. * @return
  627. * @throws Exception
  628. */
  629. public static String convertFloorNoZero(double doubleObj, int position) throws Exception {
  630. if (doubleObj == 0) {
  631. return "";
  632. } else {
  633. String temp = "";
  634. String tempTwoDigit = "";
  635. int inx = 0;
  636. String patternAttachedZero = "";
  637. String point = ".";
  638. if (position < 0)
  639. throw new Exception(" Position 을 0 이상으로 설정 하십시오 ");
  640. if (position == 0)
  641. point = ""; // 소수점이 포함되어 나타나는것 방지
  642. // doubleObj : 0, 0.xxx 인 경우 오류 수정 2010.08.27 김영수.
  643. temp = convert(doubleObj, "#0.0000000000000000000000000000000000");
  644. // BigDecimal 에서 표현하는 소수점 가장 끝자리까지 이므로
  645. //반올림되어 오류가 나는 소지를 최소화 한다.
  646. inx = temp.lastIndexOf(".");
  647. tempTwoDigit = temp.substring(0, ((inx + 1) + position));
  648. for (int i = 0; i < position; i++) {
  649. patternAttachedZero += "0";
  650. }
  651. return convert(Double.parseDouble(tempTwoDigit), "#,##0" + point + patternAttachedZero);
  652. }
  653. }
  654. /**
  655. * 해당문자의 소숫점(int point까지)을 마추어준다.
  656. * 2004-06-07 sukjin chang
  657. * @param String str, int point
  658. * @return String
  659. * @exception
  660. */
  661. public static String strPoint(String str, int point) {
  662. if (str == null)
  663. return "";
  664. String zero = "0000000000";
  665. if (str.equals(""))
  666. str = "0";
  667. if (str.indexOf(".") != -1) {
  668. int p = str.indexOf(".");
  669. str = str + zero;
  670. str = str.substring(0, str.indexOf(".") + 1 + point);
  671. } else {
  672. str = str + "." + zero;
  673. str = str.substring(0, str.indexOf(".") + 1 + point);
  674. }
  675. //2004-10-20 추가
  676. if (point == 0)
  677. str = str.substring(0, str.indexOf("."));
  678. return str;
  679. }
  680. /**
  681. * rate를 나타내는 format.
  682. *
  683. * @param pRate raw string
  684. * @return formatted string
  685. */
  686. public static String formatRate(String pRate, int pPost) {
  687. String vReturn = null;
  688. if (pRate == null)
  689. return "";
  690. if (pPost <= 0)
  691. return pRate;
  692. if (pRate.length() <= pPost)
  693. return pRate;
  694. int vIndex = pRate.length() - pPost;
  695. vReturn = convType(pRate.substring(0, vIndex));
  696. vReturn = vReturn + "." + pRate.substring(vIndex);
  697. return vReturn;
  698. }
  699. /**
  700. * 날짜 포맷 맞추기
  701. * 2004-06-02 sukjin chang
  702. * @param String str, String format
  703. * @return String
  704. * @exception
  705. */
  706. public static String dateFormat(String str, String format) {
  707. if (str == null)
  708. return "";
  709. str = str.trim();
  710. if (str.length() == 6) {
  711. return str.substring(0, 4) + format + str.substring(4);
  712. } else if (str.length() == 8) {
  713. return str.substring(0, 4) + format + str.substring(4, 6) + format
  714. + str.substring(6);
  715. } else {
  716. return str;
  717. }
  718. }
  719. /**
  720. * 시간 포맷 맞추기
  721. * 2006-01-12 Sunwoo Park
  722. * @param String str(HHMM)
  723. * @return String
  724. * @exception
  725. */
  726. public static String timeFormat(String str) {
  727. if (str == null)
  728. return "";
  729. str = str.trim();
  730. if (str != null && str.length() == 4) {
  731. return str.substring(0, 2) + ":" + str.substring(2, 4);
  732. } else {
  733. return "";
  734. }
  735. }
  736. /**
  737. * 금액에 -가 붙어있는 경우 ▼로 치환한다
  738. * ,자릿수 콤마가 없다면 추가한다.
  739. * @param str
  740. * @param color
  741. * @return
  742. */
  743. public static String setNSymbol(String str, String color) {
  744. int idx = str.indexOf("-");
  745. //맨 앞에 minus 기호가 붙어있는 경우 ▼ 로 치환한다.
  746. if (idx == 0) {
  747. if (str.indexOf(",") == -1) {
  748. str = convType(str);
  749. }
  750. //△ ▲ ▽ ▼
  751. str = str.replace('-', '▼');
  752. str = "<font color=" + color + ">" + str + "</font>";
  753. }
  754. return str;
  755. }
  756. /**
  757. * -금액의 경우 -제거후 색추가
  758. * 2004-06-02 sukjin chang
  759. * @param String str, String color
  760. * @return String
  761. * @exception
  762. */
  763. public static String strColor(String str, String color) {
  764. if (str == null)
  765. return "";
  766. if (str.indexOf("-") != -1) {
  767. str = "<font color=" + color + ">"
  768. + StringUtil.replace(str, "-", "△") + "</font>";
  769. }
  770. return str;
  771. }
  772. /**
  773. * -금액의 경우 -제거후 색추가
  774. * 2004-06-02 sukjin chang
  775. * @param String str, String color
  776. * @return String
  777. * @exception
  778. */
  779. public static String setColor(String str, String color) {
  780. if (str == null)
  781. return "";
  782. str = "<font color=" + color + ">" + str + "</font>";
  783. return str;
  784. }
  785. /**
  786. * -금액의 경우 -제거후 색추가
  787. * 2004-06-02 sukjin chang
  788. * @param String str, String color
  789. * @return String
  790. * @exception
  791. */
  792. public static String setColor(String str, String color, String gubun) {
  793. if (str == null)
  794. return "";
  795. if (gubun.equals("N"))
  796. str = "<font color=" + color + ">"
  797. + StringUtil.replace(str, "-", "") + "</font>";
  798. else
  799. str = "<font color=" + color + ">" + str + "</font>";
  800. return str;
  801. }
  802. /**
  803. * 두 날짜를 비교하여 일수를 리턴한다.
  804. * 2004-07-01 추가 sukjin chang
  805. * @param dbDate 시작일
  806. * @param inDate 끝일
  807. * @return int
  808. * @exception
  809. */
  810. public static int getDays(String dbDate, String inDate) {
  811. if ((dbDate == null || dbDate.equals("") || dbDate.length() != 8)
  812. && (inDate == null || inDate.equals("") || inDate.length() != 8)) {
  813. return 0;
  814. }
  815. if (Integer.parseInt(dbDate) > Integer.parseInt(inDate))
  816. return 0;
  817. int dbyyyy = Integer.parseInt(dbDate.substring(0, 4));
  818. int inyyyy = Integer.parseInt(inDate.substring(0, 4));
  819. int dbmm = Integer.parseInt(dbDate.substring(4, 6));
  820. int inmm = Integer.parseInt(inDate.substring(4, 6));
  821. int dbdd = Integer.parseInt(dbDate.substring(6));
  822. int indd = Integer.parseInt(inDate.substring(6));
  823. int days = 0; // 그 해의 총 일수
  824. if (inyyyy == dbyyyy) {
  825. if (inmm == dbmm) {
  826. days = indd - dbdd;
  827. } else {
  828. days += getMonOfDays(dbmm, dbyyyy) - dbdd; // 그달의 남은 일수 계산
  829. dbmm++;
  830. while (true) {
  831. if (inmm == dbmm)
  832. break;
  833. days += getMonOfDays(dbmm, dbyyyy); // 중간 차이가 나는 달의 일수 계산
  834. dbmm++;
  835. } // end of while
  836. days += indd; // 그달까지의 지나간 날짜 계산
  837. } // end of if
  838. } else {
  839. int n_dbmm = dbmm;
  840. for (int j = 0; j < (DAY_OF_MONTH - dbmm); j++) {
  841. days += getMonOfDays(++n_dbmm, dbyyyy); // 남아있는 개월수의 날짜 총합
  842. } // end of for
  843. days += getMonOfDays(dbmm, dbyyyy) - dbdd;
  844. dbyyyy++;
  845. while (true) {
  846. if (dbyyyy == inyyyy)
  847. break;
  848. for (int i = 1; i < 13; i++) {
  849. days += getMonOfDays(i, dbyyyy);
  850. } // end of for
  851. dbyyyy++;
  852. } // end of while
  853. for (int j = 1; j < inmm; j++) {
  854. days += getMonOfDays(j, inyyyy); // 남아있는 개월수의 날짜 총합
  855. } // end of for
  856. days += indd;
  857. } // end of else
  858. return days;
  859. } // end of function
  860. /**
  861. * 그해 그달의 일수 구하기
  862. * 2004-07-01 추가
  863. * @param month 달
  864. * @param year 년
  865. * @return int
  866. * @exception
  867. */
  868. public static int getMonOfDays(int month, int year) {
  869. switch (month) {
  870. case 1:
  871. case 3:
  872. case 5:
  873. case 7:
  874. case 8:
  875. case 10:
  876. case 12:
  877. return (31);
  878. case 4:
  879. case 6:
  880. case 9:
  881. case 11:
  882. return (30);
  883. default:
  884. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
  885. return (29); // 2월 윤년계산을 위해서
  886. else
  887. return (28);
  888. }
  889. }
  890. /**
  891. * 금액 ""원일때 0 리턴
  892. * @param cnt
  893. * @return
  894. */
  895. public static String space_Zero(String str) {
  896. if (str.equals(""))
  897. str = "0";
  898. return str;
  899. }
  900. /**
  901. * 금액 "0"원일때 "" 리턴
  902. * @param cnt
  903. * @return
  904. */
  905. public static String Zero_space(String str) {
  906. if (str.equals("0"))
  907. str = "";
  908. else if (str.equals("0.0"))
  909. str = "";
  910. else if (str.equals("0.00"))
  911. str = "";
  912. else if (str.equals("0.000"))
  913. str = "";
  914. return str;
  915. }
  916. public static String Zero_space(float flt) {
  917. if (flt == 0)
  918. return "";
  919. return String.valueOf(flt);
  920. }
  921. public static String Zero_space(long lg) {
  922. if (lg == 0)
  923. return "";
  924. return String.valueOf(lg);
  925. }
  926. public static String Zero_space(double db) {
  927. if (db == 0)
  928. return "";
  929. return String.valueOf(db);
  930. }
  931. /**
  932. * 문자열 제거
  933. * @param src
  934. * @param target
  935. * @return
  936. */
  937. /**
  938. public static String removeString (String src, String target) {
  939. return org.apache.commons.lang.StringUtils.remove (src, target);
  940. }
  941. public static boolean isNumeric(String src) {
  942. return org.apache.commons.lang.StringUtils.isNumeric (src);
  943. }
  944. public static double toDouble (String src) {
  945. return org.apache.commons.lang.math.NumberUtils.toDouble (src, 0);
  946. }
  947. public static float toFloat (String src) {
  948. return org.apache.commons.lang.math.NumberUtils.toFloat (src, 0);
  949. }
  950. public static long toLong (String src) {
  951. return org.apache.commons.lang.math.NumberUtils.toLong (src, 0);
  952. }
  953. public static int toInt (String src) {
  954. return org.apache.commons.lang.math.NumberUtils.toInt (src, 0);
  955. }
  956. public static boolean isEmptyOrBlank (String src) {
  957. return org.apache.commons.lang.StringUtils.isBlank (src) || org.apache.commons.lang.StringUtils.isEmpty (src) ;
  958. }
  959. */
  960. /**
  961. * javascript escape로 유니코드로 변환된 문자열을 디코딩한다.
  962. *
  963. * @author cmlee
  964. * @param uni javascript의 escape로 변환된 유니코드 문자열
  965. * @return String
  966. */
  967. public static String unescape(String uni) {
  968. StringBuffer str = new StringBuffer();
  969. for (int i = uni.indexOf("%u"); i > -1; i = uni.indexOf("%u")) {
  970. str.append(uni.substring(0, i));
  971. str.append(String.valueOf((char) Integer.parseInt(uni.substring(
  972. i + 2, i + 6), 16)));
  973. uni = uni.substring(i + 6);
  974. }
  975. str.append(uni);
  976. return str.toString();
  977. }
  978. public static String[] getTokenArray(String str, String strDelim) {
  979. if (str == null || str.length() == 0) {
  980. String[] temp = { "" };
  981. return temp;
  982. }
  983. StringTokenizer st = new StringTokenizer(str, strDelim);
  984. if (st.countTokens() == 0) {
  985. String[] arrToken = new String[1];
  986. arrToken[0] = str;
  987. return arrToken;
  988. }
  989. String[] arrToken = new String[st.countTokens()];
  990. for (int i = 0; i < arrToken.length; i++)
  991. arrToken[i] = st.nextToken().trim();
  992. return arrToken;
  993. }
  994. static char[] chars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
  995. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  996. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  997. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  998. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  999. public static String getRandomString(int size) {
  1000. if (size < 1) {
  1001. return "";
  1002. }
  1003. String returnStr = "";
  1004. for (int i = 0; i < size; i++) {
  1005. returnStr += getRandomChar();
  1006. }
  1007. return returnStr;
  1008. }
  1009. public static char getRandomChar() {
  1010. return chars[(int) (Math.random() * 62)];
  1011. }
  1012. //skw 추가 2008.08.26
  1013. public static boolean isEmptyOrBlank(String src) {
  1014. return org.apache.commons.lang.StringUtils.isBlank(src)
  1015. || org.apache.commons.lang.StringUtils.isEmpty(src);
  1016. }
  1017. //skw 추가 2008.08.26
  1018. public static int toInt(String src) {
  1019. return org.apache.commons.lang.math.NumberUtils.toInt(src, 0);
  1020. }
  1021. //skw 추가 2008.08.26
  1022. public static long toLong(String src) {
  1023. return org.apache.commons.lang.math.NumberUtils.toLong(src, 0);
  1024. }
  1025. //이형택 추가 2008/12/18
  1026. public static double toDouble(String src) {
  1027. return org.apache.commons.lang.math.NumberUtils.toDouble(src, 0);
  1028. }
  1029. // 이형택 추가 2008/12/18
  1030. public static float toFloat(String src) {
  1031. return org.apache.commons.lang.math.NumberUtils.toFloat(src, 0);
  1032. }
  1033. // 이형택 추가 2008/12/18
  1034. public static boolean isNumeric(String src) {
  1035. return org.apache.commons.lang.StringUtils.isNumeric (src);
  1036. }
  1037. //skw 추가 2008.10.02
  1038. public static String removeString (String src, String target) {
  1039. return org.apache.commons.lang.StringUtils.remove (src, target);
  1040. }
  1041. /**
  1042. * BigDecimal 형태로 변환하여 계산(곱하기)
  1043. * 2010-11-27 Giwan Choe
  1044. * @param Double src(곱할대상), Double src(곱할수)
  1045. * @return Double
  1046. * @exception
  1047. */
  1048. public static double multiply(double src1, double src2) {
  1049. double returnVal = 0;
  1050. BigDecimal dcSrc1 = new BigDecimal(src1 + "");
  1051. BigDecimal dcSrc2 = new BigDecimal(src2 + "");
  1052. returnVal = dcSrc1.multiply(dcSrc2).doubleValue();
  1053. return returnVal;
  1054. }
  1055. /**
  1056. * BigDecimal 형태로 변환하여 계산(나누기)
  1057. * 2010-11-27 Giwan Choe
  1058. * @param Double src(나눌대상), Double src(나눌수)
  1059. * @return Double
  1060. * @exception
  1061. */
  1062. public static double divide(double src1, double src2) {
  1063. double returnVal = 0;
  1064. BigDecimal dcSrc1 = new BigDecimal(src1 + "");
  1065. BigDecimal dcSrc2 = new BigDecimal(src2 + "");
  1066. returnVal = dcSrc1.divide(dcSrc2, 16, 6).doubleValue();
  1067. return returnVal;
  1068. }
  1069. /**
  1070. * BigDecimal 형태로 변환하여 계산(곱하기)
  1071. * 2010-11-27 Giwan Choe
  1072. * @param String src(곱할대상), String src(곱할수)
  1073. * @return Double
  1074. * @exception
  1075. */
  1076. public static double multiply(String src1, String src2) {
  1077. double returnVal = 0;
  1078. BigDecimal dcSrc1 = new BigDecimal(src1);
  1079. BigDecimal dcSrc2 = new BigDecimal(src2);
  1080. returnVal = dcSrc1.multiply(dcSrc2).doubleValue();
  1081. return returnVal;
  1082. }
  1083. /**
  1084. * BigDecimal 형태로 변환하여 계산(나누기)
  1085. * 2010-11-27 Giwan Choe
  1086. * @param String src(나눌대상), String src(나눌수)
  1087. * @return Double
  1088. * @exception
  1089. */
  1090. public static double divide(String src1, String src2) {
  1091. double returnVal = 0;
  1092. BigDecimal dcSrc1 = new BigDecimal(src1);
  1093. BigDecimal dcSrc2 = new BigDecimal(src2);
  1094. returnVal = dcSrc1.divide(dcSrc2, 16, 6).doubleValue();
  1095. return returnVal;
  1096. }
  1097. /**
  1098. * 마이너스, 소수점자리수가 있는 숫자형 문자열에 comma를 붙여주는 함수
  1099. * 2011-01-20 김영수
  1100. * @param amount 금액
  1101. * @return String 콤마가 붙인 숫자(-,소수점 자리수 포함)
  1102. * @throws Exception
  1103. */
  1104. public static String addComma(String amount) throws Exception {
  1105. String[] dotPos = amount.split("\\.");
  1106. String sign = "";
  1107. String dotU = "";
  1108. String dotD = "";
  1109. dotU = dotPos[0];
  1110. if(dotU.indexOf("-") == 0) {
  1111. sign = dotU.substring(0, 1);
  1112. dotU = dotU.substring(1);
  1113. }
  1114. if(!PCMSUtil.isNumeric(dotU)) {
  1115. throw new Exception("숫자가 아납니다.");
  1116. }
  1117. if(dotPos.length == 2) {
  1118. dotD = dotPos[1];
  1119. }
  1120. int length = dotU.length();
  1121. int commaFlag = length%3;
  1122. String temp = "";
  1123. if(commaFlag > 0) {
  1124. temp = dotU.substring(0, commaFlag);
  1125. if (length > 3) {
  1126. temp += ",";
  1127. }
  1128. } else {
  1129. temp = "";
  1130. }
  1131. for (int i=commaFlag; i < length; i+=3) {
  1132. temp += dotU.substring(i, i+3);
  1133. if( i < (length-3)) {
  1134. temp += ",";
  1135. }
  1136. }
  1137. if(!"".equals(dotD)) {
  1138. amount = temp + "." + dotD;
  1139. } else {
  1140. amount = temp;
  1141. }
  1142. if("-".equals(sign)) {
  1143. amount = sign + amount;
  1144. }
  1145. return amount;
  1146. }
  1147. /**
  1148. * [설명]
  1149. * 시간 형식 변형
  1150. *
  1151. * @param string strTime
  1152. * @return string
  1153. */
  1154. public static String setConvertTime(String strTime) {
  1155. if (strTime.length() == 6) {
  1156. return strTime.substring(0,2) + ":" + strTime.substring(2,4) + ":" + strTime.substring(4,6);
  1157. } else if (strTime.length() == 4) {
  1158. return strTime.substring(0,2) + ":" + strTime.substring(2,4);
  1159. } else {
  1160. return strTime;
  1161. }
  1162. }
  1163. /**
  1164. * [설명]
  1165. * 카드번호 형식 변형
  1166. *
  1167. * @param string strCardNum
  1168. * @return string
  1169. */
  1170. public static String setConvertCard(String strCardNum) {
  1171. if (strCardNum.length() == 16) {
  1172. return strCardNum.substring(0,4) + "-" + strCardNum.substring(4,8) + "-" + strCardNum.substring(8,12) + "-" + strCardNum.substring(12,16);
  1173. } else {
  1174. return strCardNum;
  1175. }
  1176. }
  1177. }