-2

I am using earthquake API when I fetch time of earthquake it is not in Human readable as shown below.

Time in API is in Long form and I want to display it in listview as 00/00/0000

This is how I fetch time and some more data from API :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        l=(ListView)findViewById(R.id.list_id);
        queue= Volley.newRequestQueue(this);
        request();
        l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              ApiClass apiClass=list.get(position);
               Uri uri=Uri.parse(apiClass.getUrl());
               Intent intent=new Intent(Intent.ACTION_VIEW,uri);
               startActivity(intent);
              }
          });

    }


                   @Override
                   public void onResponse(String response) {

                       try {
                           JSONObject ob1=new JSONObject(response);
                           JSONArray ob2=ob1.getJSONArray("features");
                           for (int i=0;i<ob2.length();i++){
                               JSONObject ob3=ob2.getJSONObject(i);
                               JSONObject ob4=ob3.getJSONObject("properties");

                               String title=ob4.getString("title");
                               Double mag=ob4.getDouble("mag");
                               Long time=ob4.getLong("time");
                               String u=ob4.getString("url");

                               list.add(new ApiClass(title,mag,time,u));

                           }
                           CustomAdapter adapter=new CustomAdapter(MainActivity.this,list);
                           l.setAdapter(adapter);
                       }
                       catch (JSONException e) {
                           e.printStackTrace();
                       }

                       // Display the first 500 characters of the response string.
                   }
               }, new Response.ErrorListener() {
           @Override
               public void onErrorResponse(VolleyError error) {
               Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
           }
       });

       queue.add(stringRequest);
     }
}

Following is my custom adapter java class:

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v=convertView;
           if(convertView==null){
               v= LayoutInflater.from(c).inflate(R.layout.custom_layout,parent,false);
           }
           ApiClass ob=(ApiClass) arrayList.get(position);
        CircleImageView image=v.findViewById(R.id.image_id);
        TextView tv1=v.findViewById(R.id.title_id);
        TextView tv2=v.findViewById(R.id.magnitude_id);
        TextView tv3=v.findViewById(R.id.time_id);
        tv1.setText(ob.getTitle().toString());
        tv2.setText(ob.getMag().toString());
        tv3.setText(ob.getTime().toString());
        image.setImageResource(R.drawable.mouse);

        return v;
    }

This is my Apiclass which return value to set value in listview:

    public ApiClass(String title, Double mag, Long time, String url) {
        this.title = title;
        this.mag = mag;
        this.time = time;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public Double getMag() {

        return mag;
    }

    public Long getTime() {

        return  time;
    }

    public String getUrl() {
        return url;
    }

}
Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
RahulSingh
  • 11
  • 6

3 Answers3

0

convert long time stamp to Date object

Date date = new Date(long)

then use java provided SimpleDateFormat API to convert time to your desired format, for example to get 2018-Mar-1

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-d")
String date = sdf.format(date) // date object created above
Pwn
  • 308
  • 3
  • 9
0

tl;dr

Instant.ofEpochMilli( 1_737_394_537_378L  )

2025-01-20T17:35:37.378Z

java.time

The modern approach uses java.time classes.

You apparently have a count of milliseconds since an epoch of first moment of 1970 in UTC, 1970-01-01T00:00Z.

Instant instant =  Instant.ofEpochMilli( 1_737_394_537_378L  ) ;

Generate a String in standard ISO 8601 format.

String output = instant.toString() ;

2025-01-20T17:35:37.378Z

For other formats, use DateTimeFormatter. Search Stack Overflow for many many examples and discussions already posted.

To see the same moment in the wall-clock time used by the people of a certain region, apply a time zone.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

This has worked for me :

   private String getDate(long time){
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    String dateString = formatter.format(new Date(time));
    String date = ""+dateString;
    return date;
}
RahulSingh
  • 11
  • 6
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 02 '18 at 00:25