【问题标题】:Java\Android timestamp returns wrong hour valueJava\Android 时间戳返回错误的小时值
【发布时间】: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

标签: java android timestamp


【解决方案1】:

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

不要使用 Calendar 和 SimpleDateFormatter 来计算已用时间 - 这不是工作的正确工具。请查看 Apache-Commons 中的 DurationFormatUtils。

【讨论】:

  • 那么我获取此信息的最佳方式是什么?我需要知道从用户“点击”到他点击“退出”已有多长时间
  • @Yosi199:您正在尝试格式化持续时间(经过了多少毫秒)-但这不是日期和时间。将`timeStampOut` 和timeStampIn 格式化为日期/时间值是合理的(尽管我建议使用HH 而不是hh),但您不应该尝试以这种方式格式化getShiftLength
  • 谢谢你们,我现在更好地理解了为什么它是错误的。你介意给我一个例子,说明什么是正确的方法吗?
  • @Yosi199,看看Android DateUtils.formatElapsedTime developer.android.com/reference/android/text/format/…
【解决方案2】:

代码对我来说运行良好。你能显示你在哪里调用 getDate 方法吗?这是我如何使用它:

System.out.println(getDate(System.currentTimeMillis(), "hh:mm:ss"));

这是记录正确的时间(对我来说当前是 6:10:29)

编辑: 你为什么还要在中间使用日历?您将您的方法交给毫秒,它会放入日历中,然后立即取出。

public static String getDate(long milliSeconds, String dateFormat)  {

    DateFormat formatter = new SimpleDateFormat(dateFormat);

     return formatter.format(milliSeconds);
}

^ 这样会好很多

【讨论】:

  • I\O 输出的最后一行是对这个方法的调用。您可以看到它只运行了几秒钟(根据时间戳),但它打印出来的好像已经过了 2 小时。
  • 我认为这一定与格林威治标准时间有关,因为我在格林威治标准时间+3 并且我玩过一些格林威治标准时间设置并且小时值已更改。但我不知道如何在我的代码中解决这个问题
  • Calendar.getInstance() 应该返回与您的系统处于同一时区的日历。你能在你调用它的地方显示代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 2011-09-26
  • 1970-01-01
  • 2021-09-15
相关资源
最近更新 更多