Value conversion Date no sqlite Android error The method gettext() is Undefined for the type String

Asked

Viewed 38 times

1

I am doing the insertion of a DATE type value in my project, for that I had to convert it, in my corresponding class, my Dt_reading tribute, is declared as string and in the database it is as Date. For this, the moment I am entering the register in the bank, I am converting it, as below:

public Consumo inserir (Consumo consumo){

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    dateFormat.applyPattern("yyyy-MM-dd");
    Date data =  (Date) dateFormat.parse(consumo.dt_leitura.getText());  


        ContentValues valores = new ContentValues();

        valores.put("dt_leitura",dateFormat.format(data));  
        valores.put("registro", consumo.getRegistro());

        consumo.setId(db.insert("consumo", null, valores ));
        return consumo;


}

Only on the stretch

Date data =  (Date) dateFormat.parse(consumo.dt_leitura.getText());

is returning the following error:

The method gettext() is Undefined for the type String

Could you tell me if I am also making the correct date conversion?

1 answer

1


The error says that the String class has no method with the name gettext

The method parse() class Simpledateformat receives a String, as you say in the question, the attribute consumo.dt_leitura is a String, so pass it directly to the method parse():

Date data =  (Date) dateFormat.parse(consumo.dt_leitura);
  • Hello extension, Thanks for the attention and reply Unfortunately after I changed the way you showed me Date data = (Date) dateFormat.parse(consumption.dt_reading); still returns me the error Unhandled Exception type Parseexception, would know tell me the reason why ?

  • Without knowing what consumo.dt_leitura returns/contains no.

  • Amaral, it worked out the way it did, actually this mistake I mentioned above, the application was only informed that it needed a Try catch, put and worked. thank you

Browser other questions tagged

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