Android 如何知道json日期的运行时间

Android 如何知道json日期的运行时间,android,json,date,time,elapsedtime,Android,Json,Date,Time,Elapsedtime,我有一个项目列表,它通过json链接从服务器获取数据,其中一个属性是项目创建的日期/时间,如下所示: created_at: "2017-05-13T08:53:40Z" 我想在每个项目顶部的文本视图中显示这一次,但以Facebook状态显示,如format=> 1分钟前| 2小时前| 4天前| 5个月前等 我怎么能做到呢 更新 这是我的最新尝试 public String getOrderTime() { GregorianCalendar now = new Gregoria

我有一个项目列表,它通过json链接从服务器获取数据,其中一个属性是项目创建的日期/时间,如下所示:

created_at: "2017-05-13T08:53:40Z"
我想在每个项目顶部的文本视图中显示这一次,但以Facebook状态显示,如format=>

1分钟前| 2小时前| 4天前| 5个月前等

我怎么能做到呢

更新

这是我的最新尝试

  public String getOrderTime() {

    GregorianCalendar now = new GregorianCalendar();
    Date time = getTime(); // getTime is coming from json

    Log.i("Json Time ", time + "");

    long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
    long timePortion = time.getTime() % MILLIS_PER_DAY;

    Log.i("New Json Time ", timePortion + "");
    Log.i("Time Now ", now.getTimeInMillis() + "");

    String elapsed_time = DateUtils.getRelativeTimeSpanString(
            timePortion,
            now.getTimeInMillis(),
            DateUtils.SECOND_IN_MILLIS)
            .toString();

    Log.i("Elapsed Time", elapsed_time);

    return elapsed_time;
 }
这是日志,显然出了问题

I/Json Time: Sun May 14 03:27:22 EDT 2017
I/New Json Time: 26842000
I/Time Now: 1494747376756
I/Elapsed Time: Jan 1, 1970

更新2

新方法

  public String getOrderTime() {

    long end_time = theTime().getTime();  // json time
    long right_now = System.currentTimeMillis(); // right now

    Log.i("Time Now ", right_now + "");
    Log.i("Json Time ", end_time + "");

    long difference = right_now - end_time;
    Log.i("Elapsed Time", difference + "");

    if (difference < 60) {

        Log.i("Time in Minutes", difference + "m");
        return  difference + "m";

    } else {
        difference /= 60;
        if (difference < 24) {

            Log.i("Time in Hours", difference + "h");
            return difference + "h";

        } else {
            difference /= 30;

            Log.i("Time in Months", difference + "mo");
            return difference + "mo";
        }
    }

首先,您需要计算两个日期之间的差异,即您给定的日期和当前日期:

long difference = endDate.getTime() - startDate.getTime()
差异
将以毫秒为单位保存差异

difference /= 60000; //minutes

if (difference < 60) {
    //minutes
} else {
    difference /= 60; //hours
    if (difference < 24) {
        //hours
    } else {
        difference /= 24; //days
        if (difference < 30) {
            //days
        } else {
            //months
        }
    }
}
差/=60000//会议记录
如果(差值<60){
//会议记录
}否则{
差值/=60;//小时
如果(差异<24){
//小时数
}否则{
差值/=24;//天
如果(差值<30){
//日子
}否则{
//月份
}
}
}

解析日期并将其与当前日期进行比较。在哪一点上你有困难?@Henry检查更新第一个问题是只比较一个时间戳的时间部分和完整的第二个时间戳。如果你有机会展示如何解决这个问题,我真的需要你的帮助!我需要用一种有意义的格式来展示它。。!?请检查更新2@SamZar您遗漏了这一行
difference/=60000@SamZar,亨利是对的。你在更新中使用的是毫秒而不是秒。哦,我的糟糕。。我认为这是一个更有意义的结果,非常感谢你,但是如果我想在
之前显示
,那会是什么情况?@SamZar是的,你是对的,请查看我的编辑。
difference /= 60000; //minutes

if (difference < 60) {
    //minutes
} else {
    difference /= 60; //hours
    if (difference < 24) {
        //hours
    } else {
        difference /= 24; //days
        if (difference < 30) {
            //days
        } else {
            //months
        }
    }
}