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 .

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
Amrinder
  • 55
  • 6
  • 3
    Instead of splitting, you should parse the date and work on the date object. I will save you headaches and bugs. – assylias Jan 30 '13 at 10:50
  • i am getting the date form the time picker so it is giving me the whole string – Amrinder Jan 30 '13 at 10:52
  • Then you should definitely parse the date and work on a date object :) Check [this out](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html) – Less Jan 30 '13 at 10:53
  • Try this SO solution http://stackoverflow.com/questions/3732790/android-split-string – Shri Jan 30 '13 at 10:59

6 Answers6

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
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)
Avinash Nair
  • 1,984
  • 2
  • 13
  • 17
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);
Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36
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]);
SHAKIL DANISH
  • 69
  • 1
  • 7
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
developer
  • 9,116
  • 29
  • 91
  • 150
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

Tech_Harikishan
  • 123
  • 1
  • 7