时间戳值不正确 - c.getTimeInMills()

2022-01-13 00:00:00 timestamp android java

我有一个正常工作的 DatePicker,但是,当我将日期转换为时间戳时,时间戳完全错误.请参阅下面的代码:

I have a DatePicker which is working correctly, however, when I convert the date to a timestamp, the timestamp is completely wrong. Please see my code below:

final Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR); // current year
                int mMonth = c.get(Calendar.MONTH); // current month
                int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                timestamp = c.getTimeInMillis();

c.getTimeInMillis 部分似乎没有得到正确的日期.有谁知道如何解决这个问题?

The part c.getTimeInMillis does not seem to be getting the correct date. Does anyone know how to fix this?

预计日期是今天,我得到的是 5/31/51128, 4:47:42 PM.我已经弄清楚为什么它显示错误的日期:生成的时间戳末尾有 3 个额外的数字.我不知道为什么,或者如何解决这个问题.

Expected date was today’s date and I'm getting 5/31/51128, 4:47:42 PM. I have figured out why it is showing the wrong date: the generated timestamp has 3 extra digits at the end. I don't know why though, or how to fix this.

推荐答案

我怀疑您正在将 timestamp(以毫秒为单位)提供给需要秒数的例程.那些3 个额外数字"是时间戳的毫秒部分.如果您觉得这听起来不错,请在传递之前将 timestamp 除以 1000.

I suspect you are feeding timestamp, which is in milliseconds, to a routine that wants seconds. Those "3 extra digits" are the milliseconds part of the timestamp. If this sounds right to you, divide timestamp by 1000 before passing it on.

相关文章