[英]Java Date.getTime() returns an incorrect response for 2022 April
[英]Java Date getTime function returns negative value
我正在将一个String转换为一个日期,然后将其转换为整数值以使日期之间的比较更容易。 这是我的代码:
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
lastModified = "Sun, 02 Oct 2011 18:38:26 GMT";
Date date = format.parse(lastModified);
lastModifiedDate = (int) date.getTime();
然后,我打印机lastModifiedDate,它是(-974253872),我知道它应该是1970年1月1日以毫秒为单位的时间,但它是负值,所以发生了什么?
谢谢你的帮助。
date.getTime()
返回一个long
。 如果将其int
为int
,则由于溢出可能会变为负数。
如果您将代码更改为:
long lastModifiedDate = date.getTime();
你会得到一个正值 - 1317580706000 - 它不能适合int变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.