How can I separate a string from a string


I need to enter a date, that you enter as string example: 21/10 / 2016-12: 04, and these data convert each one into a data type, (int, byte), to make validations and then enter them to a calendar

asked by Yeferson Gallo 21.10.2016 в 07:07

3 answers


The usual thing for this particular case is to use a date parser, with which you get directly a date that you can use to initialize the calendar you want to obtain.


try {
    String fechaEntera = "21/10/2016-12:04";
    //transforma la cadena en un tipo date
    Date miFecha = new SimpleDateFormat("dd/MM/yyyy-HH:mm").parse(fechaEntera);

    //creo un calendario
    Calendar calendario = Calendar.getInstance();
    //establezco mi fecha

    //obtener el año
    int anio = calendario.get(Calendar.YEAR);
    //obtener el mes (0-11 ::: enero es 0 y diciembre es 11)
    int mes = calendario.get(Calendar.MONTH);
    //obtener el dia del mes (1-31)
    int dia = calendario.get(Calendar.DAY_OF_MONTH);
    //obtener el hora del dia (1-24)
    int hora = calendario.get(Calendar.HOUR_OF_DAY);
    //obtener el minuto
    int minuto = calendario.get(Calendar.MINUTE);
    //obtener el segundo
    int segundo = calendario.get(Calendar.SECOND);

    //...mas campos... 

} catch (ParseException ex) {
    //manejar excepcion

You can get more fields by looking at the constants of Calendar

answered by 21.10.2016 в 09:24

You can save the date as a String and divide it using the split function.

String fechaEntera = "21/10/2016-12:04";
String[] fechaHora = fecha.split("-"); //Ahora tenemos un array de Strings tomando como referencia el -, es decir, con dos posiciones, en la primera estará almacenado la fecha y en la segunda la hora

String[] fechaDividida = fechaHora[0].split("/"); //Dividiremos la fecha (que está almacenada en la posición 0 del array anterior) tomando como referencia los '/'.

And we can show the dateDivided:

System.out.println(fechaDividida[0]); //21
System.out.println(fechaDividida[1]); //10
System.out.println(fechaDividida[2]); //2016

Now we will divide the time in the same way as the date but taking as reference the two points:

String[] horaDividida = fechaHora[1].split(":");

And we can show it:

System.out.println(horaDividida[0]); //12
System.out.println(horaDividida[1]); //04

Finally, to move from String to int you will have to use the parseInt function:

int hora = Integer.parseInt(horaDividida[0]);

EDIT: I just saw that you want to enter these values in a Calendar. For this, you can use the function set .

calendar.set(anno, mes - 1, dia, hora, minutos, segundos);

To this function you will have to pass the whole data that you just calculated. Notice that the month starts with 0, that is, the 0 is the equivalent of putting January. Be careful with this because if you put a 12 for December it will give you an error (that's why you spend 1 on the whole value of the month you spend).

answered by 21.10.2016 в 07:33

There is also the java.util.StringTokenizer that allows you to separate the string from the characters that you indicate. There are many examples of using this, you just have to search.

answered by 21.10.2016 в 18:41