How to handle a variable date within a loop

1

Problem

I take data from an excel xls document (use jxl libraries), the data goes to text, and the dates must be converted to YYYY-MM-DD.

But when I read them with the application, it takes them in the following way (length of the date varies):

Question

How can I change the format and make YYYY-MM-DD?

Expected result

Month and day with 2 digits.

Code

do{

            codigo_cuenta = hoja.getCell(columna_codigo_cuenta,fila).getContents();
            debe_apertura = "0";
            haber_apertura = "0";
            tipo_comprobante = "U";//hoja.getCell( columna_tipo_comprobante,fila).getContents();
            numero_comprobante =  hoja.getCell(columna_numero_comprobante,fila).getContents();
            fecha_contable =  hoja.getCell(columna_fecha_contable,fila).getContents();
            glosa = hoja.getCell(columna_glosa,fila).getContents();
            deber = hoja.getCell(columna_debe,fila).getContents();
            haber = hoja.getCell(columna_haber,fila).getContents();

            auxiliar = hoja.getCell(columna_numero_comprobante,fila).getContents();
            deber = deber.replace(",",".").replace("-", "0");
            haber = deber.replace(",",".").replace("-", "0");



            //dia = fecha_contable.substring(0,2);
            //anio = fecha_contable.substring(6);
            //mes = fecha_contable.substring(3,5);

            //fecha_contable = anio+"-"+mes+"-"+dia;

                DecimalFormat formatea = new DecimalFormat("###,###.##");
                int debe_punto = Integer.parseInt(deber);
                deber = formatea.format(debe_punto).replace(",",".").replace("-", "");;
                int haber_punto = Integer.parseInt(haber);
                haber = formatea.format(haber_punto).replace(",",".").replace("-", "");



            if (tipo_comprobante.compareTo(detener) != 0)  {                                                 // String impresion = String.format("|%s|%-18s|%-10s|%-40s|%-18s|||||||%s|%s|",tipo_comprobante,numero_comprobante,fecha_contable,glosa,codigo_cuenta,debe,haber);


                //System.out.println("Valor de Auxiliar 1 :"+auxiliar);
                // System.out.println("Valor de Auxiliar 2: "+auxiliar2);
                if(numCuentaAux.compareTo(codigo_cuenta) != 0)
                {
                    numCuentaAux = codigo_cuenta;
                    System.out.println(String.format("|%-20s|%-18s|%-18s|%-1s|%-18s|%-10s|%-40s|||||||||%-18s|%-18s|",codigo_cuenta,debe_apertura,haber_apertura,tipo_comprobante,numero_comprobante,fecha_contable ,glosa,deber,haber));
                    // salArch.printf(String.format("|%-20s|%-18s|%-18s|%-1s|%-18s|%-10s|%-40s|||||||||%-18s|%-18s|",codigo_cuenta,debe_apertura,haber_apertura,tipo_comprobante,numero_comprobante,fecha_contable ,glosa,deber,haber));
                    // salArch.println();

                }
                else
                {
                    System.out.println(String.format("|%-20s|%-18s|%-18s|%-1s|%-18s|%-10s|%-40s|||||||||%-18s|%-18s|","","","",tipo_comprobante,numero_comprobante,fecha_contable ,glosa,deber,haber));
                    // salArch.printf(String.format("|%-20s|%-18s|%-18s|%-1s|%-18s|%-10s|%-40s|||||||||%-18s|%-18s|","","","",tipo_comprobante,numero_comprobante,fecha_contable ,glosa,deber,haber));
                    // salArch.println();
                }




            }
            fila = fila +1;
            this.txt_contador.setText(Integer.toString(fila));
            // fila < hoja.getRows() usar cuando el doc termina en nada importante !! getRow Salva la vida!!
        }while(fila < hoja.getRows() );

        System.out.println("Termine!");
        salArch.close();


    } catch (IOException | BiffException ex) {
        Logger.getLogger(VENTANA_PRINCIPAL.class.getName()).log(Level.SEVERE, null, ex);
    }




}
    
asked by Felipe Inostroza 22.08.2016 в 05:49
source

1 answer

1
  

But when I read them with the application, it takes them in the following way (length of the date varies):

In order for java to accept both the dates with only one digit and those with 2 for the month and day, you do not need to do it by hand, it is worth it with a SimpleDateFormat("d/M/yy") as parseador .

  

How can I change the format and make YYYY-MM-DD?

With a formateador , in this case SimpleDateFormat("yyyy/MM/dd") .

Sample data:

private static final String[] fechas = {
    "1/1/16",
    "10/1/16",
    "1/10/16",
    "12/10/16"
}; 

Pacing and formatting within a loop:

for (String fecha : fechas) {
    try {
        Date d = parseador.parse(fecha);
        System.out.println(formateador.format(d));
    } catch (ParseException e) {
        System.out.println("fecha " + fecha + " no valida");
    }
}

Result:

2016/01/01
2016/01/10
2016/10/01
2016/10/12

DEMO IN IDEONE

    
answered by 22.08.2016 / 10:16
source