2014-11-01 71 views
0

我設法讓當地的時間,但現在我想用它在if語句是這樣的:檢查當地時間if語句

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00")); 
    Date currentLocalTime = cal.getTime(); 
    DateFormat date = new SimpleDateFormat("HH:mm"); 
    // you can get seconds by adding "...:ss" to it 
    date.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); 
    String localTime = date.format(currentLocalTime); 

    Log.w(localTime, "1"); 

    if (localTime == "12:11") { 
    Log.w("Yes", "this is"); 
    } 
    else { 
    Log.w("jhlgsaljfd", "kajfhsd"); 
    } 

但是,這是行不通的,如何檢查本地時間在if-else語句中?

+0

你在「localTime」中得到了什麼值?你在這個變量中得到正確的時間嗎?顯示你的logcat .. – stacktry 2014-11-01 11:20:08

+0

順便說一句,你不知道如何使用日誌。 – 2014-11-01 11:35:57

回答

1

嘗試equals,而不是==,它應該是:

if (localTime.equals("12:11")){ 
    Log.w("Yes", "this is"); 
} 
else { 
    Log.w("jhlgsaljfd", "kajfhsd"); 
} 
0

可以使用==字符串比較不會做ü需要使用這樣

if("12:11".equals(localTime)) 

你可以使用這個太

if (localTime.equals("12:11")) 

但有時這可能會導致NullPointerException。(Wh你的字符串爲空或未被初始化)