[英]How to check if a string contains a date in Java?
How do I check if a string contains a date of this form:如何检查字符串是否包含这种形式的日期:
Sunday, January 15, 2012 at 7:36pm EST美国东部时间 2012 年 1 月 15 日星期日晚上 7:36
The data I'm working with contains a ton of strings.我正在处理的数据包含大量字符串。 But the type of string I'm looking for contains a 2 or 3 word name and a date.但是我正在寻找的字符串类型包含一个 2 或 3 个单词的名称和一个日期。 I'm checking for dates to identify these types of strings.我正在检查日期以识别这些类型的字符串。
I've figured out the simpleDateFormat for this type of date.我已经找到了这种类型的日期的 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");
But I have no idea how to proceed further.但我不知道如何进一步。
I'm guessing regex might work but I don't know how to implement that when the length of the names of months/days vary.我猜正则表达式可能会起作用,但是当月/日名称的长度不同时,我不知道如何实现它。 ie 'May' is much shorter than 'December'.即“May”比“December”短得多。
I'm wondering if there is a solution using regex or a simpler solution to this.我想知道是否有使用正则表达式的解决方案或更简单的解决方案。
I know there are other threads asking similar questions, but they don't answer my question.我知道还有其他线程在问类似的问题,但他们没有回答我的问题。
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/ 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.当您在文本中找到匹配项时,您可以使用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));
}
This will print:这将打印:
Sunday, January 15, 2012 at 7:37pm EST
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).如果你不想使用正则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);
}
}
Output输出
Rahul Chowdhury Sunday January 15 2012 7:36pm EST
7 36 pm
You could test it using the simpleDateFormat parse method.您可以使用 simpleDateFormat 解析方法对其进行测试。 to continue your code, surround the code with a try/catch, for instance:要继续您的代码,请用 try/catch 包围代码,例如:
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.如果日期是遵循 SimpleDateFormat 格式指南的字符串,则日期将被成功创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.