java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒)

转载自:LocalDateTime To Timestamp Epoch Seconds And Milliseconds

Convert LocalDateTime to seconds since January 1, 1970, 00:00:00 GMT

val now = LocalDateTime.now(ZoneOffset.UTC)

// LocalDateTime to epoch seconds
val seconds = now.atZone(ZoneOffset.UTC).toEpochSecond())

// Epoch seconds to LocalDateTime
val newNow = LocalDateTime.ofInstant(Instant.ofEpochSecond(seconds), ZoneOffset.UTC)

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), ZoneOffset.UTC)

 

转载于:https://www.cnblogs.com/passedbylove/p/10646605.html

你可能感兴趣的:(java8 LocalDateTime转unix时间戳(带毫秒,不带毫秒))