LocalDateTime 的长时间戳
long timestamp to LocalDateTime
我有一个很长的时间戳 1499070300(相当于 2017 年 7 月 3 日星期一 16:25:00 +0800)但是当我将它转换为 LocalDateTime 时我得到 1970-01-18T16:24:30.300
这是我的代码
long test_timestamp = 1499070300;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
尝试使用 Instant.ofEpochMilli()
或 Instant.ofEpochSecond()
方法 -
long test_timestamp = 1499070300L;
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp ), TimeZone
.getDefault().toZoneId());
您需要以毫秒为单位传递时间戳:
long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp),
TimeZone.getDefault().toZoneId());
System.out.println(triggerTime);
结果:
2017-07-03T10:25
或使用 ofEpochSecond
代替:
long test_timestamp = 1499070300L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
TimeZone.getDefault().toZoneId());
System.out.println(triggerTime);
结果:
2017-07-03T10:25
尝试以下..
long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
默认情况下,如果 1499070300000
在 end.Also 中不包含 l,则它是 int 传递时间(以毫秒为单位)。
您的问题是时间戳不是以毫秒为单位,而是以纪元日期开始的秒数表示。将时间戳乘以 1000 或使用 Instant.ofEpochSecond()
.
如果您使用的是 Android threeten back 端口,那么您需要的线路是这样的
LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())
简单直接的解决方案将 (KOTLIN)
val timeStamp:Long=559585985988
val sdf = SimpleDateFormat("hh:mm:ss a - MMM dd,yyyy", Locale.getDefault())
val tz = TimeZone.getDefault()
val now = Date()
val offsetFromUtc = tz.getOffset(now.time)
val localeTimeStr = sdf.format(timeStamp + offsetFromUtc) //add the offset to get the local time from the epoch timestamp
我有一个很长的时间戳 1499070300(相当于 2017 年 7 月 3 日星期一 16:25:00 +0800)但是当我将它转换为 LocalDateTime 时我得到 1970-01-18T16:24:30.300
这是我的代码
long test_timestamp = 1499070300;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
尝试使用 Instant.ofEpochMilli()
或 Instant.ofEpochSecond()
方法 -
long test_timestamp = 1499070300L;
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp ), TimeZone
.getDefault().toZoneId());
您需要以毫秒为单位传递时间戳:
long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp),
TimeZone.getDefault().toZoneId());
System.out.println(triggerTime);
结果:
2017-07-03T10:25
或使用 ofEpochSecond
代替:
long test_timestamp = 1499070300L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
TimeZone.getDefault().toZoneId());
System.out.println(triggerTime);
结果:
2017-07-03T10:25
尝试以下..
long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
默认情况下,如果 1499070300000
在 end.Also 中不包含 l,则它是 int 传递时间(以毫秒为单位)。
您的问题是时间戳不是以毫秒为单位,而是以纪元日期开始的秒数表示。将时间戳乘以 1000 或使用 Instant.ofEpochSecond()
.
如果您使用的是 Android threeten back 端口,那么您需要的线路是这样的
LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())
简单直接的解决方案将 (KOTLIN)
val timeStamp:Long=559585985988
val sdf = SimpleDateFormat("hh:mm:ss a - MMM dd,yyyy", Locale.getDefault())
val tz = TimeZone.getDefault()
val now = Date()
val offsetFromUtc = tz.getOffset(now.time)
val localeTimeStr = sdf.format(timeStamp + offsetFromUtc) //add the offset to get the local time from the epoch timestamp