Reputation: 1278
I have a long timestamp 1499070300 (equivalent to Mon, 03 Jul 2017 16:25:00 +0800) but when I convert it to LocalDateTime I get 1970-01-18T16:24:30.300
Here's my code
long test_timestamp = 1_499_070_300L;
LocalDateTime triggerTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(test_timestamp), TimeZone
.getDefault().toZoneId());
Upvotes: 104
Views: 178481
Reputation: 1557
Starting from JDK 8, It's also possible e to leverage on the LocalDateTime method ofEpochSeconds:
private LocalDateTime epochSecondsToLocalDate(long epochSeconds) {
ZoneId zoneId = ZoneOffset.systemDefault();
ZoneOffset zoneOff = zoneId.getRules().getOffset(LocalDateTime.now());
return LocalDateTime.ofEpochSecond(epochSeconds, 0, zoneOff);
}
Or, if handling milliseconds:
private LocalDateTime epochMillisToLocalDateTime(long epochMillis) {
ZoneId zoneId = ZoneOffset.systemDefault();
ZoneOffset zoneOff = zoneId.getRules().getOffset(LocalDateTime.now());
return LocalDateTime.ofEpochSecond(epochMillis / 1000,
(int) (epochMillis % 1000) * 1000000, zoneOff);
}
This method first divides the epoch timestamp by 1000 to convert milliseconds to seconds, as LocalDateTime.ofEpochSecond expects seconds. The remainder (epochMillis % 1000) represents the milliseconds part, which is then converted to nanoseconds (by multiplying by 1000000) for the nanosecond parameter of LocalDateTime.ofEpochSecond.
Upvotes: 1
Reputation: 1579
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
Upvotes: 0
Reputation: 4859
If you are using the Android threeten back port then the line you want is this
LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())
Upvotes: 9
Reputation: 11173
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());
Upvotes: 3
Reputation: 2099
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
Upvotes: 172
Reputation: 1574
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()
.
Upvotes: 3
Reputation: 1161
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.
Upvotes: 6