How to check validity of Date String?

前端 未结 4 880
无人及你
无人及你 2020-12-04 01:16

In my project I need to check if a date string evaluates to a proper Date object. I\'ve decided to allow yyyy-MM-dd, and Date formats [(year, month, date) and (year, month,

4条回答
  •  暖寄归人
    2020-12-04 01:43

    You are doing this the wrong way. You should use SimpleDateFormat.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        Date test = sdf.parse(input);
    } catch (ParseException pe) {
       //Date is invalid, try next format
    }
    

提交回复
热议问题