简体   繁体   English

为什么 java.sql.Timestamp 在 1970-01-01 之后返回负值

[英]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.

相关问题 为什么我的 SQLite 查询将系统日期返回为 1970-01-01? - Why is my SQLite query returning the system date as 1970-01-01? 如何正确地将新的Date(0L)转换为LocalDate(1970-01-01)? - How to properly convert new Date(0L) to LocalDate (1970-01-01)? 如何从Java中的纪元(1970-01-01)获得毫秒? - How do I get milliseconds from epoch (1970-01-01) in Java? 将完整的Java日期(例如,2013年6月19日上午8.00)转换为一个时间(即1970-01-01上午8.00) - Convert a full Java date (e.g. 8.00am 2013-06-19) to just a time (i.e. 8.00am 1970-01-01) 数据类型为日期时间的列的值在 Java 中始终为 1970/01/01 - The value of the column with data type of datetime always is 1970/01/01 in Java JMeter:Sampler结果显示“ Sample Start:1970-01-01 05:30:00 IST” - JMeter: Sampler results shows “Sample Start: 1970-01-01 05:30:00 IST” 为什么java.sql.Date.getTime()对于1970-01-02返回82_800_000而不是86_400_000? - Why does java.sql.Date.getTime() returns 82_800_000 for 1970-01-02 instead of 86_400_000? java.sql.timestamp与日期 - java.sql.timestamp versus date 在java.sql.Timestamp中设置日期时间 - Set date and time in java.sql.Timestamp 计算两个java.sql.Timestamp之间的差异显示负值 - Calculating difference between two java.sql.Timestamp shows negative value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM