How to check if a string contains a date in Java?

前端 未结 3 806
北海茫月
北海茫月 2021-01-19 05:50

How do I check if a string contains a date of this form:

Sunday, January 15, 2012 at 7:36pm EST

The data I\'m working with contains

3条回答
  •  野的像风
    2021-01-19 06:36

    You could test it using the simpleDateFormat parse method. to continue your code, surround the code with a try/catch, for instance:

    try {
        Date date = format.parse(string);
    } catch (ParseException e) {
            //the string is not applicable to the date format
    }
    

    If the date is a string which follows the format guidelines in the SimpleDateFormat, the Date will be created successfully.

提交回复
热议问题