Problem displaying date on Android

1

When displaying a date that was entered into the database, of type DateTime , the app shows the current date:

Iwouldalsoliketoconvertthisdatetodd/mm/yyyytofitaportraitlayout.

Partofthecodethatdealswiththislist:

publicList<Despesa>getLista(){Cursorc=getWritableDatabase().query(TABELA,COLUNAS,null,null,null,null,null);List<Despesa>lista=newArrayList<>();while(c.moveToNext()){java.util.DateparsedDate=newjava.util.Date();Stringa=c.getString(1);SimpleDateFormatdateFormat=newSimpleDateFormat("dd-MM-yyyy", Locale.US);
        SimpleDateFormat outputDate = new SimpleDateFormat("yyyy-MM-dd");

        try {
            parsedDate = dateFormat.parse(a);
            a = parsedDate.toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
     //   String returnDate=outputFormatTime.format(inputFormat);

        Despesa despesa = new Despesa();
        despesa.setValor(c.getFloat(0));
        despesa.setData(parsedDate);
        despesa.setDescricao(c.getString(2));
        despesa.setPago(c.getString(3).equalsIgnoreCase("TRUE"));
        despesa.setIdSubgrupo(c.getInt(4));
        despesa.setId(c.getInt(5));

        lista.add(despesa);
    }
    c.close();

    return lista;
}

What's wrong with the code? The String a variable is given the correct date value but is not of type Date , how to convert it to Date , and display it in the format dd-mm-yyyy ?

GetView of the Adapter

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    final int auxPosition = position;

    LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final RelativeLayout layout = (RelativeLayout)
            inflater.inflate(R.layout.row, null);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(context, EditaDespesaActivity.class);
            intent.putExtra("valor", lista.get(auxPosition).getValor());
            intent.putExtra("data", lista.get(auxPosition).getData());
            intent.putExtra("descricao", lista.get(auxPosition).getDescricao());
            context.startActivity(intent);
        }
    });

    TextView data = (TextView)
            layout.findViewById(R.id.ddata);
    data.setText(lista.get(position).getData().toString());

    TextView desc = (TextView)
            layout.findViewById(R.id.ddesc);
    desc.setText(lista.get(position).getDescricao());

    TextView valor = (TextView)
            layout.findViewById(R.id.vvalor);
    valor.setText(lista.get(position).getValor().toString());

    return layout;
}

Method to save expense

public void salvarDespesa(View view){

    String[] data = etData.getText().toString().split("/");

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    despesa.setValor(Float.valueOf(etValor.getText().toString().substring(2,etValor.getText().toString().length()).replace(",", ".")));
    try {
        despesa.setData(dateFormat.parse(data[2]+ "-" + data[1]+ "-" + data[0]));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    despesa.setDescricao(etDesc.getText().toString());

    DespesaDAO despesaDAO = new DespesaDAO(this);
    despesaDAO.inserir(despesa);

    Toast.makeText(this,"Despesa adicionada!", Toast.LENGTH_SHORT).show();

    finish();
}
    
asked by anonymous 20.01.2015 / 14:12

1 answer

3

To format a Date object in a more human way, just use SimpleDateFormat .

In the constructor of your Adapter just create an instance:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

And use in your getView to format correctly:

data.setText(sdf.format(lista.get(position).getData()));

Obs : If you want to display the correct date formatting for the device language, just use one of the static methods:

DateFormat.getDateInstance()
DateFormat.getDateTimeInstance()
DateFormat.getTimeInstance()

The only difference is having to deal with the superclass ( DateFormat ) instead of the subclass.

Anything is enough to look at the documentation with several examples: link

    
20.01.2015 / 21:20