2016-04-25 12 views
-5

ユーザーが入力した日付が今日以降であるかどうかを確認しようとしています。ここに私のコードです:今日の日付が後であるかどうかを確認する方法java

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

日付は、 "2016/04/26"という形式のユーザー日付を持つ変数です。いくつかのデバッグを行った後、私は、enterDateとcurrentDateがnullであることを発見しました。これはなぜですか? おかげ

+4

currentDateをnullにすることはできません。 [mcve]を作成してください。 – assylias

+2

'Date currentDate = new Date()'の後の変数 'currentDate'が' null'ですか? (ヒント:それはできません...) – Seelenvirtuose

+0

私のコンピュータでは、日付が '" 2016/04/26 "'で、うまくいきます。 – Hackerdarshi

答えて

1

です。ただし、sdf.parse(date)が抑制された例外をスローすると、入力された日付は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
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"); 
    } 
} 
-2

JavaのDateクラスは、前に持って、あなたは良い例を使用することができる方法の後のコメントで述べたように、それはDateオブジェクトがnull参照を持つことはできません。この

`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(); 
    } 
} 
}` 
+0

オペアンプは既に知っています - 質問はヌル参照についてです... – assylias

+0

私は彼がnull参照を避ける方法を説明しました。オペレーションは彼のコードで日付値を指定しなかったので、sdf.parse日付); –

関連する問題