Convert positions of a string to date in java

1

I am working on a project that involves the manipulation of a string with a CURP format. Although the whole program is already working, I can not validate the dates since I do the following:

     inicialAPaterno=curp.charAt(0);
     inicialApaterno1=curp.charAt(1);
     inicialAMaterno=curp.charAt(2);
     inicialNombre=curp.charAt(3);
     ano=curp.charAt(4);
     ano1=curp.charAt(5);
     mes=curp.charAt(6);
     mes1=curp.charAt(7);
     dia=curp.charAt(8);
     dia1=curp.charAt(9);
     sexo=curp.charAt(10);
     entidad=curp.charAt(11);
     entidad1=curp.charAt(12);

And that I do to separate characters from the chain and although my program already does what it should, I would like to convert to date the positions 4 , 5 , 6 , 7 , 8 and 9 That to validate and be able to show in a text box the correct results.

    
asked by Alan Erick Salgado Vazquez 07.03.2017 в 23:28
source

3 answers

0

EDIT:

You can create a chain by taking the 6 values and convert it to date in ddMMyy format

DEMO

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

class ConvertirFecha
{
    public static void main (String[] args) throws java.lang.Exception
    {

    /*Obtienes una cadena de tu fecha uniendo
     * String sDate=dia+dia1+mes+mes1+ano+ano1+;
     */

     //Uso este valor de ejemplo

        String sDate="100801";
        Date dOk=parseDate(sDate);
        System.out.println(dOk);

        System.out.println("Otras pruebas");
        System.out.println(parseDate("091120"));
        System.out.println(parseDate("100400"));

    }

    public static Date parseDate(String dateStr) {
        final SimpleDateFormat date_format = new SimpleDateFormat("ddMMyy");
        try {
            return date_format.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }


}

Result:

Fri Aug 10 00:00:00 GMT 2001

Otras pruebas
Mon Nov 09 00:00:00 GMT 2020
Mon Apr 10 00:00:00 GMT 2000
    
answered by 08.03.2017 в 00:02
0

You can simply take the substring with your data and convert it with a SimpleDateFormatter :

    SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
    Date d = sdf.parse(curp.substring(4, 10));

In the case that you can not build a Date of the characters, you throw a ParseException .

Eye! The parser is very flexible in its work, for example it will convert a "date" as "170336" (March 36, 2017) in the 5th of April 2017, which it's calendarly correct, but probably not enough for a validation.

The easiest way to include a validation would be:

public boolean validar(String fecha){
    try{
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        Date d = sdf.parse(fecha);
        return fecha.equals(sdf.format(d));
    } catch (ParseException pe) {
        return false;
    }
}

Then you can validate the date with:

boolean valido = validar(curp.substring(4,10));            
    
answered by 07.03.2017 в 23:54
-1

You can use a regular expression to verify that it is correct.

    import java.util.regex.Pattern;
    private boolean validarCURP(String curp)
      { 
         String regex ="[A-Z]{1}[AEIOU]{1}[A-Z]{2}[0-9]{2}" +
        "(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])" +"[HM]{1}"+"(AS|BC|BS|CC|CS|CH|CL|CM|DF|DG|GT|GR|HG|JC|MC|MN|MS|NT|NL|OC|PL|QT|QR|SP|SL|SR|TC|TS|TL|VZ|YN|ZS|NE)" + [B-DF-HJ-NP-TV-Z]{3}" +"[0-9A-Z]{1}[0-9]{1}$";
          Pattern patron = Pattern.compile(regex);
          if(!patron.matcher(curp).matches())
           { return false;
           }else
           { return true;
           }
       }

This function receives as a parameter a string that is the curp and returns true or false depending on whether it is correct or not the curp.

    
answered by 07.03.2017 в 23:57