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

前端 未结 3 794
北海茫月
北海茫月 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:28

    If you don't want to use Regex you may do something like this (I know it is a pain but just a different approach).

    public class ParseDate {
    
        public static void main(String[] args) {
            String date = "Rahul Chowdhury Sunday, January 15, 2012 at 7:36pm EST";
            //Pattern: "Fullname EEEEE, MMM dd, yyyy 'at' hh:mmaa z"
            String dateComponents[] = date.split(",");
            String fullName = dateComponents[0].substring(0, dateComponents[0].lastIndexOf(" "));
            String dayText = dateComponents[0].substring(dateComponents[0].lastIndexOf(" "));
            String month = dateComponents[1].trim().split(" ")[0];
            String dayNumber = dateComponents[1].trim().split(" ")[1];
            String year = dateComponents[2].split("at")[0];
            String time = dateComponents[2].split("at")[1].trim().split(" ")[0];
            String zone =dateComponents[2].split("at")[1].trim().split(" ")[1];
    
            // if you want to go further 
            String hour = time.split(":")[0];
            String minutes = time.split(":")[1].substring(0,2);
            String aa = time.split(":")[1].substring(2,4);
    
    
            System.out.println(fullName + " " + dayText + " " + month + " " + dayNumber + " " + year + " " + time + " " + zone);
            System.out.println(hour + " " + minutes + " " + aa);
        }
    
    
    }
    

    Output

    Rahul Chowdhury Sunday January 15  2012  7:36pm EST
    7 36 pm
    
    0 讨论(0)
  • 2021-01-19 06:34

    You could first check the presence of your date with a regex:

    \w+,\s+\w+\s+\d+\,\s+\d+\s+at\s+\d+:\d+(pm|am)\s+\w{3,4}
    

    This regex matches both

    Rahul Chowdhury Sunday, January 15, 2012 at 7:37pm EST
    Aritra Sinha Nirmal Friday, April 1, 2016 at 10:16pm EDT
    

    https://regex101.com/r/V0dAf8/2/

    When you found the match in your text then you could use SimpleDateFormat to check if it is well formed.

    String input = "Rahul Chowdhury Sunday, January 15, 2012 at 7:37pm EST";
    String regex = "(\\w+,\\s+\\w+\\s+\\d+\\,\\s+\\d+\\s+at\\s+\\d+:\\d+(pm|am)\\s+\\w{3,4})";
    Matcher matcher = Pattern.compile(regex).matcher(input);
    if (matcher.find()) {
      System.out.println(matcher.group(1));
    }
    

    This will print:

    Sunday, January 15, 2012 at 7:37pm EST
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题