2017년 7월 28일 금요일

[java]날짜 유효성 검사

아래 오버 로딩 된 isParseableDateByFormat 함수는
객체를 받아서 날짜형 데이터로 변환이 가능한지 에 대한 true ,false
값을 리턴 하는 얘제이다

먼저 만들게 된 이유는 리스트 출력시 파라미터로 날짜 입력을 필수로 요구 하는 경우가 잇는데 null 체크만했을때 발생 되는 예외상황을 미연에 방지하기 위해서 null 체크 및 실제 날짜 데이터가 맞는지에 대한 검사를 실시 하기 위함이다.

파라미터타입이 원하는 포맷이 있을때와 없을때를 구분하여 처리하도록 오버로딩 하였다.

포맷이 없을 경우 기본 yyyyMMdd 형식  >> 20170728 형식이 맞는지 체크하고
포맷값이 있을경우 해당 포맷형태로 날짜 데이타가 맞는지에 체크한다.

!!) 주의사항

포맷이 yyyy 또는 mm 으로만 입력 되어지는 경우에는 parse 함수가 자동적으로

0145 는 145로 존재 할수 있는 년도 이기때문에 false를 리턴 하지 않는다.

자바소스로 한번 굴려보고 사용하길 권장함.


/**
* 전달받은 객체 8자리 날짜데이터로 변환 가능 한지 유효성 체크
* @param o
* @return
*/
public static boolean isParseableDateByFormat(Object o){
boolean result = true;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
df.setLenient(false);
if(o instanceof java.lang.String || o instanceof java.lang.Integer){

if(o instanceof java.lang.String){
try {
@SuppressWarnings("unused")
Date dt = df.parse((String) o);
}catch(ParseException pe){
result = false;
}catch(IllegalArgumentException ae){
result = false;
}
}else{
try {
@SuppressWarnings("unused")
Date dt = df.parse(String.valueOf(o));
}catch(ParseException pe){
result = false;
}catch(IllegalArgumentException ae){
result = false;
}
}
}else{
result = false;
}
return result;
}

/**
* 전달받은 객체를 특정 포맷의 날짜데이터로 변환 가능 한지 유효성 체크
* @param o
* @return
*/
public static boolean isParseableDateByFormat(Object o,String format){
boolean result = true;
SimpleDateFormat df = new SimpleDateFormat();
if(format.length() > 0){
df.applyPattern(format);
df.setLenient(false);
if(o instanceof java.lang.String || o instanceof java.lang.Integer){

if(o instanceof java.lang.String){
try {
@SuppressWarnings("unused")
Date dt = df.parse((String) o);
}catch(ParseException pe){
result = false;
}catch(IllegalArgumentException ae){
result = false;
}
}else{
try {
@SuppressWarnings("unused")
Date dt = df.parse(String.valueOf(o));
}catch(ParseException pe){
result = false;
}catch(IllegalArgumentException ae){
result = false;
}
}
}else{
result = false;
}
}else{
result = false;
}

return result;
}

댓글 없음:

댓글 쓰기

[lunux]리눅스 폴더별 용량 확인

리눅스 폴더별 용량 확인 조회 하고자 하는 디렉토리 리스트가있는 경로로 이동후 du -h --max-depth=1