Java date validation joda time

前端 未结 4 1710
失恋的感觉
失恋的感觉 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:26

    Use SimpleDateFormat

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

提交回复
热议问题