SoFunction
Updated on 2025-03-11

Analysis of the time display example of Android programming SMS list

This article describes the time display of the SMS list for Android programming. Share it for your reference, as follows:

The display of the time of the Android text message is very fine. First, the text message time stored in the text message database is a long-type number. After the query action is finished, after obtaining this value, a conversion will be performed. The specific conversion action is completed in the formatTimeStampString function;

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();
  (when);
  Time now = new Time();
  ();
  // 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 ( != ) {
   format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
  } else if ( != ) {
   // 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 (context, when, format_flags);
}

From the second specific implementation function, it can be seen that Android determines the display time format based on the current time as the basis for comparison:

1. If the year in the current text message time is inconsistent with the current year on the phone, the year, month and day will be displayed, and the specific minutes will not be displayed, 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, only the month and day are displayed, such as: June 29th;

3. If it is a text message of the same day, it will calculate whether it is a text message in the morning or afternoon, and the text message recorded at the same time is displayed, such as: 12:55 pm;

After comprehensive consideration, this display design is still very reasonable

I hope this article will be helpful to everyone's Android programming design.