Return DataHora Updated Android

1

I made a class called DataHoraAtual containing this data:

package com.projeto.projetov1.model;

import java.text.SimpleDateFormat;

public class DataHoraAtual {
    long date = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    String dateString = sdf.format(date);
}

And in my main class, when the event is created it has this code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//aplica data hora atual do sistema no app
txtDataHoraOcorrencia = (EditText) 
findViewById(R.id.txtDataHoraOcorrencia);
DataHoraAtual dataHoraAtual = new DataHoraAtual();
txtDataHoraOcorrencia.setText(dataHoraAtual.toString());
}

Doing so the date and time is not displayed, but the following appears:

  

with.project.projectv1.model.database=4f3b05b

But if I do so, without calling the class the date and time appears:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//aplica data hora atual do sistema no app
txtDataHoraOcorrencia = (EditText) findViewById(R.id.txtDataHoraOcorrencia);
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String dateString = sdf.format(date);
txtDataHoraOcorrencia.setText(dataHoraAtual.toString());
}

What am I doing wrong?

Doing the procedure suggested by Valdeir presented the following:

    
asked by anonymous 16.12.2017 / 23:28

2 answers

1

If you want the method toString() to return the value of dateString you must overwrite it:

public class DataHoraAtual {
    long date = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    String dateString = sdf.format(date);

    @Override
    public String toString(){
        return dateString;
    }
}
    
16.12.2017 / 23:37
1

It is not working because you are getting the hash of the class.

For your code to work the way you want it, you need to call the value of the dateString attribute

This way:

txtDataHoraOcorrencia.setText(dataHoraAtual.dateString);
    
16.12.2017 / 23:38