Problem getting date from screen

Asked

Viewed 53 times

-1

I have a small error when picking a jtextfield date, error occurs:

String cannot be converted to date

Follows my code:

public TelaControle getVendaFromTela() throws ParseException {

    Venda vDaTela = new Venda();

    vDaTela.setCliente(inpCliente.getText());
    vDaTela.setCarro(inpCarro.getText());
    vDaTela.setData(inpData.getText());

    return vDaTela;
    }
  • What is this variable question? Where does this getDataAtualDate come from()?

  • If inpData is Jtextfield, how can it have the field "sdf": "inpData.sdf"?

  • Inpdata is the Jtextfield component that the user inserts the date.

  • What getDataAtualDate() does there?

  • In theory getDataAtualDate() is to pick up the current date

  • you have to do "inpData.gettext()" to take the date entered by the user as String, and then process it (validate and format it), then put it in vDaTela.

  • I don’t understand anything, why are you taking the current date from somewhere else, and ,mixing it with a supposed date typed by the user and applying it to a model? Edit the question and explain better what you intend to do, or what this code was supposed to do.

  • I just edited the question. I expressed myself badly when trying to explain

  • is getData or setData?

  • the problem is in getData

  • @diegofm He just wants to convert a String as "04/20/2016" to a corresponding Date Object, not Eduardo?

  • correct Douglas

  • But there’s nothing getdata in that code.

  • I edited the question. I think it’s now clearer...

Show 9 more comments

1 answer

2


If the problem is converting data into string for Date type, the initial code was going the right way:

public TelaControle getVendaFromTela() throws ParseException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    Venda vDaTela = new Venda();

    vDaTela.setCliente(inpCliente.getText());
    vDaTela.setCarro(inpCarro.getText());
    vDaTela.setData(sdf.parse(inpData.getText()));

    return vDaTela;
}

Only to convert string to Date the correct method is parse, the format does the opposite.

Browser other questions tagged

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