简体   繁体   English

为什么 Java 告诉我“”是有效日期?

[英]Why is Java telling me “” is a valid date?

So, this is what I'm using as my isDate in Java.所以,这就是我在 Java 中使用的isDate

public class Common {
    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static boolean isDate(String text) {
        return isDate(text, DATE_PATTERN);
    }

    public static boolean isDate(String text, String date_pattern) {
        String newDate = text.replace("T00:00:00", "");
        SimpleDateFormat formatter = new SimpleDateFormat(date_pattern);
        ParsePosition position = new ParsePosition(0);
        formatter.parse(newDate, position);
        formatter.setLenient(false);
        if (position.getIndex() != newDate.length()) {
            return false;
        } else {
            return true;
        }
    }
}

Here is my test code:这是我的测试代码:

String fromDate = "";

if (Common.isDate(fromDate)) {
    System.out.println("WHAT??????");
}

I see WHAT??????我看到WHAT?????? printed every time.每次打印。 What am I missing here?我在这里想念什么?

Thanks.谢谢。

It is because your logic is not correct.那是因为你的逻辑不正确。 newDate="" , ie newDate.length()==0 . newDate="" ,即newDate.length()==0 As well as position.getIndex()==0 since the error is occuring at the very beginning of the string.以及position.getIndex()==0因为错误发生在字符串的最开头。 You may test whether position.getErrorIndex()>=0 .您可以测试position.getErrorIndex()>=0是否。

The right way to check for a successful parse is to see, if the parse method returns a Date or null .检查成功解析的正确方法是查看parse方法是否返回 Date 或null Try this:尝试这个:

public static boolean isDate(String text, String date_pattern) {
    String newDate = text.replace("T00:00:00", "");
    SimpleDateFormat formatter = new SimpleDateFormat(date_pattern);
    ParsePosition position = new ParsePosition(0);
    formatter.setLenient(false);
    return formatter.parse(newDate, position) != null;
}

Don't reinvent the wheel... use Joda Time ;)不要重新发明轮子……使用Joda Time ;)

    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");
    try {
        DateTime dt = fmt.parseDateTime("blub235asde");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return false;
    }
    return true;

Output: Output:

java.lang.IllegalArgumentException: Invalid format: "blub235asde"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:673)
    at Test.main(Test.java:21)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我不能使用Integer时,为什么Java没有告诉我? - Why is Java not telling me when I can't use Integer? 为什么Java告诉我我的软件包不存在? - Why is java telling me that my package does not exist? java -version 是否告诉我有关 JRE 或 JDK 的信息? - Is java -version telling me about the JRE or the JDK? Java-为什么Eclipse告诉我我的方法必须返回int类型? - Java - Why is Eclipse telling me my method must return type int? 为什么服务器向我返回 Java 中有效文件的响应代码 403? - Why server returns me the Response Code 403 for a valid file in java? 为什么IntelliJ告诉我该变量尚未初始化? - Why is IntelliJ telling me this variable has not been initialized? 在 Eclipse 中,为什么它告诉我添加一个已经存在的分号? - In Eclipse, why is it telling me to add in a semi-colon that is already there? Java 1.7 仍然告诉我 switch 语句中的字符串不兼容 - Java 1.7 still telling me that strings in switch statements are incompatible 为什么netbeans不断告诉我我的变量没有被使用? - Why does netbeans keep telling me that my variables are not being used? 为什么Playframework告诉我它不支持子类化? - Why is Playframework telling me that it doesn't support subclassing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM