-1

I have a json response string like this: 2015-09-30 11:09:00 (date and time is a single string) I need to add "at" between "2015-09-30" and "11:09:00". How can I split this date and time so that I can add the word "at" in between them. My requirement::I displayed the date and time in a single textview(without splitting them). But now when I split this, how can I display them on two different text view,i.e; date in first textview and time in second text view.Please help me to sort this problem.

Adarsh
  • 89
  • 4
  • 14

4 Answers4

0

Use replaceFirst

string.replaceFirst("[ \\t]+", " at ");
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
String jsonString = "2015-09-30 11:09:00";
String dateTime[]=jsonString.split(" ");
String finalStr = dateTime[1]+" at "+dateTime[0];

i guess this should work

or String finalStr = jsonString.replace(" "," at ");

  • this jsonString value always varies, so I can't assign it as a single value like this :jsonString = "2015-09-30 11:09:00". How can I split the date and time for all json response values? – Adarsh Aug 17 '15 at 10:31
0

You can use split() for that :

String[] dateTimeArray = dateTimeString.split(" ");
String date = dateTimeArray[0]; // date
String time = dateTimeArray[1]; // time

Edit

set it to two different textviews:

tvFirstTextView.setText(date);
tvSecondTextView.setText("at " +time);
SANAT
  • 8,489
  • 55
  • 66
0
String [] DateTime = ANY_JSON_STRING.split(" ");
String Date = DateTime[0];
String Time = DateTime[1];

Now you can play with String as per your requirement i.e. Date + " at " + Times, or anything else like that.

paxbat14
  • 101
  • 7