我试图在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);
Run Code Online (Sandbox Code Playgroud)
在此之后检查日历显示我正在接收正确的日期和时间.然后我获得自纪元以来的毫秒值obCal.getTimeInMillis(),并将其存储为秒数long
long stamp = obCal.getTimeInMillis() / 1000L;
Run Code Online (Sandbox Code Playgroud)
当我到达日期字符串所代表的时间,并将日历中的时间戳与时间戳进行比较时System.currentTimeMillis(),我发现前者的距离要大得多.当前时间和日历时间(curTime-calTime)之间的差异通常是在某个地区-2,500,000
可能是什么导致了这个?谢谢
| 归档时间: |
|
| 查看次数: |
799 次 |
| 最近记录: |