Java date validation joda time

前端 未结 4 1739
失恋的感觉
失恋的感觉 2021-01-12 02:24

Is there anyway to validate if a given date(yyyy-MM-dd) is a valid date? It should handle leap year too. eg(2015-02-29) should be invalid. I\'m retrieving the date as a stri

4条回答
  •  清歌不尽
    2021-01-12 03:08

    This should work for you, I think (if you want to keep it simple).
    You have to do setLenient(false) on a SimpleDateFormat.

    public static boolean validateDate(String dateString){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        try {
            sdf.parse(dateString);
            return true;
        } catch (ParseException ex) {
            return false;
        }
    }
    

提交回复
热议问题