Search code examples
javaswinggetdatejcalendar

Jdatechooser content


im trying to get de date from a a jdatechooser, then convert it to a string, but when I try to do this this.fechaTxt.setText(""); it doesnot let me

        String fecha1 = this.fechaTxt.getDate().toString();

        Viaje viaje = new Viaje();

        viaje.setFecha(fecha1);


        try {
            this.ingresarViaje(viaje);

            this.fechaTxt.setText("");

            JOptionPane.showMessageDialog(null, "Viaje Ingresado");

        } catch (Exception e) {
            System.out.println("ERROR" + e.getMessage());

}

Solution

  • Start by getting the actual date from the JDateChooser...

    Date date = fechaText.getDate();
    

    Then you could use DateFormat or a SimpleDateFormat to format as a String

    Just remember, unless you need to display the date for some reason, you really shouldn't convert it to a String, but instead leave it as a Date object