到 LocalDateTime 的长时间戳

我有一个很长的时间戳1499070300(相当于 Mon,03 Jul 201716: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());
130510 次浏览

Try with Instant.ofEpochMilli() or Instant.ofEpochSecond() method with it-

long test_timestamp = 1499070300L;
LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp ), TimeZone
.getDefault().toZoneId());

You need to pass timestamp in milliseconds:

long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp),
TimeZone.getDefault().toZoneId());


System.out.println(triggerTime);

Result:

2017-07-03T10:25

Or use ofEpochSecond instead:

long test_timestamp = 1499070300L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp),
TimeZone.getDefault().toZoneId());


System.out.println(triggerTime);

Result:

2017-07-03T10:25

Try with the following..

long test_timestamp = 1499070300000L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());

By default 1499070300000 is int if it dosen't contain l in end.Also pass time in milliseconds.

Your issue is that the timestamp is not in milliseconds but expressed in seconds from the Epoch date. Either multiply by 1000 your timestamp or use the Instant.ofEpochSecond().

If you are using the Android threeten back port then the line you want is this

LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())

SIMPLE and straight forward solution will (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