2012-03-01 61 views
0

我有兩個字符串,第一個包含實際日期,第二個包含日期格式。如何比較兩個日期字符串

我想比較兩個字符串。這裏是我的代碼:

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

我試過所有的字符串方法(如compare(),equalTo()等)。它始終執行else部分,即條件始終爲「假」。

+0

比較?如果's1'在's2'中指定的'DateFormat'中,你的意思是返回true嗎? – king14nyr 2012-03-01 18:39:05

+0

這些字符串不相等 - 那麼您喜歡什麼比較? – 2012-03-01 18:39:28

+1

你問是否給定的日期將正確解析格式字符串?同時檢查'DateFormat'和'SimpleDateFormat'的參考文檔。 – 2012-03-01 18:39:52

回答

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 2012-03-02 17:20:02

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

請注意:一個小小的警告

如果你有s1="01/13/2012"解析會獲得成功,雖然它不正確,因爲它會將其視爲"01/01/2013"。所以,如果你對此感到滿意,那麼繼續,否則繼續你自己的實現。