2012-03-01 32 views
0

2つの文字列があり、最初の文字列には実際の日付が含まれ、2つ目の文字列には日付形式が含まれています。2つの日付文字列を比較する方法

両方の文字列を比較したいと思います。

String s1 = "01/02/2012"; 
String s2 = "dd/MM/yyyy"; 
if (s1.equalsIgnoreCase(s2)){ 
    System.out.println("true");} 
else { 
    System.out.println("false");} 

私はすべての文字列メソッドを試してみました(のような(比較)、equalTo()、など):ここに私のコードです。 else部分を常に実行しています。つまり、条件は常に「false」です。

+0

をご比較ください。 's1'が' s2'で指定された 'DateFormat'内にあればtrueを返しますか? – king14nyr

+0

これらの文字列は等しくないので、どのようなものを比較したいですか? –

+1

指定された日付がフォーマット文字列で正しく解析されるかどうかを確認していますか?また、 'DateFormat'と' SimpleDateFormat'のリファレンスドキュメントもチェックしてください。 –

答えて

4

チェック使用してフォーマット

if(isValidDate("01/02/2012")){ 
     System.out.println("true");}else{ 
     System.out.println("false");} 
    } 

    public boolean isValidDate(String inDate) { 

      if (inDate == null) 
       return false; 

      // set the format to use as a constructor argument 
      SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); 

      if (inDate.trim().length() != dateFormat.toPattern().length()) 
       return false; 

      dateFormat.setLenient(false); 

      try { 
       // parse the inDate parameter 
       dateFormat.parse(inDate.trim()); 
      } catch (ParseException pe) { 
       return false; 
      } 
      return true; 
     } 
+0

ありがとう、それは私をひっくり返す! – chanduH

0
// date validation using SimpleDateFormat 
// it will take a string and make sure it's in the proper 
// format as defined by you, and it will also make sure that 
// it's a legal date 

public boolean isValidDate(String date) 
{ 
    // set date format, this can be changed to whatever format 
    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc. 
    // you can read more about it here: 
    // http://java.sun.com/j2se/1.4.2/docs/api/index.html 

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 

    // declare and initialize testDate variable, this is what will hold 
    // our converted string 

    Date testDate = null; 

    // we will now try to parse the string into date form 
    try 
    { 
     testDate = sdf.parse(date); 
    } 

    // if the format of the string provided doesn't match the format we 
    // declared in SimpleDateFormat() we will get an exception 

    catch (ParseException e) 
    { 
     errorMessage = "the date you provided is in an invalid date" + 
           " format."; 
     return false; 
    } 

    // dateformat.parse will accept any date as long as it's in the format 
    // you defined, it simply rolls dates over, for example, december 32 
    // becomes jan 1 and december 0 becomes november 30 
    // This statement will make sure that once the string 
    // has been checked for proper formatting that the date is still the 
    // date that was entered, if it's not, we assume that the date is invalid 

    if (!sdf.format(testDate).equals(date)) 
    { 
     errorMessage = "The date that you provided is invalid."; 
     return false; 
    } 

    // if we make it to here without getting an error it is assumed that 
    // the date was a valid one and that it's in the proper format 

    return true; 

} // end isValidDate 
0

は、以下のようにそれを実行します。

String s1 = "01/02/2012"; 
String s2 = "dd/MM/yyyy"; 
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(s2); 
try { 
    Date date = simpleDateFormat.parse(s1); 
    System.out.println(simpleDateFormat.format(date)); 
    System.out.println("Parse successful. s1 matches with s2"); 
} catch (ParseException e) { 
    System.out.println("Parse failed. s1 differs by format."); 
} 

は注意してください:少し警告を

場合代わりに"01/01/2013"と見なされるため、s1="01/13/2012"の解析は成功しますが、正しくはありません。だからあなたがこれで大丈夫なら、あなた自身の実装を進めてください。

関連する問題