英文:
Time conversion from seconds to date issue
问题
我有以下的Long
变量,其中保存着以秒为单位的时间戳,我想要将其转换为一个Date
对象。
val seconds = 1341855763000
val date = Date(TimeUnit.SECONDS.toMillis(seconds))
输出的结果与我预期的相差甚远。我做错了什么?
实际输出:Wednesday, September 19, 44491 5:26:40 AM GMT+05:30
预期输出:Monday, July 9, 2012 11:12:43 AM GMT+05:30
英文:
I have the following Long
variable holding epoch value in seconds, which I'm trying to convert into a Date
.
val seconds = 1341855763000
val date = Date(TimeUnit.SECONDS.toMillis(seconds))
The output is way off than I expected. Where did I go wrong?
Actual: Wed Sep 19 05:26:40 GMT+05:30 44491
Expected: Monday July 9 11:12:43 GMT+05:30 2012
答案1
得分: 7
以下是翻译好的部分:
> 输出结果与我的预期相差甚远。我哪里做错了?
实际结果:Wed Sep 19 05:26:40 GMT+05:30 44491
预期结果:Monday July 9 11:12:43 GMT+05:30 2012
这个值已经是以 毫秒
为单位,通过使用 TimeUnit.SECONDS.toMillis(seconds)
,你错误地将它乘以了 1000
。
使用[现代日期时间 API][1]:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.ofEpochMilli(1341855763000L);
System.out.println(instant);
}
}
输出结果:
2012-07-09T17:42:43Z
使用传统的 java.util.Date
:
import java.util.Date;
public class Main {
public static void main(String[] args) {
System.out.println(new Date(1341855763000L));
}
}
输出结果:
Mon Jul 09 18:42:43 BST 2012
我建议你从过时且容易出错的 `java.util` 日期时间 API 和 `SimpleDateFormat` 切换到[现代的](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) `java.time` 日期时间 API 以及相应的格式化 API(包名为 `java.time.format`)。从**[教程:日期时间](https://docs.oracle.com/javase/tutorial/datetime/index.html)**中了解有关现代日期时间 API 的更多信息。
[1]: https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html
<details>
<summary>英文:</summary>
> The output is way off than I expected. Where did I go wrong?
Actual: Wed Sep 19 05:26:40 GMT+05:30 44491
Expected: Monday July 9 11:12:43 GMT+05:30 2012
The value is already in `milliseconds` and by using `TimeUnit.SECONDS.toMillis(seconds)` you are wrongly multiplying it by `1000`.
## By using the [modern date-time API][1]:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.ofEpochMilli(1341855763000L);
System.out.println(instant);
}
}
**Output:**
2012-07-09T17:42:43Z
## By using legacy `java.util.Date`:
import java.util.Date;
public class Main {
public static void main(String[] args) {
System.out.println(new Date(1341855763000L));
}
}
**Output:**
Mon Jul 09 18:42:43 BST 2012
I recommend you switch from the outdated and error-prone `java.util` date-time API and `SimpleDateFormat` to the [modern](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) `java.time` date-time API and the corresponding formatting API (package, `java.time.format`). Learn more about the modern date-time API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.
[1]: https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html
</details>
# 答案2
**得分**: 6
你所拥有的值不是以秒为单位,而是以毫秒为单位。移除“秒转毫秒”的转换。
val milliSeconds = 1341855763000
val date = Date(milliSeconds)
<details>
<summary>英文:</summary>
The value you have is not in seconds but in milliseconds. Remove the "seconds to millis" conversion.
val milliSeconds = 1341855763000
val date = Date(milliSeconds)
答案3
得分: 4
Your value : 1341855763000 is not in seconds, it is in milliseconds.
The current timestamp is :
new Date().getTime() =>
1598612990351
Same number of digits than :
1341855763000
If you multiply 1341855763000 by 1000 (as you say), it gives the year :
44491 after JC
Have a good day
英文:
Your value : 1341855763000 is not in seconds, it is in milliseconds.
The current timestamp is :
new Date().getTime() =>
1598612990351
Same number of digits than :
1341855763000
If you multiply 1341855763000 by 1000 (as you say), it gives the year :
44491 after JC
Have a good day
答案4
得分: 3
我认为你的时间实际上是以毫秒为单位的。如果我使用这个网站将 1341855763000
进行转换,它会给我你期望的时间,还有这个:
fun main() {
val millis = 1341855763000
val date = Date(millis)
println(date)
}
或者你也可以使用秒数:
fun main() {
val seconds = 1341855763L
val date = Date(TimeUnit.SECONDS.toMillis(seconds))
println(date)
}
只需要将数值除以 1000
。
英文:
I think your time is actually in milliseconds. If I convert 1341855763000
using this website it gives me your expected time and this as well:
fun main() {
val millis = 1341855763000
val date = Date(millis)
println(date)
}
Alternatively, you can also use seconds:
fun main() {
val seconds = 1341855763L
val date = Date(TimeUnit.SECONDS.toMillis(seconds))
println(date)
}
Just divide by 1000
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论