1

I hava one string "2013-1-31 08:25 PM"

i want to split from space and :

i able to split after space it become "2013-1-31 08" and "25 PM"

now i want the "2013-1-31" and "08"

i dont not able to get the value in the 08 in the new string but i get the "2013-1-31"

String view_datee = view_date.getText().toString();

               String[] separated = view_datee.split(":");
               String first =separated[0];
               String second=separated[1]; 


               String[] newSeperated = first.split(" ");
               String third = newSeperated[0]; 
               String four=   newSeperated[1];



               Log.i("first",first);
               Log.i("second",second);

               Log.i("third", third);
               Log.i("four", four);

I do not how to get the four value means 08 .

4
  • 3
    Instead of splitting, you should parse the date and work on the date object. I will save you headaches and bugs. Commented Jan 30, 2013 at 10:50
  • i am getting the date form the time picker so it is giving me the whole string Commented Jan 30, 2013 at 10:52
  • Then you should definitely parse the date and work on a date object :) Check this out Commented Jan 30, 2013 at 10:53
  • Try this SO solution stackoverflow.com/questions/3732790/android-split-string Commented Jan 30, 2013 at 10:59

6 Answers 6

3

Here is an example using a date / calendar (it uses desktop java but easily transposable):

public static void main(String args[]) throws Exception {
    String data = "2013-1-31 08:25 PM";
    DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
    Calendar cal = Calendar.getInstance();
    cal.setTime(fmt.parse(data));
    //2013-1-31
    System.out.println(new SimpleDateFormat("yyyy-M-dd").format(cal.getTime()));
    //20
    System.out.println(cal.get(Calendar.HOUR_OF_DAY));
    //08
    System.out.println(new SimpleDateFormat("hh").format(cal.getTime()));
}

Note that 08:25 PM is 20:05 so you can get either 08 or 20 depending on what you need. I showed both in my example.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

public static void main(String[] args) {
        String str = "2013-1-31 08:25 PM";
        System.out.println("[Date:"+str.split(" ")[0]+"][Hours:"+str.split(":")[0].split(" ")[1]+"]");
    }

Output,

run:
[Date:2013-1-31][Hours:08]
BUILD SUCCESSFUL (total time: 1 second)

Comments

0

From the official Javadoc, split takes a RegExp as argument.

So, you cannot use " " as split argument.

Instead, you should use "\s" to sepparate the string by whitespace.

Then, your code would be:

           String[] separated = view_datee.split(":");
           String first =separated[0];
           String second=separated[1]; 


           String[] newSeperated = first.split("\\s");
           String third = newSeperated[0]; 
           String four=   newSeperated[1];



           Log.i("first",first);
           Log.i("second",second);

           Log.i("third", third);
           Log.i("four", four);

Comments

0

Are u sure String ends with PM Or AM Then u can do like this

String s= "2013-1-31 08:25 PM";

        String newStr=s.substring(s.indexOf(" ")+1,s.lastIndexOf(" "));
        System.out.println(newStr);
        String result[]=newStr.split(":");
        System.out.println(result[0]);
        System.out.println(result[1]);

Comments

0

check below code, its working properly

String date = "2013-1-31 08:25 PM";
    String[] split = date.split(":");
    System.out.println(split[0]+"date:::"  + split[1] );
    String[] Datesplit = split[0].split(" ");
    System.out.println(Datesplit[0]+"date splited:::"  + Datesplit[1] );

Output below

2013-1-31 08date:::25 PM
2013-1-31date splited:::08

Comments

0

I have checked out your code and its working properly.. Here is i am posting whatever i have typed and excuted:

    String teststr = "2013-1-31 08:25 PM";
    System.out.println("teststr: " + teststr);
    String[] separated = teststr.split(":");
    String first = separated[0];
    String second = separated[1];
    String[] newSeperated = first.split(" ");
    String third = newSeperated[0];
    String four = newSeperated[1];
    System.out.println("first : "+first);
    System.out.println("second : "+second);
    System.out.println("third : "+third);
    System.out.println("fourth : "+four);

and its giving me following output: teststr: 2013-1-31 08:25 PM first : 2013-1-31 08 second : 25 PM third : 2013-1-31 fourth : 08

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.