How to check if a date is after today java

2019-09-25 11:22发布

问题:

Im trying to check whether a date entered by the user is after todays date. Here is my code:

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

Date is a variable with the user date in the format "2016/04/26". After doing some debugging i found that enteredDate and currentDate are null. Any ideas why this is? Thanks

回答1:

As mentioned in comments, it's not possible that Date object will have null reference. However if sdf.parse(date) throws an exception which is suppressed then enteredDate could be null.

 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:

The java date class has before and after method that you can use a good example is 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();
    }
}
}`


回答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");
    }
}