long timestamp to LocalDateTime
Asked Answered
S

6

100

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());
Synesthesia answered 3/7, 2017 at 10:35 Comment(4)
long test_timestamp = 1499070300l;??Lanner
Use ZoneId.systemDefault() instead of TimeZone.getDefault().toZoneId()Jidda
You’re not the first to ask a about a bug like this one. For example: SimpleDateFormat always returns 1970.01.17 with wrong timezone.Megaton
Similar: Converting 19-digit Unix Timestamp to a Readable DateTowandatoward
C
168

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
Chintzy answered 3/7, 2017 at 10:42 Comment(1)
BTW if you are using AndroidThreeTen, replace TimeZone.getDefault().toZoneId() with DateTimeUtils.toZoneId(TimeZone.getDefault()).Withy
H
9

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

LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())
Hinz answered 13/1, 2020 at 11:33 Comment(0)
L
6

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.

Lanner answered 3/7, 2017 at 10:43 Comment(0)
N
3

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

Nicias answered 3/7, 2017 at 10:44 Comment(0)
A
2

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());
Anele answered 3/7, 2017 at 10:40 Comment(1)
You mean "Try with Instant.ofEpochSecond()", right? Otherwise your text is confusing.Incongruent
J
0

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
Johnjohna answered 30/9, 2021 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.