Date Format

You would require to display the date and time for messages on certain places. Applozic provides a utility class DateUtils to get the different formats for date and time to use in different places. Every message object in Applozic consists of the createdAtTime field which holds the time in milliseconds for when the message was created on the server.

Below are different types of formats which you may require to display:

  1. Latest message time on Conversation list
  2. Last seen time in chat thread
  3. Date difference in chat thread

Follow the below steps to get the date and time formats:

Adding the texts in strings.xml file

Add the below texts in your string.xml file

<string name="JUST_NOW">Just now</string>
    <string name="YESTERDAY">Yesterday</string>

    <plurals name="HOURS">
        <item quantity="one">%d hr</item>
        <item quantity="other">%d hrs</item>
    </plurals>

    <plurals name="MINUTES">
        <item quantity="one">%d min</item>
        <item quantity="other">%d mins</item>
    </plurals>

    <plurals name="MINUTES_AGO">
        <item quantity="one">%d min ago</item>
        <item quantity="other">%d mins ago</item>
    </plurals>

    <plurals name="HOURS_AGO">
        <item quantity="one">%d hr ago</item>
        <item quantity="other">%d hrs ago</item>
    </plurals>

Get the formatted date

Call the below method to get the date for conversation list .

DateUtils.getFormattedDateAndTime(context, message.getCreatedAtTime(), R.string.JUST_NOW, R.plurals.MINUTES, R.plurals.HOURS);

The above code would return the time as Just now, 2 min ago, 1 hr ago etc.

Call the below method to display the Last seen at time for a user:

"Last seen at" + DateUtils.getDateAndTimeForLastSeen(getContext(), contact.getLastSeenAt(), R.string.JUST_NOW, R.plurals.MINUTES_AGO, R.plurals.HOURS_AGO, R.string.YESTERDAY)

Call the below method to display the time between messages in a chat thread:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy");
                SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("EEEE");
                Date date = new Date(message.getCreatedAtTime());

if (DateUtils.isSameDay(message.getCreatedAtTime())) {
                    myViewHolder2.dayTextView.setVisibility(View.VISIBLE);
                    myViewHolder2.dateView.setVisibility(GONE);
                    myViewHolder2.dayTextView.setText(R.string.today);
                } else {
                    myViewHolder2.dayTextView.setVisibility(View.VISIBLE);
                    myViewHolder2.dateView.setVisibility(View.VISIBLE);                  myViewHolder2.dayTextView.setText(simpleDateFormatDay.format(date));
           myViewHolder2.dateView.setText(simpleDateFormat.format(date));
                }

Did this page help you?