简体   繁体   English

我想编写一个代码来使用Java验证不同类型的日期,但带有T和Z的日期仍显示无效

[英]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的日期仍然显示无效”,我想您的意思是:

  1. "yyyy-mm-dd'T'HH:MM:ss" “ yyyy-mm-dd'T'HH:MM:ss”
  2. "yyyy-MM-dd'T'HH:mm:ss.SSS" “ yyyy-MM-dd'T'HH:mm:ss.SSS”

and

  1. "hh:mm:ss MMM d, yyyy z" “ hh:mm:ss MMM d,yyyy z”
  2. "EEE, dd MMM yyyy HH:mm:ss zzz" “ EEE,dd MMM yyyy HH:mm:ss zzz”
  3. "EEE MMM dd hh:mm:ss z yyyy" “ EEE MMM dd hh:mm:ss z yyyy”

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: 以下是一些适合我的日期示例:

  1. "yyyy- MM -dd'T'HH: mm :ss" -> "2019-09-12T13:21:31" “ yyyy- MM -dd'T'HH: mm :ss”->“ 2019-09-12T13:21:31”
  2. "yyyy-MM-dd'T'HH:mm:ss.SSS" -> "2019-09-12T13:21:31.020" “ yyyy-MM-dd'T'HH:mm:ss.SSS”->“ 2019-09-12T13:21:31.020”
  3. "hh:mm:ss MMM d, yyyy z" -> "1:21:31 Sep 12, 2019 PST" “ hh:mm:ss MMM d,yyyy z”->“ 2019年9月12日1:21:31 PST”
  4. "EEE, dd MMM yyyy HH:mm:ss zzz" -> "Thursday, 12 Sep 2019 13:21:31 PST" “ EEE,dd MMM yyyy HH:mm:ss zzz”->“ PST 2019年9月12日,星期四13:21:31”
  5. "EEE MMM dd hh:mm:ss z yyyy" -> "Thursday Sep 12 1:21:31 PST 2019" “ EEE MMM dd hh:mm:ss z yyyy”->“ PST 2019年9月12日星期四1:21:31”

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.

相关问题 Java正则表达式验证日期范围 - Java regex to validate range of dates 使用 Java 日期,给定一年,我想要一个方法,该方法应该返回表示一周中给定日期的日期列表,所有 52 次 - Using Java Dates, Given a year, I want a method that should return a list of dates representing a given day of the week, all 52 times 在Java中两个不同日期之间打印日期 - printing dates between two different dates in Java Java下一次约会 - Next dates using java Java Calendar返回不同的日期 - Java Calendar returns different dates SQL和Java返回不同的日期? - SQL and Java return different dates? Java 给出不同日期的日期 - Java Date giving different dates 我有一个日期列表,我想成对使用,然后在pja查询中成对使用,直到所有日期都用完了为止 - i have a list of dates and i want to use then in pairs as from and to in a pja query until all dates have been used end in java 如何从Java中的特定日期获取前7个日期?我正在从当前日期获取7个日期,但我想从特定日期获取 - How to get previous 7 dates from a particular date in java?I am getting 7 dates from present date, but I want from particular date 我想使用android中的按钮隐藏和取消隐藏文本; 该代码是正确的,但仍然无法正常工作 - I want to hide and unhide text using a button in android ; The code is correct but it still doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM