Transform Date to String and print it to a JDateChooser

2

I need your help, I have searched for different solutions on the internet but I have not found the correct one. My question is how can I transform from Date to String by obtaining the date from my MySQL database?

The solution I found is using:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date fecha = new Date();
String F = sdf.format(fecha);

But that solution brings me today's date and I do not need that, I need to get the date that is stored in my BD. In my method cargarDatos() I do the following:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date fecha = new Date();
String F = sdf.format(fecha);
producto.setFechaVencimiento(F);

My attribute fechaVencimiento is of type String , so I need to format the date of type Date to a String of that way I can format it correctly but I only get the current date.

To bring the date of my BD in my method obtener() I do:

while (rs.next()) {
   Date fecha = new Date(rs.getDate("fechaVencimiento").getTime());
   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
   String f = sdf.format(fecha);
   System.out.println(f);
   jdcFechaVencimiento.setDateFormatString(f);
}

In this way, if I print my f object in console, I get the stored date which is 06/20/2019 , so all right! My problem is to print the date in my JDateChooser that is formatted in the same way in my method cargarDatos() .

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date fecha = new Date();
String F = sdf.format(fecha);
producto.setFechaVencimiento(F);

I would greatly appreciate your help. Thanks again.

    
asked by Gerardo Ferreyra 05.07.2017 в 03:11
source

1 answer

1

Seeing some examples I think that you can simplify this part, apart from the fact that you are doing something wrong, I leave you my code and you tell me if it works.

       while (rs.next()) {             
            jdcFechaVencimiento.setDateFormatString("dd/MM/yyyy"); // Sirve para mostrar el formato en el date chooser
            jdcFechaVencimiento.setDate(new Date(rs.getDate("fechaVencimiento").getTime())); // la fecha como tal
        }

greetings !!

    
answered by 05.07.2017 / 18:02
source