Copy the date of a JTextfield to a JDateChooser

-1

I currently mirror JDateChooser to JTextField , but now I need to mirror from JTextField to JDateChooser . How do I copy and convert a date from a text field to JDateChooser?

I use this way to send to JTextField

    String dia = Integer.toString(JDateChooser1.getCalendar().get(Calendar.DAY_OF_MONTH));
    String mes = Integer.toString(JDateChooser1.getCalendar().get(Calendar.MONTH) + 1);
    String year = Integer.toString(JDateChooser1.getCalendar().get(Calendar.YEAR));
    String fecha = (dia + "/" + mes + "/" + year);
    EntradadadosData.setText(fecha);
    
asked by anonymous 01.09.2017 / 17:49

1 answer

1

If the goal is to convert the date of the text field, try as below:

Date date = new SimpleDateFormat("dd/MM/yyyy").parse(seutextfield.getText());

JDateChooser1.setDate(date);
Note that taking user input from a text field and converting to date with no treatment or restriction will cause problems that can pop up in parse exceptions if it can not be guaranteed that the values are really valid dates. If the field is read-only, filled with date coming from another place where it is guaranteed that the information is actually a date, this problem can be discarded.

    
01.09.2017 / 18:17