1

I am making an API call to server when logging in user. API call returns JSON format data with field:

time_logged_in - time in iso-8601 format, for example, 2013-08-19T14:29Z

But in my TextView I would like only to display the time when user logged in, for example, 14:29.

Can anyone help and tell me how could I do that?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491

1 Answers1

1

Use SimpleDateFormat to convert from one format to another. Then you can extra the relevant part from that. One way is following:

String server_format = "2013-08-19T14:29Z";    //server comes format ?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
try {

    Date date = sdf.parse(server_format);
    System.out.println(date);
    String your_format = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
    String [] splitted = your_format.split(" ");
    System.out.println(splitted[1]);    //The second part of the splitted string, i.e time
    // Now you can set the TextView here
    

} catch (ParseException e) {
    System.out.println(e.toString()); //date format error
}

P.S I haven't tested this code. Hope you get the concept. See How to format a date android? for more.

Another way is to use .split() using T as delimiter. Then just remove the Z part using answers given on Remove all occurrences of char from string and show the result it in TextView.

Third way can be to replace T and Z with " " ( space ) using .replace function. Then using .split() you can just split the string using " " as delimiter. The second element of the array returned will give time to you.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I am getting error: java.text.ParseException: Unparseable date: "2013-08-29T05:23Z" –  Aug 29 '13 at 06:29
  • Try few things: `1)` Remove the single quote around `T`. i.e Do `SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddTHH:mmZ");` now. If that does not work, try second and third way of doing it. – Shobhit Puri Aug 29 '13 at 06:36
  • Now I got the runtime exception. Will try the other two ways –  Aug 29 '13 at 06:40
  • Its not working because you don't have single quotes around `T` in your input date, which usually is there. Nevertheless the other two ways are easier I guess. – Shobhit Puri Aug 29 '13 at 06:42
  • I used the second way, using "T" as delimiter and it works! Thank you for that. But I have one more question. The iso-8601 is given in UTS time zone, but how can I modify the time to show it correct at my time zone. For example, my clock now shows 11.23 but I am getting the time 07:23. Any suggestions for that? –  Aug 29 '13 at 07:24
  • Glad it solved your issue. For your other question see http://stackoverflow.com/questions/6088778/converting-utc-dates-to-other-timezones – Shobhit Puri Aug 29 '13 at 07:25
  • So instead of single quotes, double quotes didn't give error, right? – Shobhit Puri Aug 29 '13 at 07:26
  • This is how I set the simple date format: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'") . Single quotes around Z also –  Aug 29 '13 at 07:52
  • I am now trying to get the users time zone. What am I doing wrong? `TimeZone tz = TimeZone.getDefault(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); sdf.setTimeZone(tz); String [] splitted = null; try { Date date = sdf.parse(ticket.getEstimatedTimeOfService()); String format = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date); splitted = format.split(" "); } catch (Exception e) { Log.d("Errorrr", String.valueOf(e)); }` –  Aug 29 '13 at 08:00
  • Better post a new question with that issue :). Also mention the error that you are getting. Give me a link here. That'll make it easier. Also if the answer helped you feel free to accept the answer. – Shobhit Puri Aug 29 '13 at 08:15
  • @John What is the issue that you are facing with the time zone changing ? – Shobhit Puri Aug 29 '13 at 14:32