我試圖比較Java中的兩個毫秒值。一個來自日曆,另一個來自System.currentTimeMillis()
。但是,日曆中的值似乎總是遠大於來自System.currentTimeMillis()
的值。與日曆的毫秒差距太大
爲了構建日曆,我使用格式dd/MM/yyyy HH:mm
解析日期字符串,並將它的參數部分放入日曆中(爲簡潔起見,省略了try-catch)。
Calendar obCal = null;
//Exception here shows invalid format
DateFormat DF = new SimpleDateFormat("dd/MM/yyyy HH:mm");
DF.parse(Date);
//Example string: 29/11/2011 16:30
String[] parts = Date.split("/");
obCal = Calendar.getInstance();
int Y = Integer.parseInt(parts[2].split(" ")[0]);
int m = Integer.parseInt(parts[1]);
int d = Integer.parseInt(parts[0]);
int H = Integer.parseInt(parts[2].split(" ")[1].split(":")[0]);
int M = Integer.parseInt(parts[2].split(" ")[1].split(":")[1]);
obCal.set(Calendar.YEAR, Y);
obCal.set(Calendar.MONTH, m);
obCal.set(Calendar.DAY_OF_MONTH, d);
obCal.set(Calendar.HOUR_OF_DAY, H);
obCal.set(Calendar.MINUTE, M);
obCal.set(Calendar.SECOND, 0);
obCal.set(Calendar.MILLISECOND, 0);
檢查日曆後顯示我它正在收到正確的日期和時間。然後我得到了毫秒值,因爲從obCal.getTimeInMillis()
時代並將其存儲爲秒數爲long
long stamp = obCal.getTimeInMillis()/1000L;
當我到達日期字符串表示的時間,並從日曆比較時間戳時間戳System.currentTimeMillis()
,我發現前者要大得多。當前時間和日曆時間(curTime-calTime
)之間的差異通常在-2,500,000
的地方
可能是什麼原因造成的? 謝謝
你爲什麼不只需使用'Calendar#setTime(...)'? – Thomas
爲什麼不使用'obCal.setTime(DF.parse(date));'? – BalusC