[英]why java.sql.Timestamp return negative value when the date is after 1970-01-01
Timestamp timestamp = Timestamp.valueOf("1970-01-01 01:00:00");
System.out.println(timestamp.getTime());
Any idea this code returns -25200000?知道此代码返回-25200000 吗? I thought time after 1970-01-01 00:00:00 will be positive.
我认为 1970-01-01 00:00:00 之后的时间会是积极的。
You are using a terrible date-time class that was supplanted years ago by the modern java.time classes defined in JSR 310. Never use java.sql.Timestamp
. You are using a terrible date-time class that was supplanted years ago by the modern java.time classes defined in JSR 310. Never use
java.sql.Timestamp
.
Parse your input as a LocalDateTime
because it lacks an indicator of time zone or offset-from-UTC.将您的输入解析为
LocalDateTime
,因为它缺少时区指示符或与 UTC 的偏移量。 Replace the SPACE in middle with a T
to comply with ISO 8601 standard for date-time formats.将中间的空格替换为
T
以符合日期时间格式的 ISO 8601 标准。
String input = "1970-01-01 01:00:00".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
Apparently you mean for that to represent a moment in UTC.显然,您的意思是代表 UTC 中的一个时刻。 Apply an offset to get an
OffsetDateTime
.应用偏移量以获得
OffsetDateTime
。
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
Get a count of milliseconds since the epoch reference of first moment of 1970 in UTC.获取自 1970 年 UTC 第一时刻的纪元参考以来的毫秒数。
long millis = odt.toInstant().toEpochMilli() ;
3600000
360万
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.