[英]I want to write a code to validate different types of dates using java but dates with T and Z are still showing Invalid
Here is the code that I have written 这是我编写的代码
Please suggest me if anyone has ever tried something like this. 如果有人尝试过这种方法,请提出建议。
private static final String[] Date_Reg_Ex = {
"yyyy-MM", "yyyy-MM-dd", "dd-MMMM-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "dd-MM-yyyy HH:mm:ss/SSS",
"yyyy-MM-dd HH:mm", "yyyy-mm-dd'T'HH:MM:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd'@'HH:mm:ss",
"yyyy-MM-dd'/'K:mm a", "MM-dd-yyyy hh:mm a", "MM-dd-yyyy hh:mm:ss a", "hh:mm:ss MMM d, yyyy z",
"EEEE dd/MM/yyyy", "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE MMM dd hh:mm:ss z yyyy", "dd MMMMM yyyy",
"MM/dd/yyyy HH:mm", "MM/YYYY", "MM/YY", "dd/MM/yyyy", "yyyy/MM/dd", "MMM, dd/yyyy", "dd.MM.yyyy", "MMM. dd",
"yyyy", "yyyyMMdd", "MMddyyyy", "MMMM yyyy", "ddMMyyyyHHmmss", "yyyy"
};
public static boolean isValidDate(String value, TimeZone timeZone) {
DateValidator dateValidator = DateValidator.getInstance();
for (String dateRegEx : Date_Reg_Ex) {
if (dateValidator.isValid(value, dateRegEx)) {
return true;
}
}
return false;
}
Since you wrote "dates with T and Z are still showing invalid", I suppose you mean: 由于您写过“ T和Z的日期仍然显示无效”,我想您的意思是:
and 和
I could not find any problems with 2,3,4 and 5. Date 1 however switches months and minutes which I guess is not intended. 我找不到2,3,4和5的任何问题。但是日期1切换了我想不是要使用的月份和分钟。
Here are examples of dates that worked for me: 以下是一些适合我的日期示例:
It could be related to your local, I tested some example from @Aio and had to force a specific default local to make it succeed I also applied his change to 8th pattern: 它可能与您的本地语言有关,我测试了@Aio的一些示例,并且必须强制使用特定的默认本地语言才能使其成功,我还将他的更改应用于第8个模式:
I used version 1.6 of Apache Commons Validator 我使用了Apache Commons Validator的1.6版
private static final String[] Date_Reg_Ex = {
"yyyy-MM", "yyyy-MM-dd", "dd-MMMM-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "dd-MM-yyyy HH:mm:ss/SSS",
"yyyy-MM-dd HH:mm", "yyyy-MM-dd'T'HH:mm:ss" /** This has been updated as suggested by Aio **/, "yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.S", "yyyy-MM-dd'@'HH:mm:ss",
"yyyy-MM-dd'/'K:mm a", "MM-dd-yyyy hh:mm a", "MM-dd-yyyy hh:mm:ss a", "hh:mm:ss MMM d, yyyy z",
"EEEE dd/MM/yyyy", "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE MMM dd hh:mm:ss z yyyy", "dd MMMMM yyyy",
"MM/dd/yyyy HH:mm", "MM/YYYY", "MM/YY", "dd/MM/yyyy", "yyyy/MM/dd", "MMM, dd/yyyy", "dd.MM.yyyy", "MMM. dd",
"yyyy", "yyyyMMdd", "MMddyyyy", "MMMM yyyy", "ddMMyyyyHHmmss", "yyyy"
};
public static boolean isValidDate(String value, TimeZone timeZone) {
DateValidator dateValidator = DateValidator.getInstance();
for (String dateRegEx : Date_Reg_Ex) {
if (dateValidator.isValid(value, dateRegEx)) {
return true;
}
}
return false;
}
public static void main(String... args) {
// without the next line some tests fail
Locale.setDefault(Locale.US);
assertTrue("2019-09-12T13:21:31");
assertTrue("2019-09-12T13:21:31.020");
assertTrue("1:21:31 Sep 12, 2019 PST");
assertTrue("Thursday, 12 Sep 2019 13:21:31 PST");
assertTrue("Thursday Sep 12 1:21:31 PST 2019");
}
private static void assertTrue(String s) {
if (!isValidDate(s, null)) {
System.err.println("Not valid: " + s);
} else {
System.out.println("Valid: " + s);
}
}
This gives me: 这给了我:
Valid: 2019-09-12T13:21:31
Valid: 2019-09-12T13:21:31.020
Valid: 1:21:31 Sep 12, 2019 PST
Valid: Thursday, 12 Sep 2019 13:21:31 PST
Valid: Thursday Sep 12 1:21:31 PST 2019
Process finished with exit code 0
I think there is a typo at yyyy-mm-dd'T'HH:MM:ss. 我认为yyyy-mm-dd'T'HH:MM:ss有一个错字。
mm stands for minutes and MM stands for month, here it should be yyyy-MM-dd'T'HH:mm:ss. mm代表分钟,MM代表月份,此处应为yyyy-MM-dd'T'HH:mm:ss。
Please try correcting it once, I hope you we get the desired result without any program/extra code. 请尝试对其进行一次更正,希望您在没有任何程序/附加代码的情况下获得理想的结果。
I used SimpleDateFormat for this instead of any external Library, You can Validate the Date with simply parsing the date and checking an exception - 我为此使用了SimpleDateFormat而不是任何外部库,您可以通过简单地解析日期并检查异常来验证日期-
I suppose you can use something like this - 我想你可以用这样的东西-
private static final String[] DATEFORMATS = {"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'","yyyy-MM-dd'T'HH:mm:ss'Z'",
"dd-MM-yy HH:mm:ss","dd-MMM-yy HH:mm:ss","yyyy-MM-dd","yyyy-MMM-dd","dd-MMM-yyyy","dd-MM-yyyy"};
public static Date parseDate(String date ) {
SimpleDateFormat sdf = new SimpleDateFormat();
Date parsedDate = null;
for(String format : DATEFORMATS) {
try {
sdf.applyPattern(format);
parsedDate = sdf.parse(date);
break;
}catch(ParseException e) {
LOG.debug("parseDate:: Not in format::{} Moving to next ",format);
}
}
return parsedDate;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.