-1

I read from a very old post here on stackoverflow that joda is a possible solution to convert Unix timestamp.

import org.joda.time._
new DateTime(1511544070).toString("yyyy-MM-dd")

I got 1970-01-18 for this case, however, this is wrong because the date should be according to this online converter: 11/24/2017 @ 5:21pm (UTC)

It is possible the online converter is correct because the sample unix timestamp 1511544070 is from a dataset that date range is November 25 to December 03, 2017, the dataset is from China time which is 8 hours ahead of UTC, meaning 11/24/2017 @ 5:21pm (UTC) is actually 11/25/2017 @ 1:21am (Beijing Time)

Where can I get a working library or is there a working library that can get the same result like the online converter?

Shaido
  • 27,497
  • 23
  • 70
  • 73
Choix
  • 555
  • 1
  • 12
  • 28

2 Answers2

1

You can do that using java.time:

import java.time.{ LocalDateTime, ZoneOffset }
import java.time.format.DateTimeFormatter

LocalDateTime.ofEpochSecond(1511544070, 0, ZoneOffset.UTC)
  .format(DateTimeFormatter.ofPattern("yyyy-MM-dd @ h:mm a"))
defdog
  • 26
  • 1
  • 1
  • Thank you. How can I convert it into Beijing time then? In the final, I need only yyyyMMdd, but it needs to be in Beijing time. – Choix Jun 19 '18 at 11:08
  • @Choix, change zone offset to ZoneOffset.of("+08:00") – defdog Jun 19 '18 at 11:49
  • yes, thank you. LocalDateTime.ofEpochSecond(1511544070, 0, ZoneOffset.ofHours(8)).format(DateTimeFormatter.ofPattern("yyyyMMdd")) works – Choix Jun 19 '18 at 12:48
0

Looking at the documentation for joda-time we see that a DateTime can take a Long specifying the milliseconds since 1 Jan 1970. However, you seem to be providing a value in seconds. Joda-time is actually calculating it correctly, since since 1511544070/(1000*3600*24) equals 17.49 days, i.e. 1970-01-18.

To get the expected result multiply with 1000:

new DateTime(1511544070*1000).toString("yyyy-MM-dd")

To get the time in another timezone, add withZone() as follows (for Shanghai/Beijing):

new DateTime(1511544070*1000).toString("yyyy-MM-dd")
  .withZone(DateTimeZone.forID("Asia/Shanghai"))
Shaido
  • 27,497
  • 23
  • 70
  • 73
  • Thanks. Can't do any change to the original unix timestamp. How can I convert it into Beijing time then? In the final, I need only yyyyMMdd, but it needs to be in Beijing time. – Choix Jun 19 '18 at 11:11
  • @Choix: Just multiply the value with 1000. I added some information about how to change the timezone to the answer. – Shaido Jun 19 '18 at 12:19
  • Thank you. I am afraid multiplying the timestamp would potentially introduce inaccuracy, besides, the dataset has 100M rows so I am also worrying it would be an extra cost. – Choix Jun 19 '18 at 18:28
  • @Choix: The multiplication would be entirely negligible compared to the other transformations, you wouldn't notice anything. Inaccuracies are also impossible, those could appear if it was division, but with multiplication like this you will **never** see it (the only thing it's doing is adding three 0 to the end). – Shaido Jun 19 '18 at 23:26