我有以下内容Long
变量以秒为单位保存纪元值,我试图将其转换为Date
.
val seconds = 1341855763000
val date = Date(TimeUnit.SECONDS.toMillis(seconds))
输出比我预期的要差很多。我哪里做错了?
Actual: Wed Sep 19 05:26:40 GMT+05:30 44491
Expected: Monday July 9 11:12:43 GMT+05:30 2012
输出比我预期的要差很多。我哪里做错了?
Actual: Wed Sep 19 05:26:40 GMT+05:30 44491
Expected: Monday July 9 11:12:43 GMT+05:30 2012
该值已经在milliseconds
并通过使用TimeUnit.SECONDS.toMillis(seconds)
你错误地将它乘以1000
.
通过使用现代日期时间 API https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html:
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
通过使用遗留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
我建议您从过时且容易出错的java.util
日期时间 API 和SimpleDateFormat
to the modern https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html java.time
日期时间API和相应的格式化API(包,java.time.format
)。了解有关现代日期时间 API 的更多信息轨迹:日期时间 https://docs.oracle.com/javase/tutorial/datetime/index.html.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)