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.