如何在Java中检查字符串是否包含日期?

5

如何检查字符串是否包含以下格式的日期:

2012年1月15日星期日晚上7:36 EST

我正在处理大量字符串数据。但是,我要查找的字符串类型包含两个或三个单词的名称和一个日期。我正在检查日期以识别这些字符串类型。

我已经找出了适用于此类型日期的simpleDateFormat。

String string1 = "Rahul Chowdhury Sunday, January 15, 2012 at 7:37pm EST";
String string2 = "Aritra Sinha Nirmal Friday, April 1, 2016 at 10:16pm EDT";    

SimpleDateFormat format = new SimpleDateFormat("EEEEE, MMM dd, yyyy 'at' hh:mmaa z");

但我不知道该如何进一步处理。

我猜正则表达式可能适用,但我不知道如何实现当月份/日期的名称长度不同的情况下。例如,“五月”比“十二月”要短得多。

我想知道是否有使用正则表达式或更简单的解决方案。

我知道还有其他帖子提出类似的问题,但它们没有回答我的问题。


使用 format.parse(string)。如果格式不匹配,它将抛出 ParseException。 - Ashraful Islam
2
一个用于检查星期和月份的正则表达式可能会非常长。您能否更好地描述实际问题,您认为需要检查字符串以查看它们是否为日期? - Tim Biegeleisen
为什么要这样做?只需尝试将其解析为日期并捕获异常即可。没有必要重复所有这些努力。 - user207421
字符串不仅仅包含日期。因此,符合我的条件但还有其他单词的字符串将抛出异常,我会错过它们。 - Rahul Chowdhury
3个回答

6
你可以首先使用正则表达式检查日期是否存在:
\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

我爱你。非常感谢你。 - Rahul Chowdhury
我认为你至少希望在结尾处使用\w{3,4};有些时区缩写是四个字母。你可能需要适应更多的时区名称,例如Z+01:00 - Ole V.V.
@OleV.V. 感谢您的建议,我会更新我的答案。 - freedev

2
如果您不想使用 Regex,可以尝试以下方法(我知道这很麻烦,但这是另一种方法)。
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);
    }


}

输出

Rahul Chowdhury Sunday January 15  2012  7:36pm EST
7 36 pm

1
您可以使用simpleDateFormat的解析方法进行测试。为了继续您的代码,请将代码放在try/catch块中,例如:
try {
    Date date = format.parse(string);
} catch (ParseException e) {
        //the string is not applicable to the date format
}

如果日期是符合SimpleDateFormat格式指南的字符串,那么日期将会成功创建。

我使用的字符串不仅包含日期。因此,这将始终引发异常。 - Rahul Chowdhury
如果一个字符串不仅包含日期,它仍然会解析并创建一个日期,如果该字符串首先有日期和其余部分作为后缀。因此,对于字符串:“January 15, 2012 at 7:37pm EST, Rahul Chowdhury Sunday”,它将起作用。如果翻转顺序太麻烦,您只能解析子字符串 - 只需运行:try { Date date = format.parse(string.substring(string.indexOf(“,”)+ 1);} catch(ParseException e){ //该字符串不适用于日期格式} - Assafs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接