前一段时间用于Android / java [英] Time ago for Android/java

查看:150
本文介绍了前一段时间用于Android / java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java和android的新手。我的一个Android新闻应用程序,我将时间显示为2014年8月7日,上午8:20

I am newbie to java and android. One of my android news app i am displaying the time as "7 August 2014, 8:20 am"

但我需要显示如下:

5 mins ago    
1 hour ago    
2 days ago

我们很多时候发现了很多图书馆,joda。但我不知道如何将其添加到我的Android应用程序。
即使这个链接 https://stackoverflow.com/a/13018647/2020685 也告诉我。
但是如何将我的日期和时间传递给它。

Found many libraries such us pretty time, joda. But i dont know how to add it to my android app. Even this link https://stackoverflow.com/a/13018647/2020685 show me. But how to pass my date and time into it.

任何简单的代码都可以。

Any simple code to do it.

谢谢

推荐答案

您要显示的内容称为相对时间显示。 Android提供方法
来显示相对于当前时间的时间。您不必为此目的使用任何第三方库。

What you want to display is called as the Relative time display. Android provides methods to display time relative to your current time. You don't have to use any third party library just for this purpose.

你可以使用

DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)



请参阅此处的文档



例如。

Refer docs Here

eg.

DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);

UPDATE1:

您可以尝试按照传递日期并获取毫秒数。

You can try following to pass your date and get the milliseconds for it.

public static long getDateInMillis(String srcDate) {
    SimpleDateFormat desiredFormat = new SimpleDateFormat(
        "d MMMM yyyy, hh:mm aa");

    long dateInMillis = 0;
    try {
        Date date = desiredFormat.parse(srcDate);
        dateInMillis = date.getTime();
        return dateInMillis;
    } catch (ParseException e) {
        Log.d("Exception while parsing date. " + e.getMessage());
        e.printStackTrace();
    }

    return 0;
    }

希望对您有帮助。

这篇关于前一段时间用于Android / java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆