2

I am using Volley to get the data from Api.I am receiving Time and date in this format 2018-04-04T08:41:21.265185Z.I want to set time as Local time in RecyclerView in this format 8:41 PM.
This is code for converting the Time

 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                            dateFormat.setTimeZone(TimeZone.getDefault());
                            try {
                                Date date=dateFormat.parse(bookingTime);
                                TimeZone zone=TimeZone.getDefault();
                                dateFormat.setTimeZone(zone);
                                finalTime=dateFormat.format(date);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }  

This is code for setting the converted time to ReyclerView

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {

                        for (int i = 0; i < response.length(); i++) {
                            JSONObject object = response.getJSONObject(i);
                            final CurrentEntry entry = new CurrentEntry();
                            String id = object.getString("myid");
                            String Time=object.getString("time");
                            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                            dateFormat.setTimeZone(TimeZone.getDefault());
                            try {
                                Date date=dateFormat.parse(Time);
                                TimeZone zone=TimeZone.getDefault();
                                dateFormat.setTimeZone(zone);
                                finalTime=dateFormat.format(date);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            JsonObjectRequest foodie_request = new JsonObjectRequest(Request.Method.GET, myurl, null, new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {
                                    try {
                                        String fname = response.getString("fname");
                                        String lname = response.getString("lname");
                                        entry.setName(first_name + "\t" + last_name);
                                       // progressDialog.dismiss();
                                        adapter.notifyDataSetChanged();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {

                                }
                            }) 
                            };
                            AppController.getInstance().addToRequestQueue(foodie_request, foodie_data_req);
                            String no = object.getString("no");
                            String ptime = object.getString("pt");
                            entry.setP(people);
                            entry.setT(finalTime);
                            current.add(entry);

                                adapter = new CurrentStatusAdapter(current, getActivity().getApplicationContext());
                                recyclerView.setAdapter(adapter);
                        }  

How to convert and set this time ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

2 Answers2

2

I think your dateTime 2018-04-04T08:41:21.265185Z is missing TIMEZONE Z

Format - yyyy-MM-dd'T'HH:mm:ss.SSSZ

Output - 2001-07-04T12:08:56.235-0700

OR Sample -

try {
    DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
    Date d = f.parse("2018-04-04T08:41:21.265185");
    DateFormat date = new SimpleDateFormat("MM/dd/yyyy");
    DateFormat time = new SimpleDateFormat("hh:mm a");
    System.out.println("Date: " + date.format(d));
    System.out.println("Time: " + time.format(d));
} catch (Exception e) {
    e.printStackTrace();
}

Ref - See Examples SimpleDateFormat in this link

AndyBoy
  • 564
  • 6
  • 29
1

java.time

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
    String bookingTimeString = "2018-04-04T08:41:21.265185Z";
    Instant bookingTime = Instant.parse(bookingTimeString);
    String finalTime = bookingTime.atZone(ZoneId.systemDefault()).format(timeFormatter);
    System.out.println(finalTime);

On my computer in Europe/Copenhagen time zone this printed:

10:41 AM

So the time has been converted to my local time as you requested.

I am using java.time, the modern Java date and time API. Even on Android and even though java.time is only built-in on new Android devices consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends. The modern API is so much nicer to work with and works nicely on older devices too, see below for details.

The Instant class from java.time parses the string you receive without any explicit formatter. This is because the string is in ISO 8601 format, the standard format the the modern classes parse as their default. Given the number of questions on Stack Overflow concerning trouble with date-time format pattern strings this seems a rather great advantage.

Question: Can I use java.time on Android?

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

  • In Java 8 and later and on new Android devices the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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