0

I was unable to find an answer to this problem, so once I figured it out, I felt posting the answer might help other people down the road.

The issue is when you have downloaded your messages from Facebook and you download them in JSON format, the timecode needs to be converted to a format that can be used in your Java program.

The timecode from Facebook is a 13 digit number that looks something like this: 1548410106047

Michael Sims
  • 2,360
  • 1
  • 16
  • 29
  • Without knowing which API specifically you're asking about, that **might** be an Epoch-millis, but unless the API states it, this is just a guess. – M. Prokhorov Feb 13 '19 at 08:13
  • It’s a very good guess, @M.Prokhorov. 1548410106047 would correspond to Friday, January 25, 2019 9:55:06.047 AM GMT. – Ole V.V. Feb 13 '19 at 08:55
  • 1
    @OleV.V, still, I would never feel comfortable asking and answering a question about just some random API returning a 13-digit number. API docs exist for a good reason - so we don't have to guess this stuff based on outside experience. – M. Prokhorov Feb 13 '19 at 09:22
  • Michael Sims, one of the original questions I linked to was lacking a modern answer, so I had your question in mind when [writing one here](https://stackoverflow.com/a/54666535/5772882). – Ole V.V. Feb 13 '19 at 09:24
  • @OleV.V. There were no questions or answers that specifically addressed the Facebook timecode format when downloaded in JSON. You marked my question as a duplicate, yet I still don't see any questions about timecode that SPECIFICALLY address the FACEBOOK JSON data that users can download from their account. So I am a bit confused about this being marked as a duplicate - and not even sure if this is a bad thing? – Michael Sims Mar 02 '19 at 18:08
  • Thanks for your comment, Michael. It’s true that none of the other questions mentions Facebook. I think that the presence of your question tying the 13 digit number to Facebook *with* the links to the modern and correct solutions to the problem is likely to be the best help for future readers. Feel free to vote for reopening if you disagree. – Ole V.V. Mar 03 '19 at 16:49

1 Answers1

1

The solution to the problem is:

You must first convert it to a Long datatype, then simply make a Timestamp out of it like this:

Long fbt = Long.parseLong(facebookTime);
Timestamp ts = new Timestamp(fbt);

Once you have it in a timestamp you can do anything with it such as:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, YYYY hh:mm a");
System.out.println(sdf.format(ts));

The output from above looks like this:

Friday, January 25, 2019 01:55 AM

Michael Sims
  • 2,360
  • 1
  • 16
  • 29
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Feb 13 '19 at 08:54
  • 1
    Uppercase `YYYY` is wrong and will give you surprises around New Year. Use `DateTimeFormatter` and then for example `uuuu` for year. – Ole V.V. Feb 13 '19 at 08:57