Copy a Jtextfield date to a Jdatechooser

Asked

Viewed 279 times

-1

Currently I mirror the JDateChooser for a JTextField, but now I need to mirror a JTextField for a JDateChooser. How to copy and convert a date from a text field to Jdatechooser?

I use it this way to send to the 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);
  • You tested the solution below?

1 answer

1


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

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

JDateChooser1.setDate(date);

Note the fact that taking user input from a text field and converting it to date, without any treatment or restriction, will cause problems that can burst into parse exceptions, if it cannot be guaranteed that the values are actually valid dates. If the field is read-only, filled with date from another place where it is guaranteed that the information is actually a date, this problem can be discarded.

  • Sorry Articuno, I could only see it now. It worked, yes, I just had to change the date format. I had already thought about this possibility of the person sending the wrong date, but I did it in a way that will not happen.

  • I have a little doubt and would like to know if you have a post related to my problem. I’m needing now when a person changes an item from a combobox, it clear that field of date and a few more. for which the person is forced to enter a new date.

  • @Rafaelchaves yes, see this answer: https://answall.com/a/117903/28595

  • perfect! two birds on one stone. Vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.