Unix时间到正常时间

时间:2017-11-30 19:22:52

标签: java android datetime unix-timestamp

我正在制作一个关于json解析的地震报告的项目。我收到了关于https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson的信息,但是在这个网站上的时间是unix时间,那我该如何转换呢?

MainActivity

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.aaa.quake.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();
private ListView lv;

ArrayList<HashMap<String, String>> contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    contactList = new ArrayList<>();
    lv = findViewById(R.id.list);

    new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(MainActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson";
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray features = jsonObj.getJSONArray("features");

                // looping through All Contacts
                for (int i = 0; i < features.length(); i++) {
                    JSONObject c = features.getJSONObject(i);
                    JSONObject properties = c.getJSONObject("properties");
                    String mag = properties.getString("mag");
                    String place= properties.getString("place");
                    String time = properties.getString("time");



                    // tmp hash map for single contact
                    HashMap<String, String> feature = new HashMap<>();

                    // adding each child node to HashMap key => value
                    //contact.put("id", id);
                    feature.put("mag", mag);
                    feature.put("place", place);
                    feature.put("time", time);

                    // adding contact to contact list
                    contactList.add(feature);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
                R.layout.list_item, new String[]{"place", "time","mag"},
                new int[]{R.id.place,R.id.time, R.id.mag});
        lv.setAdapter(adapter);
    }
}


}

0 个答案:

没有答案