0

I want to set a DateTime every time that I make an entry and show it. I'm not able to show the right DateTime. It shows me 1st January 1970 + HH MM and SS. The idea is every time I make an entry, it will print the DateTime this entry was made, like a history.

Here my code:

   @Override
public void onBindViewHolder(ViewHolder holder, int position) { 
    FirebaseEntry xx= mDataset.get(position);
    Date date = new Date(Long.parseLong(xx.getDate()));
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String dateInCorrectFormat = (dateFormat.format(date));
    holder.mDate.setText(dateInCorrectFormat);
}

Thanks for the help.

esQmo_
  • 1,464
  • 3
  • 18
  • 43
Chris
  • 9
  • 3
  • Date currentTime = Calendar.getInstance().getTime(); – Ravi Kilnake Aug 23 '18 at 15:41
  • Thanks @RaviKilnake, but that's not what I looking for, It gives me the current time in the entries, I want to leave the DateTime that it was created the entry. – Chris Aug 23 '18 at 15:49
  • 1
    what do you mean :/ – Ravi Kilnake Aug 23 '18 at 15:52
  • For Example: If I created an entry at 12/03/2018 18:00:20 and then another one at 13/03/2018 12:33:45. When I check the history of the entries I will see those dateTime. It doesn't matter what date I check it, always I will see those dates and I won't see the current date. – Chris Aug 23 '18 at 15:56
  • Is `xx.getDate()` to return the time the entry was created? If so please include at least one example of its return value and the date and time you want to print. Best [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Ole V.V. Aug 24 '18 at 09:14
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 24 '18 at 09:14
  • Possibly related: [Firebase TIMESTAMP to date and Time](https://stackoverflow.com/questions/34718668/firebase-timestamp-to-date-and-time) – Ole V.V. Aug 24 '18 at 09:19

2 Answers2

0

Try this

Long current = System.currentTimeMillis();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String date = df.format(current);

Now add this date to your db. And use it. Don't get the current date when you are showing the details. Instead, show the date stored in the db.

NirmalCode
  • 2,140
  • 1
  • 14
  • 19
0

Is it showing you the first date: 1st January 1970 + HH MM and SS, because there is a problem with the time, Java is expecting the long value in miliseconds and you probably have the time in seconds.

It happened to me, try this or make sure you have the time in miliseconds:

Long miliseconds = Long.parseLong(md.getDate());
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateInCorrectFormat = dateFormat.format(miliseconds*1000);
Alex Bean
  • 493
  • 7
  • 19