Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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_Html_Date_Time - Fatal编程技术网

Android 谷歌的社交网站,比如;“以前”;时间跨度

Android 谷歌的社交网站,比如;“以前”;时间跨度,android,html,date,time,Android,Html,Date,Time,我一直在尝试使用 …生成类似于Google Hangouts应用程序的运行时间字符串 但是,我得到的字符串如下: 1分钟前 58分钟前 10小时前 昨天 三天前 一周前 Hangouts应用程序显示如下字符串: 现在 58分钟前 11:05(如果时间超过1小时,则显示原始时间) 昨天 Wed(如果日期超过1天前,则显示日期) 7月21日(如果超过一周前,将显示日期) 我要找的就是像这样的风格 只是好奇我是否在这个方法中使用了错误的标志,或者Hangouts应用程序是否使用了不同的方法/库来

我一直在尝试使用

…生成类似于Google Hangouts应用程序的运行时间字符串

但是,我得到的字符串如下:

1分钟前
58分钟前
10小时前
昨天
三天前
一周前
Hangouts应用程序显示如下字符串:

现在
58分钟前
11:05(如果时间超过1小时,则显示原始时间)
昨天
Wed(如果日期超过1天前,则显示日期)
7月21日(如果超过一周前,将显示日期)
我要找的就是像这样的风格


只是好奇我是否在这个方法中使用了错误的标志,或者Hangouts应用程序是否使用了不同的方法/库来生成时间跨度字符串。如果DateUtils不能提供我想要的东西,我可以做一个自定义实现。

我最后做了不同DateUtils格式化程序的组合:

@SuppressLint("SimpleDateFormat")
public static String getTimeSpanString(Context mContext, long time, boolean showTime) {

    int flags = DateUtils.FORMAT_ABBREV_RELATIVE | DateUtils.FORMAT_ABBREV_ALL;

    if(showTime) {
        flags |= DateUtils.FORMAT_SHOW_TIME;
    }

    long now = new Date().getTime();
    long duration = now - time;

    if(duration < DateUtils.HOUR_IN_MILLIS) {
        return DateUtils.getRelativeTimeSpanString(time, now, 0, flags).toString();
    } else if (DateUtils.isToday(time)) {
        SimpleDateFormat timeInstance;
        if(DateFormat.is24HourFormat(mContext)) {
            timeInstance = new SimpleDateFormat("HH:mm");
        } else {
            timeInstance = new SimpleDateFormat("hh:mm a");
        }

        return timeInstance.format(time);
    } else if (duration < DateUtils.WEEK_IN_MILLIS) {
        return DateUtils.formatDateTime(mContext,time,flags | DateUtils.FORMAT_SHOW_WEEKDAY);
    }

    return DateUtils.formatDateTime(mContext, time, flags | DateUtils.FORMAT_SHOW_DATE);
}
@SuppressLint(“SimpleDataFormat”)
公共静态字符串getTimeSpanString(上下文mContext、长时间、布尔显示时间){
int flags=DateUtils.FORMAT_ABBREV_RELATIVE | DateUtils.FORMAT_ABBREV_ALL;
如果(表演时间){
flags |=DateUtils.FORMAT_SHOW_TIME;
}
long now=new Date().getTime();
长持续时间=现在-时间;
if(持续时间
我最终组合了不同的DateUtils格式化程序:

@SuppressLint("SimpleDateFormat")
public static String getTimeSpanString(Context mContext, long time, boolean showTime) {

    int flags = DateUtils.FORMAT_ABBREV_RELATIVE | DateUtils.FORMAT_ABBREV_ALL;

    if(showTime) {
        flags |= DateUtils.FORMAT_SHOW_TIME;
    }

    long now = new Date().getTime();
    long duration = now - time;

    if(duration < DateUtils.HOUR_IN_MILLIS) {
        return DateUtils.getRelativeTimeSpanString(time, now, 0, flags).toString();
    } else if (DateUtils.isToday(time)) {
        SimpleDateFormat timeInstance;
        if(DateFormat.is24HourFormat(mContext)) {
            timeInstance = new SimpleDateFormat("HH:mm");
        } else {
            timeInstance = new SimpleDateFormat("hh:mm a");
        }

        return timeInstance.format(time);
    } else if (duration < DateUtils.WEEK_IN_MILLIS) {
        return DateUtils.formatDateTime(mContext,time,flags | DateUtils.FORMAT_SHOW_WEEKDAY);
    }

    return DateUtils.formatDateTime(mContext, time, flags | DateUtils.FORMAT_SHOW_DATE);
}
@SuppressLint(“SimpleDataFormat”)
公共静态字符串getTimeSpanString(上下文mContext、长时间、布尔显示时间){
int flags=DateUtils.FORMAT_ABBREV_RELATIVE | DateUtils.FORMAT_ABBREV_ALL;
如果(表演时间){
flags |=DateUtils.FORMAT_SHOW_TIME;
}
long now=new Date().getTime();
长持续时间=现在-时间;
if(持续时间
您的问题不清楚。如果要显示日期时间,只需使用格式化程序创建日期时间的字符串表示形式,同时完全避免使用相对时间类。也许你可以编辑你的问题来澄清并发布更多你想要的例子;返回时间跨度字符串与hangots应用程序显示时间跨度字符串的方式。您的问题不清楚。如果要显示日期时间,只需使用格式化程序创建日期时间的字符串表示形式,同时完全避免使用相对时间类。也许你可以编辑你的问题来澄清并发布更多你想要的例子;返回时间跨度字符串与hangots应用程序显示时间跨度字符串的方式。