2013-11-01 217 views
7

嗨即時嘗試比較用戶輸入日期(作爲一個字符串)與當前日期,以確定日期是否早或更早。比較用戶輸入日期和當前日期

我當前的代碼是

String date; 
Date newDate; 
Date todayDate, myDate;  
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); 

while(true) 
{ 
    Scanner s = new Scanner (System.in); 
    date = s.nextLine(); 
    Calendar cal = Calendar.getInstance(); 
    try { 
     // trying to parse current date here 
     // newDate = dateFormatter.parse(cal.getTime().toString()); //throws exception 

     // trying to parse inputted date here 
     myDate = dateFormatter.parse(date); //no exception 
    } catch (ParseException e) { 
     e.printStackTrace(System.out); 
    } 

} 

林試圖讓這兩個用戶輸入日期和當前日期到兩個Date對象,這樣我可以使用Date.compareTo(),以簡化比較日期。

我能解析用戶輸入字符串到Date對象中。但是,當前日期cal.getTime()。toString()不會解析到Date對象中,因爲它是無效字符串。

如何去做這件事?在此先感謝

+0

只是爲了澄清 - 你想要的東西,將返回TRUE;如果輸入的日期是昨天,但'FALSE'如果今天輸入的日期爲。是對的嗎? –

+0

是的即時即時嘗試做什麼。假設即時在運行該程序,newDate = new Date();給我(2013年11月1日晚8點),如果用戶輸入2013年11月1日,它應該返回錯誤,因爲輸入日期是今天(不管時間)。 – kype

回答

6

您可以獲得當前Date有:

todayDate = new Date(); 

編輯:既然你需要的日期不考慮時間分量比較,我建議你看這個:How to compare two Dates without the time portion?

儘管一個答案的「糟糕表現」,其實我挺喜歡它:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
sdf.format(date1).equals(sdf.format(date2)); 

在你的情況,你已經有了:

SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); 

,所以我會考慮(爲簡單起見,而非性能):如您所願

todayDate = dateFormatter.parse(dateFormatter.format(new Date())); 
+0

謝謝。將當前時間信息存入日期嗎? – kype

+0

是的,如果你需要,你可以刪除它。 – rolfl

+0

我必須把它放到日曆才能修改時間嗎?由於Date.setHour已被折舊 – kype

0

Fomat不符合。

Calendar cal = Calendar.getInstance(); 
     System.out.println(cal.getTime().toString()); 

輸出:週五年11月1 13時46分52秒EET 2013

1

你可以做到這一點。

// Make a Calendar whose DATE part is some time yesterday. 
Calendar cal = Calendar.getInstance(); 
cal.roll(Calendar.DATE, -1); 

if (myDate.before(cal.getTime())) { 
    // myDate must be yesterday or earlier 
} else { 
    // myDate must be today or later 
} 

這不要緊,cal有一個時間分量,因爲myDate沒有。因此,當您比較它們時,如果calmyDate是相同的日期,則時間分量將使cal晚於myDate,而不管時間分量是什麼。

+0

錯誤,不應該正常工作,因爲Date.before()需要一個日期作爲參數,而不是一個日曆對象 – kype

+0

對不起。現在修復。 –

1

創建一個新的Date()將爲您提供一個包含當前日期的Date對象。

這樣:

Date currentDate = new Date(); 

將做的工作

3

這裏是檢查代碼,如果給定的日期,時間較大,則當前日期,time.Below方法採用perticular日期時間字符串參數和返回true(如果提供)日期時間大於當前日期時間。#thmharsh

private boolean isExpire(String date){ 
    if(date.isEmpty() || date.trim().equals("")){ 
     return false; 
    }else{ 
      SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss a"); // Jan-20-2015 1:30:55 PM 
       Date d=null; 
       Date d1=null; 
      String today= getToday("MMM-dd-yyyy hh:mm:ss a"); 
      try { 
       //System.out.println("expdate>> "+date); 
       //System.out.println("today>> "+today+"\n\n"); 
       d = sdf.parse(date); 
       d1 = sdf.parse(today); 
       if(d1.compareTo(d) <0){// not expired 
        return false; 
       }else if(d.compareTo(d1)==0){// both date are same 
          if(d.getTime() < d1.getTime()){// not expired 
           return false; 
          }else if(d.getTime() == d1.getTime()){//expired 
           return true; 
          }else{//expired 
           return true; 
          } 
       }else{//expired 
        return true; 
       } 
      } catch (ParseException e) { 
       e.printStackTrace();      
       return false; 
      } 
    } 
} 


    public static String getToday(String format){ 
    Date date = new Date(); 
    return new SimpleDateFormat(format).format(date); 
} 
相關問題