查看此方法以查看支持哪些字段。您将找到LocalDateTime
:
•NANO_OF_SECOND
•NANO_OF_DAY
•MICRO_OF_SECOND
•MICRO_OF_DAY
•MILLI_OF_SECOND
•MILLI_OF_DAY
•SECOND_OF_MINUTE
•SECOND_OF_DAY
•MINUTE_OF_HOUR
•MINUTE_OF_DAY
•HOUR_OF_AMPM
•CLOCK_HOUR_OF_AMPM
•HOUR_OF_DAY
•CLOCK_HOUR_OF_DAY
•AMPM_OF_DAY
•DAY_OF_WEEK
•ALIGNED_DAY_OF_WEEK_IN_MONTH
•ALIGNED_DAY_OF_WEEK_IN_YEAR
•DAY_OF_MONTH
•DAY_OF_YEAR
•EPOCH_DAY
•ALIGNED_WEEK_OF_MONTH
•ALIGNED_WEEK_OF_YEAR
•MONTH_OF_YEAR
•PROLEPTIC_MONTH
•YEAR_OF_ERA
•YEAR
•ERA
当然,不支持INSTANT_SECONDS字段,因为a LocalDateTime
不能引用任何绝对(全局)时间戳。但是有帮助的是字段EPOCH_DAY,该字段统计从1970-01-01开始的经过天数。类似的想法适用于该类型LocalDate
(具有更少支持的字段)。
如果要获取不存在的millis-since-unix-epoch字段,则还需要时区以将本地类型转换为全局类型。这种转换可以简单得多,请参阅其他SO-posts。
回到您的问题和代码中的数字:
The result 1605 is correct
=> (2014 - 1970) * 365 + 11 (leap days) + 31 (in january 2014) + 3 (in february 2014)
The result 71461 is also correct => 19 * 3600 + 51 * 60 + 1
自1970-01-01T00:00:00以来的16105L * 86400 + 71461 = 1391543461秒(注意,无时区)然后您可以减去时区偏移量(请注意,如果以毫秒为单位,则可能乘以1000)。
给定时区信息后更新:
local time = 1391543461 secs
offset = 3600 secs (Europe/Oslo, winter time in february)
utc = 1391543461 - 3600 = 1391539861
作为具有两种等效方法的JSR-310代码:
long secondsSinceUnixEpoch1 =
LocalDateTime.of(2014, 2, 4, 19, 51, 1).atZone(ZoneId.of("Europe/Oslo")).toEpochSecond();
long secondsSinceUnixEpoch2 =
LocalDate
.of(2014, 2, 4)
.atTime(19, 51, 1)
.atZone(ZoneId.of("Europe/Oslo"))
.toEpochSecond();