import java.time.LocalDateTime; import java.time.Instant; public class Main { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); Instant instant = localDateTime.toInstant(); long milliseconds = instant.toEpochMilli(); System.out.println("Milliseconds: " + ...
java Instant instant = zonedDateTime.toInstant(); 调用Instant对象的toEpochMilli()方法获取自1970年1月1日以来的毫秒数:java long milliseconds = instant.toEpochMilli(); 将以上步骤整合在一起,你可以得到以下完整的代码示例: java import java.time.LocalDateTime; import java.time.ZoneId; import java....
// Epoch seconds to LocalDateTime 1. 2. 3. 4. 5. 6. Convert LocalDateTime to milliseconds since January 1, 1970, 00:00:00 GMT // LocalDateTime to epoch milliseconds val milliseconds = now.atZone(ZoneOffset.UTC)?.toInstant()?.toEpochMilli() // epoch mi...
//LocalDateTime to epoch milliseconds Instantinstant=localDateTime.atZone(ZoneId.systemDefault()).toInstant(); longtimeInMillis=instant.toEpochMilli(); System.out.println(timeInMillis); } } 输出 1573823730 1573803930000 1.1 LocalDateTime 转换为Epoch秒 LocalDateTime提供toEpochSecond()方法将本地日期时间转换...
Convert LocalDateTime to milliseconds since January 1, 1970, 00:00:00 GMT // LocalDateTime to epoch milliseconds val milliseconds = now.atZone(ZoneOffset.UTC)?.toInstant()?.toEpochMilli()// epoch milliseconds to LocalDateTime val newNow = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ...
java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒) 转载自:LocalDateTime To Timestamp Epoch Seconds And Milliseconds ConvertLocalDateTimeto seconds sinceJanuary 1, 1970, 00:00:00 GMT val now =LocalDateTime.now(ZoneOffset.UTC)//LocalDateTime to epoch secondsval seconds =now.atZone(ZoneOffset.UTC).to...
java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒) 转载自:LocalDateTime To Timestamp Epoch Seconds And Milliseconds ConvertLocalDateTimeto seconds sinceJanuary 1, 1970, 00:00:00 GMT val now =LocalDateTime.now(ZoneOffset.UTC)//LocalDateTime to epoch secondsval seconds =now.atZone(ZoneOffset.UTC).to...
让我们把这些放在一边,但你应该知道这些情况是存在的。 无论如何,如果您可以获得有效的 ZonedDateTime ,您可以将其转换为自纪元以来的毫秒数,如下所示: long millis = zdt.toInstant().toEpochMilli(); 原文由 Stuart Marks 发布,翻译遵循 CC BY-SA 3.0 许可协议 有用 回复 ...
longmilliSeconds = Instant.now().toEpochMilli();//输出:1682354579089 //3、毫秒转LocalDateTime LocalDateTime dateTimeFromSecond = LocalDateTime.ofInstant( Instant.ofEpochSecond(seconds), ZoneOffset.ofHours(8) ); //4、时间戳转LocalDateTime LocalDateTime dateTimeFromMilliSecond = LocalDateTime.ofInstant( ...
@TestpublicvoidlocalTimeTest3(){// 获得当前时间LocalDateTime localDateTime = LocalDateTime.now();// 将当前时间转为时间戳longmilliseconds = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();// 1580707268System.out.println(milliseconds/1000); ...