您可以首先使用正则表达式检查日期是否存在:
\w+,\s+\w+\s+\d+\,\s+\d+\s+at\s+\d+:\d+(pm|am)\s+\w{3,4}
这个正则表达式匹配两者
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/
当您在文本中找到匹配项时,您可以使用SimpleDateFormat
检查它是否格式良好。
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));
}
这将打印:
Sunday, January 15, 2012 at 7:37pm EST