[英]Check if a string contains only date
I have a string which can contain a date(yyyy-MM-dd) or date and time (yyyy-MM-dd HH:mm:ss) in respective formats.我有一个字符串,它可以包含相应格式的日期(yyyy-MM-dd)或日期和时间(yyyy-MM-dd HH:mm:ss)。
I want to know which strings contains only date.我想知道哪些字符串只包含日期。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.parse("2015-02-02"));
System.out.println(dateFormat.parse("2015-02-02 23:23:23"));
In above code, both the strings are parsed successfully, whereas the format is same for only first.在上面的代码中,两个字符串都被成功解析,而只有第一个的格式相同。
public static void main(String[] args) {
String dateOnly = "2015-02-02";
String dateAndTimeOnly = "2015-02-02 23:23:23";
System.out.println("Date Only = " + validateDateFormat(dateOnly));
System.out.println("Date And time Only = " + validateDateFormat(dateAndTimeOnly));
}
public static boolean validateDateFormat(String input) {
return input.matches("([0-9]{4})-([0-9]{2})-([0-9]{2})");
}
output输出
Date Only = true
Date And time Only = false
Regex is self explanatory - Input will be separated by -
, ist part( [0-9]{4}
) can contain 4 digit , 2nd part can contain 2 digit [0-9]{2}
, so as 3rd.正则表达式是自解释的-输入将被分离
-
,IST部分( [0-9]{4}
可包含4位,第二部分可以包含2位[0-9]{2}
从而第三。
I would use the overload of parse
which takes a ParsePosition
- you can then check the position afterwards:我会使用带有
ParsePosition
的parse
重载 - 然后你可以检查位置:
import java.util.*;
import java.text.*;
public class Test {
public static void main(String[] args) throws Exception {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
System.out.println(parseFully(dateFormat, "2015-02-02"));
System.out.println(parseFully(dateFormat, "2015-02-02 23:23:23"));
}
private static Date parseFully(DateFormat format, String text)
throws ParseException {
ParsePosition position = new ParsePosition(0);
Date date = format.parse(text, position);
if (position.getIndex() == text.length()) {
return date;
}
if (date == null) {
throw new ParseException("Date could not be parsed: " + text,
position.getErrorIndex());
}
throw new ParseException("Date was parsed incompletely: " + text,
position.getIndex());
}
}
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. java.util
Date-Time API 及其格式化 API SimpleDateFormat
已过时且容易出错。 It is recommended to stop using them completely and switch to the modern Date-Time API * .建议完全停止使用它们并切换到现代 Date-Time API * 。
Solution using java.time
, the modern Date-Time API:使用现代日期时间 API
java.time
解决方案:
Let's first try to do it the way you have done:让我们首先尝试按照您的方式进行操作:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String[] arr = { "2015-02-02", "2015-02-02 23:23:23" };
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ENGLISH);
for (String s : arr) {
System.out.println("Attempting to parse '" + s + "':");
LocalDate date = LocalDate.parse(s, dtf);
System.out.println("Parsed successfully: " + date);
}
}
}
Output:输出:
Attempting to parse '2015-02-02':
尝试解析“2015-02-02”:
Parsed successfully: 2015-02-02解析成功:2015-02-02
Attempting to parse '2015-02-02 23:23:23':试图解析“2015-02-02 23:23:23”:
Exception in thread "main" java.time.format.DateTimeParseException: Text线程“main”中的异常 java.time.format.DateTimeParseException: Text
'2015-02-02 23:23:23' could not be parsed, unparsed text found at index 10'2015-02-02 23:23:23' 无法解析,在索引 10 处找到未解析的文本
As you can see, the java.time
API correctly throws an exception informing you about the problem.如您所见,
java.time
API 正确地抛出了一个异常通知您有关问题。 SimpleDateFormat
, on the other hand, parses the input string silently which has caused the problem that you have posted.另一方面,
SimpleDateFormat
以静默方式解析输入字符串,这导致了您发布的问题。
Thus, with the modern date-time API, you have two easy options:因此,使用现代日期时间 API,您有两个简单的选择:
2015-02-02 23:23:23
) is not a date string as per the specified date pattern.2015-02-02 23:23:23
)不是指定日期模式的日期字符串。DateTimeFormatter#parse(CharSequence, ParsePosition)
with the ParsePosition
index set to 0
.DateTimeFormatter#parse(CharSequence, ParsePosition)
并将ParsePosition
索引设置为0
。 Given below is a demo of the second option:下面给出了第二个选项的演示:
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String[] arr = { "2015-02-02", "2015-02-02 23:23:23" };
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ENGLISH);
for (String s : arr) {
ParsePosition pp = new ParsePosition(0);
LocalDate.from(dtf.parse(s, pp));
if (pp.getIndex() < s.length()) {
System.out.println("'" + s + "' is not a date string as per the specified date pattern.");
}
}
}
}
Output:输出:
'2015-02-02 23:23:23' is not a date string as per the specified date pattern.
'2015-02-02 23:23:23' 不是指定日期模式的日期字符串。
Note: Never use SimpleDateFormat or DateTimeFormatter without a Locale .注意:切勿在没有 Locale 的情况下使用 SimpleDateFormat 或 DateTimeFormatter 。
Learn more about the modern Date-Time API from Trail: Date Time .从Trail: Date Time 中了解有关现代日期时间 API 的更多信息。
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaring和How to use ThreeTenABP in Android Project 可用的 Java 8+ APIs 。
Once the desired format
is reached , SimpleDateFormat
doesnt format the rest of String
.一旦达到所需的
format
, SimpleDateFormat
不会格式化String
的其余部分。 It is the reason why your second string is parsed.这就是解析第二个字符串的原因。
This post SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?这篇文章SimpleDateFormat parse(string str) 在 str = 2011/12/12aaaaaaaaa 时不会抛出异常? may help you .
可能会帮助你。
Also check the DateFormat#parse
method in java docs还要检查 java docs 中的
DateFormat#parse
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.