0

I am creating a chat application and I want to print the date and time in chat bubble whenever the message is sent or received.

I used the below code to get and convert the date and time.

   long itemLong = (long) (chatMessage.getTime()*1000);
    Date itemDate = new Date(itemLong);
    String itemDateStr = new SimpleDateFormat("dd-MMM HH:MM").format(itemLong);
    holder.time.setText(itemDateStr);

The itemLong variable gets the value 1.4847986E15, which is converted to date and timestamp in the format I mentioned in SimpleDateFormat, but eveytime some random date and time gets displayed not at which the message was sent or received.

I tried various solutions but I am unable to get the correct date and time. Any help is appreciated.

Saloni
  • 172
  • 2
  • 15

1 Answers1

3

Edited the code to this and its working fine.

 long itemLong = (long) (chatMessage.getTime()/1000);
    java.util.Date d = new java.util.Date(itemLong*1000L);
    String itemDateStr = new SimpleDateFormat("dd-MMM HH:mm").format(d);
    holder.time.setText(itemDateStr);
Saloni
  • 172
  • 2
  • 15