2011-05-24 65 views
0

當我在eclipse中通過編譯器合規性級別6.0編譯此代碼時,它編譯正常,但是當我更改編譯器級別4.0時,此代碼顯示錯誤,即不兼容條件操作數類型String和Integer。這是什麼問題,並改變了這種代碼需要得到同樣的結果使用java版本4.0編譯

Calendar cal = Calendar.getInstance(); 
String timeStr = (calendar.get(Calendar.HOUR_OF_DAY) < 10 ?  
    "0" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) :  
    Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY))) + ":" + 
     (calendar.get(Calendar.MINUTE) < 10 ? 
     "0" + Integer.valueOf(calendar.get(Calendar.MINUTE)) : 
     Integer.valueOf(calendar.get(Calendar.MINUTE))); 
+0

它只是因爲追加0,如果我們是小於10 – 2011-05-24 05:45:12

+0

考慮第一小時和分鐘拉出到變量? – 2011-05-24 05:57:30

回答

1

爲什麼不直接使用SimpleDateFormat得到相同的結果,就像這樣:

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
String timeStr = sdf.format(cal.getTime()); 

它與編譯器合規水平集到1.4。

編輯:供參考,如果SimpleDateFormat不存在,我建議你寫它是這樣的:

int hour = calendar.get(Calendar.HOUR_OF_DAY); 
int minute = calendar.get(Calendar.MINUTE); 
String timeStr = (hour < 10 ? "0" : "") + hour + ":" 
     + (minute < 10 ? "0" : "") + minute; 
+0

是的,你是對的,它給出了我需要的結果。謝謝 – 2011-05-24 05:50:41

2

的問題是,如果HOUR_OF_DAY是< 10,條件運算符將評估爲一個字符串,如果不是,它將評估爲一個整數。在4.0,這被認爲是一個錯誤(因爲編譯器有益指出。)

爲了解決這個問題,你應該只使用一個內置的日期格式化,而不是複雜的邏輯並不總是編譯。例如:

SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm"); 
String theCurrentTime = dateFormatter.format(cal.getTime()); 

如果你真的需要保留的邏輯,不過,你可以通過把"" +Integer.valueOf()呼叫前在有條件的經營者的:分支修復它。雖然不是最好的選擇。

0

大多數人類程序員可能會被你的代碼弄糊塗;我猜測編譯器也是。 :-)

稍好的風格,但沒有直接回答你的問題應該是:

Calendar cal = Calendar.getInstance(); 
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
String timeStr = sdf.format(cal); 
0

快速修復:添加一些空字符串(如果你想保持你的代碼):

String timeStr = 
    (calendar.get(Calendar.HOUR_OF_DAY) < 10 ?  
     "0" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) :  
     "" + Integer.valueOf(calendar.get(Calendar.HOUR_OF_DAY))) 
    + ":" + 
    (calendar.get(Calendar.MINUTE) < 10 ? 
     "0" + Integer.valueOf(calendar.get(Calendar.MINUTE)) : 
     "" + Integer.valueOf(calendar.get(Calendar.MINUTE))); 

(但我也推薦簡單的格式化程序 - 你有很多可避免的隱藏轉換,從String到Integer到int並返回String)

1

真正的問題是那些Integer.valueOf(...)調用將int值轉換爲Integer對象。這沒有做任何有用的事情。當編譯器級別爲4.0時,會出現編譯錯誤,因爲Java 1.4.x不支持拆箱,因此三元運算符的操作數不兼容。

如果我們剔除了Integer.valueOf東西,它看起來像這樣:

String timeStr = 
    (calendar.get(Calendar.HOUR_OF_DAY) < 10 ?  
     "0" + calendar.get(Calendar.HOUR_OF_DAY) :  
     calendar.get(Calendar.HOUR_OF_DAY)) + 
    ":" + 
    (calendar.get(Calendar.MINUTE) < 10 ? 
     "0" + calendar.get(Calendar.MINUTE) : 
     calendar.get(Calendar.MINUTE)); 

這仍然不能編譯,因爲該類型的第二和三元運算符的第三個操作數不匹配。這是很容易解決:

String timeStr = 
    (calendar.get(Calendar.HOUR_OF_DAY) < 10 ?  
     "0" + calendar.get(Calendar.HOUR_OF_DAY) :  
     "" + calendar.get(Calendar.HOUR_OF_DAY)) + 
    ":" + 
    (calendar.get(Calendar.MINUTE) < 10 ? 
     "0" + calendar.get(Calendar.MINUTE) : 
     "" + calendar.get(Calendar.MINUTE)); 

(編譯器可能會優化掉的串聯與"" ...)

最後,你可以重構它進一步把這個:

int hours = calendar.get(Calendar.HOUR_OF_DAY); 
int minutes = calendar.get(Calendar.MINUTE); 
String timeStr = 
     (hours < 10 ? "0" : "") + hours + ":" + 
     (minutes < 10 ? "0" : "") + minutes;