2012-07-31 36 views
0

比較我有這個CalendarView低於安卓:getDat()當前日期

enter image description here

,如果我點擊的那一天也就是今天的日期會彈出一個對話框..但這個日曆不會彈出,如果你點擊一個日期CurrentDate之前它只會說你不能在當前日期/時間之前添加事件,並且運行良好,如果我點擊今天之後的任何一天,但是當我點擊日期今天我會說一個消息就像當我點擊日期之前...

這是我的代碼比較日期

long currentTime; 
Date curr = new Date(); 
currentTime = curr.getTime(); 
if(cw.getDate() >= currentTime || formatDate(cw.getDate()) == formatDate(currentTime)) 
{ 
      Dialog d = new Dialog(ScheduleActivity.this); 
      //some code for setting up the dialog 
      d.show(); 

} 
else 
{ 
     Toast toast = Toast.makeText(ScheduleActivity.this, Long.toString(cw.getDate())+" || "+Long.toString(currentTime),10000); 
     toast.show(); 

     Log.d("timecheck", formatDate(cw.getDate())+" || "+ formatDate(currentTime)); 
} 

,我用了formatDate與MM/DD/YYYY的格式只是兩個日期比較的Cuz單獨比較時間戳仍不(使用「> =」)

順便說一句敬酒不工作被改變,所以我可以看到什麼值被比較......在毫秒中,他們是不是真的,但當我格式化爲MM/dd/yy應該是它會看到它一樣...但它不會通過如果部分它會去別的部分..我登錄下面的logcat中,logcat中的formattedDate ..

logcat的日誌 enter image description here

回答

1

您將需要使用equals,而不是==比較來自formatDate返回的字符串:

if(cw.getDate() >= currentTime || formatDate(cw.getDate()).equals(formatDate(currentTime))) 
+0

噢是的,我忘了字符串也是一個對象..謝謝你!讚賞它..它工作得很好! – lemoncodes 2012-07-31 14:02:49

0

因爲你是比較所有輸入當前日期嘗試進入其他值你的彈出窗口沒有出現比較通過添加以前的日期,並再次嘗試它會工作:)