long timestamp to LocalDateTime

JavaJava 8TimestampJava Time

Java Problem Overview


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 = 1499070300;

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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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

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

Solution 4 - Java

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());

Solution 5 - Java

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().

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrhandomView Question on Stackoverflow
Solution 1 - JavaJuanView Answer on Stackoverflow
Solution 2 - JavaAkshayView Answer on Stackoverflow
Solution 3 - JavaJamieHView Answer on Stackoverflow
Solution 4 - JavaRazibView Answer on Stackoverflow
Solution 5 - JavaAlex RoigView Answer on Stackoverflow
Solution 6 - JavaNasibView Answer on Stackoverflow