Format of dates in Java

1

I have a problem with the format of the date in the java language, I want to format it in DD / MM / YYYY but it does not allow me, I format it as MM / DD / YYYY I have tried with the

  

new SimpleDateFormat ("dd / MM / yyyy"). format (getDate ())

But he will not let me. This is the code:

for (InfoTarjeta t : list) {
                tblImpresion.addItem(
                        new Object[] { t.getRut(), t.getDv(), t.getApellidoPaterno(), t.getApellidoMaterno(),
                                t.getNombres(), t.getNumeroTarjeta(), t.getTipoProducto().getDescripcion(),
                                t.getTipoSolicitud().getDescripcion(), new SimpleDateFormat("dd/MM/yyyy").format(t.getFechaEmision()), t.getNumeroAutentia() },
                        null);
            }

Does anyone know about a possible solution in Java?     thanks.

    
asked by Luis Eduardo Martinez Ocoro 03.05.2017 в 18:11
source

2 answers

0

The Date object can be converted to any string format using a SimpleDateFormat see the following example, which will build a string representation of the date in the format dd / MM / yyyy, which seems to be the format that search.

SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy");
String fechaFormato = formatFecha.format(t.getFechaEmision());

In your case, something like that should work.

SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy");
    for (InfoTarjeta t : list) {
                    tblImpresion.addItem(
                            new Object[] { t.getRut(), t.getDv(), t.getApellidoPaterno(), t.getApellidoMaterno(),
                                    t.getNombres(), t.getNumeroTarjeta(), t.getTipoProducto().getDescripcion(),
                                    t.getTipoSolicitud().getDescripcion(), formatFecha.format(t.getFechaEmision()), t.getNumeroAutentia() },
                            null);
                }

or

    SimpleDateFormat formatFecha = new SimpleDateFormat("dd/MM/yyyy");
    String fechaFormato = formatFecha.format(t.getFechaEmision());

        for (InfoTarjeta t : list) {
                        tblImpresion.addItem(
                                new Object[] { t.getRut(), t.getDv(), t.getApellidoPaterno(), t.getApellidoMaterno(),
                                        t.getNombres(), t.getNumeroTarjeta(), t.getTipoProducto().getDescripcion(),
                                        t.getTipoSolicitud().getDescripcion(), fechaFormato , t.getNumeroAutentia() },
                                null);
                    }
    
answered by 03.05.2017 / 20:15
source
0

You have an error while converting

new SimpleDateFormat("dd/MM/yyyy").format(t.getFechaEmision())

If done this way:

new SimpleDateFormat(FORMATO INICIAL).format(new SimpleDateFormat(FORMATO DESEADO).parse(FECHA A CONVERTIR)));

Convert date from one format to another in Java.

I give as an example this method that receives the format of input, output and the string with the date to be converted. You can also define a LOCALE for example:

  public static final Locale LOCALE_MX = new Locale("es", "MX");

and this would be the method:

public static String dateFormatter(String inputFormat, String outputFormat, String inputDate){
      //Define formato default de entrada.   
      String input = inputFormat.isEmpty()? "yyyy-MM-dd hh:mm:ss" : inputFormat; 
      //Define formato default de salida.
      String output = outputFormat.isEmpty()? "d 'de' MMMM 'del' yyyy" : outputFormat;
    String outputDate = inputDate;
    try {
        outputDate = new SimpleDateFormat(output, LOCALE_MX).format(new SimpleDateFormat(input, LOCALE_MX).parse(inputDate));
    } catch (Exception e) {
        System.out.println("dateFormatter(): " + e.getMessage());           
    }
    return outputDate;
}

These are examples of how to use the method, where I define the format that has my original date "MM / dd / yyyy", the format I want to obtain "dd / MM / yyyy" and the date that I want to change format:

   System.out.println("FECHA :" + dateFormatter("MM/dd/yyyy","dd/MM/yyyy", "05/03/2017"));

would get:

FECHA: 03/05/2017

another example:

    System.out.println("FECHA: " + dateFormatter("yyyy-MM-dd hh:mm:ss","dd/MM/yyyy", "2017-05-03 12:24:34"));

in which you would get the format you want:

FECHA: 03/05/2017

You can even modify the output format:

 System.out.println("FECHA: " + dateFormatter("yyyy-MM-dd hh:mm:ss","d 'de' MMMM 'del' yyyy", "2017-05-03 12:24:34"));

and you would get:

FECHA: 3 de mayo del 2017
    
answered by 03.05.2017 в 20:38