Language/JAVA

[SimpleDateFormat] 날짜 유효성 체크

개랭갱깽스타 2020. 6. 24. 14:21

날짜 유효성 체크

 

 /**
     * 날짜 형식 체크하기
     */
    public static boolean checkDateFormat(String date) {
        if (TextUtils.isEmpty(date)) {
            return false;
        }

        if(!date.matches("\\d{4}\\.\\d{2}\\.\\d{2}")){
            return false;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.mm.dd", Locale.KOREA);
        sdf.setLenient(false);

        try {
            sdf.parse(date);
            return true;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }
반응형