由于某种原因,我只运行了2秒钟就获得了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;

            }
        }
    });


为什么时间不对?

最佳答案

假设3334是经过的时间,则将日历设置为该时期以来的3.334秒(大约是1970年12月31日格林威治标准时间午夜)。然后,您要格式化该日历。它告诉您,您所在的时区是凌晨2点加3秒,我猜您所在的时区是GMT + 2。

不要在经过的时间中使用Calendar和SimpleDateFormatter-这不是正确的工具。请改为查看Apache-Commons中的DurationFormatUtils。

09-10 08:47
查看更多