Convert String from Jtextfield to Calendar

Asked

Viewed 587 times

5

How to perform input conversion via Swing’s Jtextfield via JDBC ?

public class DadosPessoais {

    private Calendar dataNascimento;

    public Calendar getDataNascimento() {
        return dataNascimento;
    }
    public void setDataNascimento(Calendar dataNascimento) {
        this.dataNascimento = dataNascimento;
    }
}

Screen class:

public class DadosPessoaisForm extends JFrame{

private JLabel jlDataNascimento;
    //private JTextField jtfDataNascimento;
    private JButton jbSave,jbUpdate;
    private JFormattedTextField jftfDataNascimento;
    private MaskFormatter mfDataNascimento;

try {
        mfDataNascimento = new MaskFormatter("##/##/####");
        mfDataNascimento.setPlaceholderCharacter('_');
    } catch (ParseException e) {
        e.printStackTrace();
    }
    jftfDataNascimento = new JFormattedTextField(mfDataNascimento);

    //

    jbSave = new JButton("Salvar");
    jbUpdate = new JButton("Editar");

    jbSave.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                onSaveDadosPessoais();
            }
        });
    jbUpdate.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                onUpdateDadosPessoais();
            }
        });

    private void onSaveDadosPessoais(){
        DadosPessoais dadosPessoais = new DadosPessoais();

        //

        dadosPessoais.setDataNascimento(Calendar.getInstance());

        //

    }

    private void onUpdateDadosPessoais() {
        DadosPessoais dadosPessoais = new DadosPessoais();

        //

        jtfDataNascimento.setText(dadosPessoais.getDataNascimento());

        //
    }
}

DAO class:

public class DadosPessoaisDAO{

    public int save(DadosPessoais dadosPessoais){

        //

        stmt.setDate(5, (new Date(pessoaDadosPessoais.getDataNascimento().getTimeInMillis())));

        //
    }

}
  • How is the jtextfield data coming? What format? Where should the conversion be done? You left a lot of details missing from the question. See the duplicate, maybe she’ll answer you.

  • I am not formatting, I am not using Jformattedtextfield or Maskformat. As it is in the code, I am not performing any conversion, the way you are saving the system time because it is a field that accepts null.

  • By the code shown, you are passing a direct Legend instance to the text field. Add a print of this field to the question, or add a [mcve], otherwise it is difficult to help.

  • I just finished editing. If the field is enabled or disabled, the user entering values or not, as is the case in the 2016-06-11 format in the database.

  • I understand, but what problem faced?

  • I want the user to type the dataDeNascimento in the form dd/MM/yyy format for the DAO. "Sorry for the inexperience or poor formulation of the questions and expressions".

  • But for this, you will need to use maskformatter, so the bars are placed automatically. Or you just want to validate the date he typed?

  • the maskformatter is enough.

  • You can change jtextfield to jformatterfield?

  • but how I perform the conversion in DAO ?

  • Well, now I understand less your problem, the field will no longer be filled? Why this line here jtfDataNascimento.setText(dadosPessoais.getDataNascimento()); if the user is going to type?

  • managed in Form : Try { mfDataNascimento = new Maskformatter("#/##/##/#######"); mfDataNascimento.setPlaceholderCharacter('_'); } catch (Parseexception e) { e. printStackTrace(); } jftfDataNascimento = new Jformattedtextfield(mfDataNascimento); now I need conversion on DAO

Show 7 more comments

1 answer

1


Just grab the string from your JFormattedTextField, and make a parse using SimpleDateFormat:

String strData = jftfDataNascimento.getText();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
cal.setTime(sdf.parse(strData));

If you need to transfer to your class DadosPessoais:

dadosPessoais.setDataNascimento(cal);

And then to save in the bank:

stmt.setDate(5, (new java.sql.Date(dadosPessoais.getDataNascimento().getTimeInMillis())));

Care must be taken because there are two Date classes: one belongs to the java.util package and another one belongs to the java.sql package used by JDBC.

However, it is recommended that you adapt to the package classes java.time of , especially if, at some future time, you intend to operate between dates. Among the links below are several examples of use of these classes, and also an explanation of why not use the Date and Calendar classes.


References:

How to migrate from Date and Calendar to the new Java 8 Date API?

Convert String to Calendar Object in Java

Difference between Date, sql. Date and Calendar

Problems comparing dates with Calendar

Difference of dates with Legend in a Textfield

  • Sorry for the delay in returning. I got it. Thanks for the help.

Browser other questions tagged

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