Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how split string in java

i have project in android, and i need split string, which is actually date here is the String that i'm going to split:

String datetime = "01/03/2013 09:00";

and this is how i call them:

int year = splitDateTime(dateTime)[0];
public int[] splitDateTime(String datetime){
        String date_time = datetime;
        String delimiter = "//:";

        String[] sParts;
        int[] intParts;

        sParts = date_time.split(delimiter);
        intParts = new int[sParts.length];

        for(int i =0; i < sParts.length ; i++)
            intParts[i] = Integer.parseInt(sParts[i]);

        return intParts;
    }

but it keeps giving me an error, so i would be happy if you help me out guys. cheers

like image 800
HasanZayed Avatar asked Feb 18 '23 08:02

HasanZayed


1 Answers

I bet you mean something like:

String datetime = "01/03/2013 09:00";

String[] sParts = datetime.split("[/:\\s+]");
int[] iParts = new int[sParts.length];

for(int i = 0; i < sParts.length; i++)
    iParts[i] = Integer.parseInt(sParts[i]);

System.out.println(Arrays.toString(iParts));

OUTPUT:

[1, 3, 2013, 9, 0]
like image 92
Eng.Fouad Avatar answered Feb 20 '23 00:02

Eng.Fouad