Millennium change date format error

3

When reading the date of birth of the CURP, and formatting it ( yyyy-MM-dd ), when entering a CURP with date of birth (example: XXXX270503XXXXXXXX ) the formatter instead of putting the date as 1927-05-03 puts it as 2027-05-03 , causing it to fail.

How can I solve this?

This is my code.

        String fechaNacimiento = request.getCurp().substring(4, 10);
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat requiredFormat1 = new SimpleDateFormat(
                "yyyy-MM-dd");
        Date fecha;
        try {
            fecha = dateFormat1.parse(fechaNacimiento);
            fechaNacimiento = requiredFormat1.format(fecha);
        } catch (ParseException e) {
            throw new Exception("DAFE", e.getMessage(), 500);
        }
    
asked by Luis 03.08.2017 в 00:14
source

2 answers

4

According to the page link to know the year 2000 or 1999 the digit 17 is used, if it is 0-9 - > 19xx and if it's A-Z - > 20xx so it would be like this:

String fechaNacimiento = request.getCurp().substring(4, 10);
if(StringUtils.isNumeric(fechaNacimiento.substring(17,18))){
    fechaNacimiento = "19" + fechaNacimiento;
}else{
    fechaNacimiento = "20" + fechaNacimiento;
}


SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat requiredFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date fecha;
try {
    fecha = dateFormat1.parse(fechaNacimiento);
    fechaNacimiento = requiredFormat1.format(fecha);
} catch (ParseException e) {
    throw new Exception("DAFE", e.getMessage(), 500);
}
    
answered by 03.08.2017 в 22:15
2

Taking into account the information that @Ajeno provides regarding the CURP another way to solve it would be through SimpleDateFormat # set2DigitYearStart (date )

set2DigitYearStart(startDate) allows to indicate that the first 2 digits will be taken into account during the pairing of a date and if we format with the abbreviated format yy will build the date from those two first digits.

The startDate parameter determines a range of dates to consider :

  

startDate - During parsing, two digit years will be placed in the range startDate to startDate + 100 years.

That is, calling it with 1900 will take the period 1900-2000 and invoking it with 2000 will take 2000-2100 , in any case SimpleDateFormat will locate the date with format yy/mm/dd within that range.

Examples (pseudocodigo):

df.set2DigitYearStart(2000)
df.format(88/5/12) -> 2088/5/12

df.set2DigitYearStart(1970)
df.format(70/5/12) -> 2070/5/12

df.set2DigitYearStart(1970)
df.format(90/5/12) -> 1990/5/12 

Note that in the latter case as the period is from 1970 - 2070 and does not exist in that period the year 2090 takes 1990

Demo

Solution to the problem that the OP raises :

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
String fechaNacimiento= "03/05/27";
Calendar cal = Calendar.getInstance();
if(request.getCurp().substring(17,18).matches("[0-9]+")){
    cal.set(Calendar.YEAR, 1900); // si es un digito corresponde a los 19XX
}else{
    cal.set(Calendar.YEAR, 2000); // sino, corresponde a 20XX
}
dateFormat.set2DigitYearStart(cal.getTime()); // a partir de este punto dateFormat conoceque dos primeros digitos tomar al parsear
cal.setTime(dateFormat.parse(fechaNacimiento));
System.out.println(cal.getTime());

What happens if we do not explicitly state the century? In the documentation of SimpleDateFormat is mentioned:

  

For parsing with the abbreviated year pattern ("and" or "and" and "),   SimpleDateFormat must interpret the abbreviated year relative to some   century It does this by adjusting dates to be within 80 years before   and 20 years after the time of the SimpleDateFormat instance is created.   For example, using a pattern of "MM / dd / yy" and a SimpleDateFormat   instance created on Jan 1, 1997, the string "11/1/12" would be   interpreted as Jan 11, 2012 while the string "04/05/64" would be   interpreted as May 4, 1964.

That is, SimpleDateFormat will take into account the period (fecha creación - 80 años) to (fecha creación + 20 años) and depending on which century the date to be parsed will determine which first two digits to use.

    
answered by 03.08.2017 в 23:09