0

I'm developing an android application and I use GSON to get the data from the server, I'm working on both APIs of Facebook and Imgur and the same issue happens. How can I convert a date from milliseconds format to a human-readable format like for example 1584780523 and I want to convert it to any format, for example, 25, Mar 2020.

What I tried to do!

Get Data

@SerializedName("datetime")
    @Expose
    private long datetime;

    // setters and getters

In my adapter after getting DATETIME I parse it to a human-readable format

// Get Datetime. 
long getDate = data.getDatetime();

// Parse it in this format for example.
DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");
Date result = new Date(getDate);

// Set Result (Human-Readable date)
date.setText(dateFormat.format(result));

But here is the problem! It gives me date like this

19 Jan 1970 09:54:00:533 +0200

Why the output at the 1970s. I saw something like that is the default output of Datetime? But in the same case, it gives me on the other items on my RecyclerView the same output but the last three digits changes!

Here what I'm asking!

1- Why we use (a lot of people use long instead of int?

2- Why the output gives me that and how can I fix this?

3- In other APIs like Youtube they use the regular DateTime why Facebook and Imgur changes them?

Note: I searched about my question for 3 days but I didn't get any answers or relative questions on StackOverflow so, I asked here. All of them or most are for PHP and JavaScript I need an example in Java Android Studio

Thanks.

  • It gives me date like this *19 Jan 1970 09:54:00:533 +0200* - that is what you asked for `"dd MMM yyyy HH:mm:ss:SSS Z"` If you want a different format then use a different pattern – Scary Wombat Mar 25 '20 at 02:11
  • Yes, I don't talk about the format I talk about the value, there is no sense in a post was in 1970. I mean it's must show 25 Mar 2020 etc... – Abdelrahman Ashref Mar 25 '20 at 02:13
  • 1
    Oh, I see, try multiplying the value by 1000 `System.out.println(new Date (1584780523000L ));` I guess that they are not interested in ms precision and an int would save diskspace – Scary Wombat Mar 25 '20 at 02:16
  • @ScaryWombat thank you sir, it works now. I will search now to know why we use 1000L and the value returned. it works like charm thank you. – Abdelrahman Ashref Mar 25 '20 at 02:27
  • 1
    If you can use Java 8, the new Time API is _much_ friendlier to work with, and you'd use `Instant.ofEpochSecond(timestamp)`. – chrylis -cautiouslyoptimistic- Mar 25 '20 at 02:54
  • @chrylis-onstrike- I would like to wrap it into Instant, I think it's much better and easier. but it gives me Call require API level 26 (current min is 17). Is there any way to use it and support older apps? maybe I can add requireAPi()? – Abdelrahman Ashref Mar 25 '20 at 03:01
  • 1
    You can use [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), which is an Android backport of the new API. – chrylis -cautiouslyoptimistic- Mar 25 '20 at 03:02
  • 1
    @chrylis-onstrike- That's awesome I use a lot of formatting date and parse them and I use java 8 in my app. this is really better than Date and Calendar. it seems that it do a lot of work as said in this question https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project. thank you sir I will wrap my old Date to the newThreeTenABR. – Abdelrahman Ashref Mar 25 '20 at 03:08

2 Answers2

2

Your 1584780523 value is in seconds, not milliseconds.

long secondsSinceEpoch = 1584780523;
long millisSinceEpoch = secondsSinceEpoch * 1000L;
Date date = new Date(millisSinceEpoch);
System.out.println(date);

SimpleDateFormat fmt = new SimpleDateFormat("d, MMM yyyy", Locale.US);
System.out.println(fmt.format(date));

Output1

Sat Mar 21 04:48:43 EDT 2020
21, Mar 2020

1) I'm in America/New_York time zone

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

1- Why we use (a lot of people use long instead of int?

This is to avoid the year 2038 problem. While for example 1584780523 does fit into an int (a 32 bits signed integer), only dates and times up to January 19 2038 03:14:07 UTC can be represented. When writing a program today, we cannot be very sure that no one will ever use our code for dates and times after that point. So we use long instead. After the year 2000 problem (using two digit years leading to problems for dates in year 2000 and later) I guess the IT world has made a sort of a commitment not again to use date and time representations that have an end date within our lifetime.

2- Why the output gives me that and how can I fix this?

Andreas has already explained this part: Because you were treating your seconds as milliseconds. It’s a common mistake.

BTW I recommend using a proven library for the conversion. The comments have already mentioned:

    Instant.ofEpochSecond(timestamp)

While multiplying by 1000 works (on a long, not on an int because of overflow), doing your date and time conversions by hand is a bad habit to get into because they very often get more complicated than you think, and the risk of errors is great. Also using a library method with a nice name much better conveys why you are multiplying by 1000.

3- In other APIs like Youtube they use the regular DateTime why Facebook and Imgur changes them?

I guess it’s because they didn’t know better back when they designed the API. There’s an international standard for transmitting dates and times, ISO 8601, and using it efficiently prevents mistakes like yours, so this is what they should have used. Even if they have understood that later, now a lot of programs rely on the old way, so changing it now would also be risky.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Impressive! That's why we have to use long instead of int. First time I have checked ThreeTenAPI and the options to know the difference between is very helpful. In APIs like YouTube and Vimeo, etc... it's very easy to parse dates by just formatting it but on Facebook and Imgur it's not very hard as I thought but as you said it's risky as there is a lot of people who uses this APIs to fetch their content. Thank you sir for making this clear for me and I think it will help a lot of people too. – Abdelrahman Ashref Mar 25 '20 at 06:22