java 如何检查字符串是否为日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33968333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a string is date?
提问by Niks
I am having string like
我有像这样的字符串
"11-04-2015 22:01:13:053" or "32476347656435"
How can I check if string is Date?
Checking String if it is numeric using regex
如何检查字符串是否为日期?
使用正则表达式检查字符串是否为数字
String regex = "[0-9]+";
回答by amit prasad
Other person are also correct
其他人也正确
This is your answer
这是你的答案
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class date {
public static boolean isValidDate(String inDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:ms");
dateFormat.setLenient(false);
try {
dateFormat.parse(inDate.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isValidDate("20-01-2014"));
System.out.println(isValidDate("11-04-2015 22:01:33:023"));
System.out.println(isValidDate("32476347656435"));
}
}
回答by Davide Lorenzo MARINO
There are two possible solutions:
有两种可能的解决方案:
- Use a
SimpleDateFormatand try to convert theStringtoDate. If noParseExceptionis thrown the format is correct. - Use a regular expressionto parse the
String
- 使用 a
SimpleDateFormat并尝试将 转换String为Date。如果ParseException抛出no则格式正确。 - 使用正则表达式解析
String
回答by Grunion Shaftoe
The best solution is to actually try to cast it to a date with DateTime.TryParse()
最好的解决方案是实际尝试使用 DateTime.TryParse() 将其转换为日期
string d = "11-04-2015 22:01:13:053";
DateTime dt = new DateTime();
if (DateTime.TryParse(d, out dt))
{
/// yes, it's a date, do something here...
}
else
{
// no, it's not a date, do something else here...
}
回答by Ole V.V.
java.time
时间
It's about time someone provides the modern answer. The SimpleDateFormatclass mentioned in a couple of the other answers is notoriously troublesome and fortunately now long outdated. Instead the modern solution uses java.time, the modern Java date and time API.
是时候有人提供现代答案了。SimpleDateFormat其他几个答案中提到的课程非常麻烦,幸运的是现在已经过时了。相反,现代解决方案使用 java.time,现代 Java 日期和时间 API。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss:SSS");
String stringToTest = "11-04-2015 22:01:13:053";
try {
LocalDateTime dateTime = LocalDateTime.parse(stringToTest, formatter);
System.out.println("The string is a date and time: " + dateTime);
} catch (DateTimeParseException dtpe) {
System.out.println("The string is not a date and time: " + dtpe.getMessage());
}
Output from this snippet is:
这个片段的输出是:
The string is a date and time: 2015-04-11T22:01:13.053
该字符串是日期和时间:2015-04-11T22:01:13.053
Suppose that instead the string was defined as:
假设字符串被定义为:
String stringToTest = "32476347656435";
Now the output is:
现在输出是:
The string is not a date and time: Text '32476347656435' could not be parsed at index 2
该字符串不是日期和时间:无法在索引 2 处解析文本“32476347656435”
Link:Oracle tutorial: Date Timeexplaining how to use java.time.
链接:Oracle 教程:解释如何使用 java.time 的日期时间。

