diff --git a/README.md b/README.md index 0e2f403..f143689 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ _예)_ ## 현재 지원되는 모듈 리스트 참조 Docs +- [유틸리티](https://github.com/ziponia/lib/blob/master/util/README.md) - [kakao api 모듈](https://github.com/ziponia/lib/blob/master/kakao/README.md) - [웹 검색](https://developers.kakao.com/docs/restapi/search#웹문서-검색) - [주소 검색](https://developers.kakao.com/docs/restapi/local#주소-검색) diff --git a/util/README.md b/util/README.md new file mode 100644 index 0000000..f40e9c3 --- /dev/null +++ b/util/README.md @@ -0,0 +1,53 @@ +# Utility Component + +## Getting Start + +```xml + + com.github.ziponia.lib + util + {library version} + +``` + +_Gradle_ + +```groovy +implementation 'com.github.ziponia.lib:util:{library version}' +``` + +## Browser + +class `DateUtilComponent` + +날짜 관련 컴포넌트 + +| method | param | return | description | +| :------------- | :------------------ | :------------- | :------------------------------------------- | +| toAfterMinute | java.util.Date, int | java.util.Date | 기준 이후의 분 시점을 반환합니다. | +| toBeforeMinute | java.util.Date, int | java.util.Date | 기준 이전의 분 시점을 반환합니다. | +| lastDate | java.util.Date | int | Date 객체 기준으로 마지막 날짜를 반환합니다. | +| lastDate | int, int | int | 년, 월 기준으로 마지막 날을 반환합니다. | + +class `VerifyUtilComponent` + +검증 관련 컴포넌트 + +| method | param | return | description | +| :-------------------------------- | :----- | :------ | :-------------------------------------------- | +| passwordThen8AndSpecialCharacters | String | boolean | 최소 8 자 이상, 숫자, 특수문자 1개이상씩 포함 | +| checkCellphone | String | boolean | 휴대폰번호 유효성 체크 | +| checkLicenseValidity | String | boolean | 운전 면허번호 유효성 체크 | + +class `UtilComponent` + +기본 유틸리티 컴포넌트 + +| method | param | return | description | +| :-------------------------------- | :----------------------------- | :----- | :--------------------------------------------------------------- | +| keyGenerator | int, boolean | String | 랜덤키를 만듭니다, 두번째 인자가 true 면, 숫자로만 만듭니다. | +| byteArrayToBinaryString | byte[] | String | 바이너리 바이트 배열을 스트링으로 변환 | +| byteToBinaryString | byte | String | 바이너리 바이트를 스트링으로 변환 | +| binaryStringToByteArray | String | byte[] | 바이너리 스트링을 바이트배열로 변환 | +| binaryStringToByte | String | byte | 바이너리 스트링을 바이트로 변환 | +| calculationDistanceFromGeographic | double, double, double, double | double | 위도, 경도를 사용하여 시작점과 끝점의 거리를 구합니다. (단위 km) | diff --git a/util/src/main/java/com/ziponia/util/DateUtilComponent.java b/util/src/main/java/com/ziponia/util/DateUtilComponent.java new file mode 100644 index 0000000..808da59 --- /dev/null +++ b/util/src/main/java/com/ziponia/util/DateUtilComponent.java @@ -0,0 +1,57 @@ +package com.ziponia.util; + +import java.util.Calendar; +import java.util.Date; + +public class DateUtilComponent { + + /** + * currentDate 기준 이후의 분 시점을 반환합니다. + * + * @param currentDate 변환 할 시간 + * @param afterMinute 변환 할 시점 + * @return java.util.Date + */ + public static Date toAfterMinute(Date currentDate, int afterMinute) { + int targetTime = 60 * afterMinute * 1000; // 초 + return new Date(currentDate.getTime() + targetTime); + } + + /** + * currentDate 기준 이전의 분 시점을 반환합니다. + * + * @param currentDate 변환 할 시간 + * @param beforeMinute 변환 할 시점 + * @return java.util.Date + */ + public static Date toBeforeMinute(Date currentDate, int beforeMinute) { + int targetTime = 60 * beforeMinute * 1000; // 초 + return new Date(currentDate.getTime() - targetTime); + } + + /** + * Date 객체 기준으로 마지막 날짜를 반환합니다. + * + * @param date 기준일이 될 Date + * @return 마지막 날짜. + */ + public static int lastDate(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + return cal.getActualMaximum(Calendar.DATE); + } + + /** + * 년, 월 기준으로 마지막 날을 반환합니다. + * + * @param year 기준 년 + * @param month 기준 월 (ex, 8월 -> month = 8, -1 아닙니다.) + * @return 마지막 날짜. + */ + public static int lastDate(int year, int month) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month - 1); + return cal.getActualMaximum(Calendar.DATE); + } +} diff --git a/util/src/main/java/com/ziponia/util/UtilComponent.java b/util/src/main/java/com/ziponia/util/UtilComponent.java index 70effe1..825f23f 100644 --- a/util/src/main/java/com/ziponia/util/UtilComponent.java +++ b/util/src/main/java/com/ziponia/util/UtilComponent.java @@ -3,10 +3,7 @@ import javax.crypto.Cipher; import java.io.*; import java.security.Key; -import java.sql.Timestamp; import java.util.UUID; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * 작성일 : 2018. 07. 08. @@ -50,41 +47,6 @@ public static String keyGenerator(int length, boolean numberType) { return result.toString(); } - /** - * currentTimestamp 기준 이후의 분 시점을 반환합니다. - * - * @param currentTimestamp 변환 할 시간 - * @param afterMinute 변환 할 시점 - * @return java.sql.Timestamp - */ - public static Timestamp afterMinute(Timestamp currentTimestamp, int afterMinute) { - int targetTime = 60 * afterMinute * 1000; // 초 - return new Timestamp(currentTimestamp.getTime() + targetTime); - } - - /** - * currentTimestamp 기준 이전의 분 시점을 반환합니다. - * - * @param currentTimestamp 변환 할 시간 - * @param beforeMinute 변환 할 시점 - * @return java.sql.Timestamp - */ - public static Timestamp beforeMinute(Timestamp currentTimestamp, int beforeMinute) { - int targetTime = 60 * beforeMinute * 1000; // 초 - return new Timestamp(currentTimestamp.getTime() - targetTime); - } - - /** - * 랜덤 주문번호를 생성합니다. - * - * @return java.lang.String - */ - public static String orderNumberGenerator() { - String key = keyGenerator(6, true); - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - return timestamp.getTime() + "_" + key; - } - /** * 바이너리 바이트 배열을 스트링으로 변환 */ @@ -134,44 +96,6 @@ public static byte binaryStringToByte(String s) { return total; } - /** - * 운전 면허번호 유효성 체크 - * 지역명이 포함된 운전면허번호 조회시 경북-95-255933-61 방식으로 넘깁니다. - * 숫자만 포함된 운전면허번호 조회시에도 19-95-255933-61 방식으로 통일합니다. - * - * @param licenseNo 유효성을 체크할 면허번호를 입력합니다. - * @return boolean - */ - public static boolean checkLicenseValidity(String licenseNo) { - - String regex = ".*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*"; //한글이 포함되어 있는 운전면허번호 구분 - - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(licenseNo); - - if (matcher.matches()) { - String regex1 = "^([가-힣]{2}(\\s|-)?|[가-힣]{2}-?)(\\s|-)?\\d{2}(\\s|-)?\\d{6}(\\s|-)?\\d{2}$"; //지역명 포함 운전면허번호 - pattern = Pattern.compile(regex1); - } else { - String regex2 = "^((1[1-9])|(2[0-6])|(26)|(28))(\\s|-)?\\d{2}(\\s|-)?\\d{6}(\\s|-)?\\d{2}$"; //숫자만 포함 운전면허번호 - pattern = Pattern.compile(regex2); - } - - matcher = pattern.matcher(licenseNo); - return matcher.matches(); - } - - /** - * 휴대폰번호 유효성 체크 - * - * @param str 휴대폰번호 - * @return boolean - */ - public static boolean checkCellphone(String str) { - //010, 011, 016, 017, 018, 019 - return str.matches("(01[016789])-(\\d{3,4})-(\\d{4})"); - } - /** * 위도, 경도를 사용하여 시작점과 끝점의 거리를 구합니다. (단위 km) * @@ -251,17 +175,4 @@ public static byte[] decryptFile(Key key, byte[] textCryp) { return decrypted; } - - /** - * 패스워드 정규식 - * 최소 8 자 이상, 숫자, 특수문자 1개이상씩 포함 - * - * @param password 체크 할 비밀번호 평문 - * @return boolean - */ - public static boolean passwordThen8AndSpecialCharacters(String password) { - Pattern p = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"); - Matcher m = p.matcher(password); - return m.matches(); - } } \ No newline at end of file diff --git a/util/src/main/java/com/ziponia/util/VerifyUtilComponent.java b/util/src/main/java/com/ziponia/util/VerifyUtilComponent.java new file mode 100644 index 0000000..b3fda8f --- /dev/null +++ b/util/src/main/java/com/ziponia/util/VerifyUtilComponent.java @@ -0,0 +1,58 @@ +package com.ziponia.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VerifyUtilComponent { + + /** + * 패스워드 정규식 + * 최소 8 자 이상, 숫자, 특수문자 1개이상씩 포함 + * + * @param password 체크 할 비밀번호 평문 + * @return boolean + */ + public static boolean passwordThen8AndSpecialCharacters(String password) { + Pattern p = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"); + Matcher m = p.matcher(password); + return m.matches(); + } + + /** + * 휴대폰번호 유효성 체크 + * + * @param str 휴대폰번호 + * @return boolean + */ + public static boolean checkCellphone(String str) { + //010, 011, 016, 017, 018, 019 + return str.matches("(01[016789])-(\\d{3,4})-(\\d{4})"); + } + + /** + * 운전 면허번호 유효성 체크 + * 지역명이 포함된 운전면허번호 조회시 경북-95-255933-61 방식으로 넘깁니다. + * 숫자만 포함된 운전면허번호 조회시에도 19-95-255933-61 방식으로 통일합니다. + * + * @param licenseNo 유효성을 체크할 면허번호를 입력합니다. + * @return boolean + */ + public static boolean checkLicenseValidity(String licenseNo) { + + String regex = ".*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*"; //한글이 포함되어 있는 운전면허번호 구분 + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(licenseNo); + + if (matcher.matches()) { + String regex1 = "^([가-힣]{2}(\\s|-)?|[가-힣]{2}-?)(\\s|-)?\\d{2}(\\s|-)?\\d{6}(\\s|-)?\\d{2}$"; //지역명 포함 운전면허번호 + pattern = Pattern.compile(regex1); + } else { + String regex2 = "^((1[1-9])|(2[0-6])|(26)|(28))(\\s|-)?\\d{2}(\\s|-)?\\d{6}(\\s|-)?\\d{2}$"; //숫자만 포함 운전면허번호 + pattern = Pattern.compile(regex2); + } + + matcher = pattern.matcher(licenseNo); + return matcher.matches(); + } +} diff --git a/util/src/test/java/com/ziponia/util/DateUtilComponentTest.java b/util/src/test/java/com/ziponia/util/DateUtilComponentTest.java new file mode 100644 index 0000000..77f223d --- /dev/null +++ b/util/src/test/java/com/ziponia/util/DateUtilComponentTest.java @@ -0,0 +1,42 @@ +package com.ziponia.util; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Calendar; + +@RunWith(JUnit4.class) +public class DateUtilComponentTest { + + @Test + public void dateLastTest() { + + int year = 2018; + int month = 2; + + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.YEAR, year); + cal.set(Calendar.MONTH, month - 1); + int last = DateUtilComponent.lastDate(cal.getTime()); + + System.out.println(String.format("%d 년 %d월 의 마지막날은 %d일 입니다.", year, month - 1, last)); + + // 2018 년 2월은 28 일까지 입니다. + Assert.assertEquals(28, last); + } + + @Test + public void dateLastByYM() { + int year = 2019; + int month = 6; + + int last = DateUtilComponent.lastDate(year, month); + + System.out.println(String.format("%d 년 %d월 의 마지막날은 %d일 입니다.", year, month, last)); + + // 2019 년 6월은 30 일까지 입니다. + Assert.assertEquals(30, last); + } +} \ No newline at end of file diff --git a/util/src/test/java/com/ziponia/util/VerifyUtilComponentTest.java b/util/src/test/java/com/ziponia/util/VerifyUtilComponentTest.java new file mode 100644 index 0000000..7375a8b --- /dev/null +++ b/util/src/test/java/com/ziponia/util/VerifyUtilComponentTest.java @@ -0,0 +1,28 @@ +package com.ziponia.util; + +import org.junit.Assert; +import org.junit.Test; + +public class VerifyUtilComponentTest { + + @Test + public void passwordThen8AndSpecialCharactersTest() { + + // only number length 4 + boolean p1 = VerifyUtilComponent.passwordThen8AndSpecialCharacters("1234"); + + // number is not found + boolean p2 = VerifyUtilComponent.passwordThen8AndSpecialCharacters("p@sswordsIsTrue"); + + // Special Characters is not found + boolean p3 = VerifyUtilComponent.passwordThen8AndSpecialCharacters("passwordIsTrue78"); + + // Good Password + boolean p4 = VerifyUtilComponent.passwordThen8AndSpecialCharacters("p@ss3WordIl0veU"); + + Assert.assertFalse(p1); + Assert.assertFalse(p2); + Assert.assertFalse(p3); + Assert.assertTrue(p4); + } +}