<bdo id='1sLns'></bdo><ul id='1sLns'></ul>
  • <tfoot id='1sLns'></tfoot>
    <legend id='1sLns'><style id='1sLns'><dir id='1sLns'><q id='1sLns'></q></dir></style></legend>

    <small id='1sLns'></small><noframes id='1sLns'>

      1. <i id='1sLns'><tr id='1sLns'><dt id='1sLns'><q id='1sLns'><span id='1sLns'><b id='1sLns'><form id='1sLns'><ins id='1sLns'></ins><ul id='1sLns'></ul><sub id='1sLns'></sub></form><legend id='1sLns'></legend><bdo id='1sLns'><pre id='1sLns'><center id='1sLns'></center></pre></bdo></b><th id='1sLns'></th></span></q></dt></tr></i><div id='1sLns'><tfoot id='1sLns'></tfoot><dl id='1sLns'><fieldset id='1sLns'></fieldset></dl></div>

        相对时间跨度的自定义格式

        时间:2023-08-31
          • <bdo id='Yx964'></bdo><ul id='Yx964'></ul>
            <legend id='Yx964'><style id='Yx964'><dir id='Yx964'><q id='Yx964'></q></dir></style></legend>

                  <i id='Yx964'><tr id='Yx964'><dt id='Yx964'><q id='Yx964'><span id='Yx964'><b id='Yx964'><form id='Yx964'><ins id='Yx964'></ins><ul id='Yx964'></ul><sub id='Yx964'></sub></form><legend id='Yx964'></legend><bdo id='Yx964'><pre id='Yx964'><center id='Yx964'></center></pre></bdo></b><th id='Yx964'></th></span></q></dt></tr></i><div id='Yx964'><tfoot id='Yx964'></tfoot><dl id='Yx964'><fieldset id='Yx964'></fieldset></dl></div>

                • <tfoot id='Yx964'></tfoot>

                  <small id='Yx964'></small><noframes id='Yx964'>

                    <tbody id='Yx964'></tbody>
                  本文介绍了相对时间跨度的自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试创建一种自定义格式来显示经过的时间.

                  I'm trying to create an custom format to display the elapsed time.

                  现在我正在使用:

                  CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(
                       dateObject.getTime(), 
                       System.currentTimeMillis(), 
                       DateUtils.SECOND_IN_MILLIS,
                       flags);
                  

                  这会返回像这样的相对时间跨度:

                  This returns relative time spans like this:

                  • 1 分钟前
                  • 36 分钟前
                  • 4 小时前

                  ...

                  我想要做的是显示这个,像这样:

                  What I'm trying to do is display this, like this:

                  • 1m
                  • 36m
                  • 4 小时

                  ...

                  我知道 DateUtils 有 FORMAT_ABBREV_ALL 标志,但这并没有像我想要的那样缩写字符串......

                  I know DateUtils has FORMAT_ABBREV_ALL flag, but this doesn't abbreviate the string like I want to...

                  我怎样才能为此创建一些自定义的东西?

                  How can I create something custom for this ?

                  推荐答案

                  我已经在我的 twitter 模块中完成了这个

                  i have done this in my twitter module

                  public static String getTimeString(Date fromdate) {
                  
                      long then;
                      then = fromdate.getTime();
                      Date date = new Date(then);
                  
                      StringBuffer dateStr = new StringBuffer();
                  
                      Calendar calendar = Calendar.getInstance();
                      calendar.setTime(date);
                      Calendar now = Calendar.getInstance();
                  
                      int days = daysBetween(calendar.getTime(), now.getTime());
                      int minutes = hoursBetween(calendar.getTime(), now.getTime());
                      int hours = minutes / 60;
                      if (days == 0) {
                  
                          int second = minuteBetween(calendar.getTime(), now.getTime());
                          if (minutes > 60) {
                  
                              if (hours >= 1 && hours <= 24) {
                                  dateStr.append(hours).append("h");
                              }
                  
                          } else {
                  
                              if (second <= 10) {
                                  dateStr.append("Now");
                              } else if (second > 10 && second <= 30) {
                                  dateStr.append("few seconds ago");
                              } else if (second > 30 && second <= 60) {
                                  dateStr.append(second).append("s");
                              } else if (second >= 60 && minutes <= 60) {
                                  dateStr.append(minutes).append("m");
                              }
                          }
                      } else
                  
                      if (hours > 24 && days <= 7) {
                          dateStr.append(days).append("d");
                      } else {
                          dateStr.append(twtimeformat.format(date));
                      }
                  
                      return dateStr.toString();
                  }
                  
                  public static int minuteBetween(Date d1, Date d2) {
                      return (int) ((d2.getTime() - d1.getTime()) / DateUtils.SECOND_IN_MILLIS);
                  }
                  
                  public static int hoursBetween(Date d1, Date d2) {
                      return (int) ((d2.getTime() - d1.getTime()) / DateUtils.MINUTE_IN_MILLIS);
                  }
                  
                  public static int daysBetween(Date d1, Date d2) {
                      return (int) ((d2.getTime() - d1.getTime()) / DateUtils.DAY_IN_MILLIS);
                  }
                  

                  这篇关于相对时间跨度的自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:android.text.format.DateFormat 和 java.text.DateFormat 有什么 下一篇:Objective-C如何打印出浮点数的前导0?

                  相关文章

                  最新文章

                    <small id='Ocypb'></small><noframes id='Ocypb'>

                  1. <legend id='Ocypb'><style id='Ocypb'><dir id='Ocypb'><q id='Ocypb'></q></dir></style></legend>

                    <tfoot id='Ocypb'></tfoot>

                  2. <i id='Ocypb'><tr id='Ocypb'><dt id='Ocypb'><q id='Ocypb'><span id='Ocypb'><b id='Ocypb'><form id='Ocypb'><ins id='Ocypb'></ins><ul id='Ocypb'></ul><sub id='Ocypb'></sub></form><legend id='Ocypb'></legend><bdo id='Ocypb'><pre id='Ocypb'><center id='Ocypb'></center></pre></bdo></b><th id='Ocypb'></th></span></q></dt></tr></i><div id='Ocypb'><tfoot id='Ocypb'></tfoot><dl id='Ocypb'><fieldset id='Ocypb'></fieldset></dl></div>

                        <bdo id='Ocypb'></bdo><ul id='Ocypb'></ul>