【发布时间】:2012-08-11 22:08:20
【问题描述】:
由于某种原因,我只运行了几秒钟,而时钟却得到了 2 个小时。 以下是时间戳和代码的结果:
08-12 01:03:47.858: I/System.out(2856): 1344722627872
08-12 01:03:51.198: I/System.out(2856): 1344722631206
08-12 01:03:54.698: I/System.out(2856): 3334
08-12 01:03:54.758: I/System.out(2856): 02:00:03
public static String getDate(long milliSeconds, String dateFormat) {
DateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime()); }
这是完整的调用代码:
// Click Listener for Check In\Out Button
checkIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Check Out
myVib.vibrate(10);
if (clicked == true) {
timeStampWhenOut = System.currentTimeMillis();
killcheck = true;
checkedStatus = "Check In";
checkIn.setText(checkedStatus);
long getShiftLength = ShiftLength(timeStampWhenIn,
timeStampWhenOut);
System.out.println(timeStampWhenOut);
System.out.println(getShiftLength);
System.out.println(getDate(getShiftLength, "hh:mm:ss"));
clicked = false;
wasShift = true;
// Check In
} else if (clicked == false) {
timeStampWhenIn = System.currentTimeMillis();
System.out.println(timeStampWhenIn);
checkedStatus = "Check Out";
checkIn.setText(checkedStatus);
killcheck = false;
clicked = true;
}
}
});
为什么时间不对?
【问题讨论】:
-
等一下,你是在经过时间吗?你不能这样做 - Calendar.setTimeInMillis 期望从纪元开始的时间。
-
2017年,关注
java.time.Duration。