Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 相对时间跨度的自定义格式_Android_Date_Format - Fatal编程技术网

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

Android 相对时间跨度的自定义格式,android,date,format,Android,Date,Format,我正在尝试创建一个自定义格式来显示经过的时间 现在我正在使用: CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString( dateObject.getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS, flags); 这将返回如下所示的相对时间跨度: 一分钟前 36分钟前 4小时前 我想做的

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

现在我正在使用:

CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(
     dateObject.getTime(), 
     System.currentTimeMillis(), 
     DateUtils.SECOND_IN_MILLIS,
     flags);
这将返回如下所示的相对时间跨度:

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

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

  • 1米
  • 36米
  • 4h

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


我怎样才能为它创建自定义的东西呢?

创建一个方法来获得这样的日期差异

public static String getDateDifferenceForDisplay(Date inputdate) {
    Calendar now = Calendar.getInstance();
    Calendar then = Calendar.getInstance();

    now.setTime(new Date());
    then.setTime(inputdate);

    // Get the represented date in milliseconds
    long nowMs = now.getTimeInMillis();
    long thenMs = then.getTimeInMillis();

    // Calculate difference in milliseconds
    long diff = nowMs - thenMs;

    // Calculate difference in seconds
    long diffMinutes = diff / (60 * 1000);
    long diffHours = diff / (60 * 60 * 1000);
    long diffDays = diff / (24 * 60 * 60 * 1000);

    if (diffMinutes < 60) {
        return diffMinutes + " m";

    } else if (diffHours < 24) {
        return diffHours + " h";

    } else if (diffDays < 7) {
        return diffDays + " d";

    } else {

        SimpleDateFormat todate = new SimpleDateFormat("MMM dd",
                Locale.ENGLISH);

        return todate.format(inputdate);
    }
} 
String dateForDisplay = getDateDifferenceForDisplay(inputDate);

我已经在我的twitter模块中完成了这项工作

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);
}
公共静态字符串getTimeString(日期fromdate){
很久以前;
然后=fromdate.getTime();
日期=新日期(然后);
StringBuffer dateStr=新的StringBuffer();
日历=Calendar.getInstance();
日历。设置时间(日期);
Calendar now=Calendar.getInstance();
int days=daysBetween(calendar.getTime(),now.getTime());
int minutes=hoursbween(calendar.getTime(),now.getTime());
整小时=分钟/60;
如果(天数==0){
int second=minuteBetween(calendar.getTime(),now.getTime());
如果(分钟>60){

如果(hours>=1&&hours没有用于此的内置方法/实用程序,但使用可用的
DateUtils
常量实现一个方法/实用程序非常简单:

private static final String ABBR_YEAR = "y";
private static final String ABBR_WEEK = "w";
private static final String ABBR_DAY = "d";
private static final String ABBR_HOUR = "h";
private static final String ABBR_MINUTE = "m";

public static String getAbbreviatedTimeSpan(long timeMillis) {
    long span = Math.max(System.currentTimeMillis() - timeMillis, 0);
    if (span >= DateUtils.YEAR_IN_MILLIS) {
        return (span / DateUtils.YEAR_IN_MILLIS) + ABBR_YEAR;
    }
    if (span >= DateUtils.WEEK_IN_MILLIS) {
        return (span / DateUtils.WEEK_IN_MILLIS) + ABBR_WEEK;
    }
    if (span >= DateUtils.DAY_IN_MILLIS) {
        return (span / DateUtils.DAY_IN_MILLIS) + ABBR_DAY;
    }
    if (span >= DateUtils.HOUR_IN_MILLIS) {
        return (span / DateUtils.HOUR_IN_MILLIS) + ABBR_HOUR;
    }
    return (span / DateUtils.MINUTE_IN_MILLIS) + ABBR_MINUTE;
}

谢谢。最后我实现了类似的方法。我希望有一个更简单的方法,比如我问题中的方法,但后面有一些定制。这仍然是一个很好的答案。我会将其标记为解决方案。这很好,只是提前一天而已。知道为什么吗??