0

I have an app which takes data from JSON file,("name","date","picture_url") and show it on RecyclerView, but... the date is written in unix, so i need to take each "date" JSON Object and change it's value to proper date format, before showing it onto the screen.

So i've written something like that :

long dv = Long.valueOf(o.getString("date"))*1000;
Date df = new java.util.Date(dv);
String vv = new SimpleDateFormat("yyyy-MM").format(df);
o.getString("date").equals(vv);

In this:

StringRequest stringRequest = new StringRequest(
            Request.Method.GET,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                            try {
                        JSONArray array = new JSONArray(response);

                        for(int i=0; i<array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            ListItem item = new ListItem(
                                    o.getString("name"),
                                    o.getString("date"),
                                    o.getString("picture_url"
                                    )
                            );
                            long dv = Long.valueOf(o.getString("date"))*1000;
                            Date df = new java.util.Date(dv);
                            String vv = new SimpleDateFormat("yyyy-MM").format(df);
                            o.getString("date").equals(vv);
                            listItems.add(item);
                        }
                        adapter = new MyAdapter(listItems, getApplicationContext());
                        recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            }
    );

But it doesn't work. The SDK is showing no error's and im out on new ideas. So how can i take my "date" and change it before showing it onto the app screen ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Marcello
  • 5
  • 2
  • what is the format of date you are receiving? – Abdul Kawee Feb 21 '18 at 15:05
  • where do you put the formatted date back to list item? – budgie Feb 21 '18 at 15:10
  • Can you show the response of date you getting from api? – Bunny Feb 21 '18 at 15:10
  • I think you may have misinterpreted the use of the `equals` method. It doesn’t assign anything. – Ole V.V. Feb 21 '18 at 19:11
  • The `SimpleDateFormat` class is long outmoded and notoriously troublesome. Consider adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project so you may use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), instead. It is so much nicer to work with. – Ole V.V. Feb 21 '18 at 19:14

4 Answers4

0

use this method :

private static String getDate(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time);
        return DateFormat.format("dd-MM-yyyy", cal).toString();
    }
Ouail Bellal
  • 1,554
  • 13
  • 27
  • that would do if i needed the current time but, i've got the dates from past(and there are many of them not just one) written in unix so i need to take them and convert them into normal date. – Marcello Feb 21 '18 at 15:13
  • bro , use this methode to convert them for example getDate(o.getString("date")) ; will return the "date" with date format not with unix timestamp – Ouail Bellal Feb 21 '18 at 16:14
0

You can use this function

public String timestampToFormatedString(long timeStamp,String format ){

    Date date = new Date(timeStamp);
    DateFormat formatter = new SimpleDateFormat(format);
    return formatter.format(date);
}

and finally

String formatedDateString=timestampToFormatedString("dd-MM-yyyy",timeStamp);
Amin Bahiraee
  • 538
  • 10
  • 20
0

To modify the original JSONObject with your new string representation you should use the put method:

    long dv = Long.valueOf(o.getString("date"));
    YearMonth ym = YearMonth.from(
            Instant.ofEpochSecond(dv).atZone(ZoneId.systemDefault()));
    o.put("date", ym.toString());

At the same time this snippet demonstrates another change: use of java.time, the modern Java date and time API, instead of the Date and SimpleDateFormat. This change isn’t necessary for things to work, but I recommend it. The date-time classes you use are long outmoded, and SimpleDateFormat in particular is notoriously troublesome. Please note that with the modern API you don’t need an explicit formatter for obtaining a string like 2017-07 since YearMonth.toString produces exactly such a string. Also the API converts directly from Unix time in seconds, so your reader need not wonder why you were multiplying by 1000.

Question: Can I use java.time with my Java version?

Yes, java.time works nicely on Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

First : all of your answer's have good methods that I could have used, but I wasn't asking for methods because I had a proper one. Thank's anyway for th answer's.

The easiest solution what i have found was :

 for (int i = 0; i < array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            long dv = Long.valueOf(o.getString("date")) * 1000L;
                            Date df = new java.util.Date(dv);
                            String vv = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(df);
                            ListItem item = new ListItem(
                                    o.getString("name")
                                    , vv
                                    , o.getString("picture_url")

                            );
                            listItems.add(item);

I had my a tiny part of my code misunderstood so i needed to put this code a few lines back and change one line.

Marcello
  • 5
  • 2