简体   繁体   English

Java Date异常处理尝试捕获

[英]Java Date exception handling try catch

Is there some sort of exception in Java to catch an invalid Date object? Java中是否存在某种异常来捕获无效的Date对象? 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. 它是一个ParseException。

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

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.date or throw an exception up if that's the desired behavior. 您可能想要对this.date进行操作,或者如果需要的话抛出异常。

What you are really seeking is either: 您真正要寻找的是:

  1. ParseException - thrown by a DateFormat object when it attempts to parse() , which would happen before your set method ParseExceptionDateFormat对象在尝试parse()时抛出,该事件将在您的set方法之前发生
  2. IllegalArgumentException - thrown by a SimpleDateFormat constructor, again it would happen before your set method. IllegalArgumentExceptionSimpleDateFormat构造函数引发,同样会在您的set方法之前发生。 Indicates you provided an invalid format string. 表示您提供了无效的格式字符串。

You'd want to catch one of those (probably #1). 您可能想抓住其中一个(可能是#1)。 But it has to happen before your method call. 但是它必须您的方法调用之前发生。 Once you have a Date object, it is either null or valid. 拥有Date对象后,该对象将为null或有效。

This might not be related to the original question. 这可能与原始问题无关。 But you must notice the method name, which is setDate() . 但是您必须注意方法名称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 boolean there? 或者,如果可能,那么您认为在此处返回boolean是个好主意吗? IMO, do something like this, 海事组织,做这样的事情,

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

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

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. 您最好能做的是确保日期不为空。

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。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM