Show the date in dd-MM-yyyy [duplicated]

0

Hi, I'm doing a program in Java by connecting to a MySql database When you run the program, you have the possibility to see the data that is stored in the database or add new data. The problem is that I see the date in aaaa-MM-dd format and to save data I also have to enter it as well. What could I do to see the date in dd-MM-yyyy ? There would be some possibility to both visualize and enter the date without changing the format in the database. Example that in the BD is saved as yyyy-MM-dd but when viewing the data or entering it is dd-MM-yyyy

Database photo:

Code to check the BD:

public boolean consultarTodo() throws SQLException
    {
        try
        {
            objetoBBDD.resultado = objetoBBDD.sentencia.executeQuery("SELECT * FROM clientes");
            objetoBBDD.resultado.first();   
            return true;
        } catch (SQLException e) {e.printStackTrace();}
        return false;
    }

Method to visualize the data in the TextField:

public void visualizarDatos()
    {

        try {
            jtfcodigoCliente.setText(objetoBBDD.resultado.getString("codCliente"));
            jtfnombre.setText(objetoBBDD.resultado.getString("nombre"));
            jtfapellidos.setText(objetoBBDD.resultado.getString("apellidos"));
            jtffechaNacimiento.setText(objetoBBDD.resultado.getString("fechaNac"));
            jtfdireccion.setText(objetoBBDD.resultado.getString("direccion"));
            jtfpoblacion.setText(objetoBBDD.resultado.getString("poblacion"));
            jtfcodigoPostal.setText(objetoBBDD.resultado.getString("codPostal"));
            jtfprovincia.setText(objetoBBDD.resultado.getString("provincia"));
            jtftelefono.setText(objetoBBDD.resultado.getString("telefono"));
        } catch (SQLException e){e.printStackTrace();}      
    }

Method to add new clients:

public void asignarDatos()
    {
        objClientes.setCodigoCliente(Integer.parseInt(jtfcodigoCliente.getText()));
        objClientes.setNombre(jtfnombre.getText());
        objClientes.setApellidos(jtfapellidos.getText());
        objClientes.setDireccion(jtfdireccion.getText());
        objClientes.setPoblacion(jtfpoblacion.getText());
        objClientes.setCodigoPostal(jtfcodigoPostal.getText());
        objClientes.setProvincia(jtfprovincia.getText());
        objClientes.setTelefono(jtftelefono.getText());
        objClientes.setFechaNacimiento(jtffechaNacimiento.getText());
    }

Customer class:

public class Clientes 
{
    private int codigoCliente;
    private String nombre;
    private String apellidos;
    private String direccion;
    private String poblacion;
    private String codigoPostal;
    private String provincia;
    private String telefono;
    private String fechaNacimiento;

    public Clientes()
    {
        super();
        codigoCliente = 0;
        nombre = new String();
        apellidos = new String();
        direccion = new String();
        poblacion = new String();
        codigoPostal = new String();
        provincia = new String();
        telefono = new String();
        fechaNacimiento = new String();
    }

    public Clientes(int codigoCliente,String nombre,String apellidos,String direccion,String poblacion,String codigoPostal,String provincia,
            String telefono,String fechaNacimiento)
    {
        super();
        this.codigoCliente = codigoCliente;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.direccion = direccion;
        this.poblacion = poblacion;
        this.codigoPostal = codigoPostal;
        this.provincia = provincia;
        this.telefono = telefono;
        this.fechaNacimiento = fechaNacimiento;
    }

    public int getCodigoCliente() {
        return codigoCliente;
    }

    public void setCodigoCliente(int codigoCliente) {
        this.codigoCliente = codigoCliente;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellidos() {
        return apellidos;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public String getDireccion() {
        return direccion;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public String getPoblacion() {
        return poblacion;
    }

    public void setPoblacion(String poblacion) {
        this.poblacion = poblacion;
    }

    public String getCodigoPostal() {
        return codigoPostal;
    }

    public void setCodigoPostal(String codigoPostal) {
        this.codigoPostal = codigoPostal;
    }

    public String getProvincia() {
        return provincia;
    }

    public void setProvincia(String provincia) {
        this.provincia = provincia;
    }

    public String getTelefono() {
        return telefono;
    }

    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }

    public String getFechaNacimiento() {
        return fechaNacimiento;
    }

    public void setFechaNacimiento(String fechaNacimiento) {
        this.fechaNacimiento = fechaNacimiento;
    }
}

Image of the program:

    
asked by Mario Guiber 08.10.2017 в 20:22
source

1 answer

1

You can create a function that returns the date formatted in the format you want. I would recommend creating it in a utility class. For example:

public static String formatFecha(Date fecha) {
    return new SimpleDateFormat("dd-MM-yyyy").format(fecha) 
}

Afterwards, you just have to invoke the function in the visualData method as follows.

jtffechaNacimiento.setText(Utilidades.formatFecha(objetoBBDD.resultado.getDate("fechaNac")));
    
answered by 09.10.2017 / 16:01
source