【问题标题】:How to check if a date is after today java如何检查日期是否在今天之后java
【发布时间】:2016-04-25 12:00:14
【问题描述】:

我正在尝试检查用户输入的日期是否在今天之后。这是我的代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date enteredDate = sdf.parse(date);
Date currentDate = new Date();
if(enteredDate.after(currentDate)){

Date 是一个变量,用户日期的格式为“2016/04/26”。做了一些调试后,我发现enteredDate 和currentDate 为空。任何想法为什么会这样? 谢谢

【问题讨论】:

  • currentDate 不能为空。请创建一个minimal reproducible example
  • Date currentDate = new Date() 后面的变量currentDate 怎么会是null? (提示:不可能……)
  • 在我的电脑上,日期为"2016/04/26",它工作正常。
  • 在这种情况下 currentDate 不可能为空。
  • 如果 enterDate 变量为 null [即enterDate.after(new Date())].

标签: java date parsing simpledateformat


【解决方案1】:

如 cmets 中所述,Date 对象不可能有空引用。但是,如果 sdf.parse(date) 抛出一个被抑制的异常,那么 enterDate 可能为空。

 String date="2016/04/26";
    Date enteredDate=null;
    try
    {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    enteredDate = sdf.parse(date);
    }catch (Exception ex)
    {
        // enteredDate will be null if date="287686";
    }
    Date currentDate = new Date();      
    if(enteredDate.after(currentDate)){
        System.out.println("after ");
    }else
        System.out.println("before");

【讨论】:

    【解决方案2】:

    java 日期类有 before 和 after 方法,你可以使用一个很好的例子是 this

    `import java.text.SimpleDateFormat;
     import java.util.Date;
     import java.text.ParseException;
     public class App 
     {
         public static void main( String[] args ) 
         {
         try{
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = sdf.parse("2009-12-31");
            Date date2 = sdf.parse("2010-01-31");
    
            System.out.println(sdf.format(date1));
            System.out.println(sdf.format(date2));
    
            if(date1.compareTo(date2)>0){
                System.out.println("Date1 is after Date2");
            }else if(date1.compareTo(date2)<0){
                System.out.println("Date1 is before Date2");
            }else if(date1.compareTo(date2)==0){
                System.out.println("Date1 is equal to Date2");
            }else{
                System.out.println("How to get here?");
            }
    
        }catch(ParseException ex){
            ex.printStackTrace();
        }
    }
    }`
    

    【讨论】:

    • 我向他解释了如何避免空引用,因为 op 没有在他调用 sdf.parse(date); 的代码中指定日期值;
    【解决方案3】:
    public class App {
        public static void main(String[] args) {
            String date = "2016/04/26";
            Date enteredDate = null;
            Date matcher = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            try {
                enteredDate = sdf.parse(date);
            } catch (Exception ex) {
                ex.printStackTrace();// enteredDate will be null if date="287686";
            }
    
            try {
                matcher = sdf.parse("2016/04/26");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
            }
    
            if (enteredDate.compareTo(matcher) == 0) {
                System.out.println("enteredDate will be null");
            }
    
            Date currentDate = new Date();
            if (enteredDate.after(currentDate)) {
                System.out.println("after ");
            } else
                System.out.println("before");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2018-04-05
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多