Android programming short message list time display example analysis

  • 2020-10-23 21:13:02
  • OfStack

This article illustrates the time display of Android programming short message lists. To share for your reference, the details are as follows:

The display of the time of Android's SMS messages is very detailed. First, the SMS time saved in ES5en.db is the number of Long type. After the query action is over, the conversion will be made.


public static String formatTimeStampString(Context context, long when) {
  return formatTimeStampString(context, when, false);
}
public static String formatTimeStampString(Context context, long when, boolean fullFormat) {
  Time then = new Time();
  then.set(when);
  Time now = new Time();
  now.setToNow();
  // Basic settings for formatDateTime() we want for all cases.
  int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |
       DateUtils.FORMAT_ABBREV_ALL |
       DateUtils.FORMAT_CAP_AMPM;
  // If the message is from a different year, show the date and year.
  if (then.year != now.year) {
   format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
  } else if (then.yearDay != now.yearDay) {
   // If it is from a different day than today, show only the date.
   format_flags |= DateUtils.FORMAT_SHOW_DATE;
  } else {
   // Otherwise, if the message is from today, show the time.
   format_flags |= DateUtils.FORMAT_SHOW_TIME;
  }
  // If the caller has asked for full details, make sure to show the date
  // and time no matter what we've determined above (but still make showing
  // the year only happen if it is a different year from today).
  if (fullFormat) {
   format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
  }
  return DateUtils.formatDateTime(context, when, format_flags);
}

As you can see from the second function, Android decides the time format to display based on the current time for comparison:

1. If the current text message time is different from the current year of the mobile phone, the year, month and day will be displayed instead of the specific time and number, such as 2010-6-30;

2. If the time of the text message is the same year as the current time of the mobile phone, but not the same day, then only the month date will be displayed, such as: June 29;

3. If it is a text message of the day, the accountant can calculate it as a text message of the morning or the afternoon, and display the time and minutes of the recorded text message, such as: 12:55 p.m.;

Taken into account, such a display design is very reasonable

I hope this article has been helpful in Android programming.


Related articles: