您可以先使用正则表达式检查日期是否存在:
w+,s+w+s+d+,s+d+s+ats+d+:d+(pm|am)s+w{3,4}此正则表达式同时匹配
Rahul Chowdhury Sunday, January 15, 2012 at 7:37pm ESTAritra 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



