Java 日期异常处理 try catch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2512947/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:34:39  来源:igfitidea点击:

Java Date exception handling try catch

javaexception-handlingformattingparseexception

提问by user69514

Is there some sort of exception in Java to catch an invalid Date object? I'm trying to use it in the following method, but I don't know what type of exception to look for. Is it a ParseException.

Java 中是否存在某种异常来捕获无效的 Date 对象?我正在尝试在以下方法中使用它,但我不知道要查找什么类型的异常。是不是 ParseException。

public boolean setDate(Date date) {
        this.date = date;                
        return true;
    }

回答by Phil

In the method you provide, there is no way to catch an exception, because none will be thrown by the simple assignment. All you can do is maybe the below change:

在您提供的方法中,无法捕获异常,因为简单赋值不会抛出任何异常。您所能做的可能就是以下更改:

if(date == null) return false;

But even that's not graceful. You may want to do something with this.dateor throw an exception up if that's the desired behavior.

但即使这样也不优雅。this.date如果这是所需的行为,您可能想要做一些事情或抛出异常。

What you are really seeking is either:

您真正寻求的是:

  1. ParseException- thrown by a DateFormatobject when it attempts to parse(), which would happen before your set method
  2. IllegalArgumentException- thrown by a SimpleDateFormatconstructor, again it would happen before your set method. Indicates you provided an invalid format string.
  1. ParseException-DateFormat对象在尝试时抛出parse(),这会在您的 set 方法之前发生
  2. IllegalArgumentException- 由SimpleDateFormat构造函数抛出,再次发生在你的 set 方法之前。表示您提供了无效的格式字符串。

You'd want to catch one of those (probably #1). But it has to happen beforeyour method call. Once you have a Dateobject, it is either nullor valid.

你想抓住其中之一(可能是#1)。但它必须您的方法调用之前发生。一旦你有了一个Date对象,它要么是null有效的,要么是有效的。

回答by Adeel Ansari

This might not be related to the original question. But you must notice the method name, which is setDate(). Do you think it sounds like it will return something? Or if it may, then do you think its a good idea to return a booleanthere? IMO, do something like this,

这可能与原始问题无关。但是您必须注意方法名称,即setDate(). 你认为它听起来会返回一些东西吗?或者,如果可以,那么您认为返回boolean那里是个好主意吗?IMO,做这样的事情,

public void setDate(Date date) {
    this.date = date;                
}

public boolean isDateNull() { // or something
    return this.date == null;                
}

回答by tomhermann

It depends on what you mean by an invalid date. Did you mean to give us a method signature that looked more like this?

这取决于您所说的无效日期是什么意思。你的意思是给我们一个看起来更像这样的方法签名吗?

public void setDate(String date) throws ParseException {
   this.date = SomeDateFormat.getInstance().format(date);
}

Otherwise, as the others stated the simple act of assigning a Java date object to a field shouldn't be exceptional as it is either an instance of Date already, or null.

否则,正如其他人所说,将 Java 日期对象分配给字段的简单行为不应该是例外,因为它要么已经是 Date 的实例,要么为 null。

If you are just trying to parse a string into a java.util.Date, look at DateFormat, FastDateFormat (apache, thread safe), or Joda Time.

如果您只是想将字符串解析为 java.util.Date,请查看DateFormat、FastDateFormat(apache,线程安全)或 Joda Time。

回答by Jay Askren

In this method, there is no need to worry about an exception. The date is already created by the time you get into this method. If you are parsing a date, it would have done outside of this code. The best you could do is make sure the date is not null.

在这种方法中,无需担心异常。当您进入此方法时,日期已经创建。如果您正在解析一个日期,它将在此代码之外完成。您能做的最好的事情就是确保日期不为空。