SoFunction
Updated on 2025-03-11

Android programming implements the conversion of time into tool classes such as a few minutes ago, a few days ago, etc.

This article describes the Android programming implementation converts time into tools such as a few minutes ago, a few days ago, etc. Share it for your reference, as follows:

describe:

When developing a client on Android, it will show how long it was, such as 10 minutes ago, 8 hours ago, one month ago, etc. Here is a tool class.

Code:

public class TimeUtil {
  private final static long minute = 60 * 1000;// 1 minute  private final static long hour = 60 * minute;// 1 hour  private final static long day = 24 * hour;// 1 day  private final static long month = 31 * day;// moon  private final static long year = 12 * month;// Year  /**
    * Returns the date of the text description
    *
    * @param date
    * @return
    */
  public static String getTimeFormatText(Date date) {
    if (date == null) {
      return null;
    }
    long diff = new Date().getTime() - ();
    long r = 0;
    if (diff > year) {
      r = (diff / year);
      return r + "New Year's Eve";
    }
    if (diff > month) {
      r = (diff / month);
      return r + "Monthly ago";
    }
    if (diff > day) {
      r = (diff / day);
      return r + "Day Ago";
    }
    if (diff > hour) {
      r = (diff / hour);
      return r + "Hours ago";
    }
    if (diff > minute) {
      r = (diff / minute);
      return r + "Minutes ago";
    }
    return "just";
  }
}

PS: Here are a few online tools for your reference:

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online Perpetual Calendar:
http://tools./bianmin/wannianli

Online Lunar/Gregorian calendar conversion tool:
http://tools./bianmin/yinli2yangli

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about Android related content, please check out the topic of this site:Android date and time operation skills summary》、《Android development introduction and advanced tutorial》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

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