如何在android中设置时间

时间:2016-03-24 14:28:06

标签: android datetime

我从服务器获取datetime作为string。我试图设定时间前的那种事情。与2 minutes ago12 hours ago

相同

所以我深入研究谷歌搜索并最终得到一个类来处理很多其他开发人员引用的那个。

问题是,我在这里真的很奇怪。

例如: 2016-03-24 13:08:20的日期字符串最终为about 11 hours ago,但这不是真的。

当我尝试现在的时间/当前我的时间是15 hours ago

我真的不知道出了什么问题。有什么想法吗?

TimeAgo.class:

public class TimeAgo {

    protected Context context;

    public TimeAgo(Context context) {
        this.context = context;
    }

    public String timeAgo(Date date) {
        return timeAgo(date.getTime());
    }

    public String timeAgo(long millis) {
        long diff = new Date().getTime() - millis;

        Resources r = context.getResources();

        String prefix = r.getString(R.string.time_ago_prefix);
        String suffix = r.getString(R.string.time_ago_suffix);

        double seconds = Math.abs(diff) / 1000;
        double minutes = seconds / 60;
        double hours = minutes / 60;
        double days = hours / 24;
        double years = days / 365;

        String words;

        if (seconds < 45) {
            words = r.getString(R.string.time_ago_seconds, Math.round(seconds));
        } else if (seconds < 90) {
            words = r.getString(R.string.time_ago_minute, 1);
        } else if (minutes < 45) {
            words = r.getString(R.string.time_ago_minutes, Math.round(minutes));
        } else if (minutes < 90) {
            words = r.getString(R.string.time_ago_hour, 1);
        } else if (hours < 24) {
            words = r.getString(R.string.time_ago_hours, Math.round(hours));
        } else if (hours < 42) {
            words = r.getString(R.string.time_ago_day, 1);
        } else if (days < 30) {
            words = r.getString(R.string.time_ago_days, Math.round(days));
        } else if (days < 45) {
            words = r.getString(R.string.time_ago_month, 1);
        } else if (days < 365) {
            words = r.getString(R.string.time_ago_months, Math.round(days / 30));
        } else if (years < 1.5) {
            words = r.getString(R.string.time_ago_year, 1);
        } else {
            words = r.getString(R.string.time_ago_years, Math.round(years));
        }

        StringBuilder sb = new StringBuilder();

        if (prefix != null && prefix.length() > 0) {
            sb.append(prefix).append(" ");
        }

        sb.append(words);

        if (suffix != null && suffix.length() > 0) {
            sb.append(" ").append(suffix);
        }

        return sb.toString().trim();
    }
}

我如何使用它:

 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
// example of time format 2016-03-24 13:08:20
                                Date date = formatter.parse(jObject.getString("time_stamp")); // You will need try/catch around this
                                long millis = date.getTime();
                                TimeAgo timeAgo = new TimeAgo(getApplicationContext());

 //where i set the time
rm.settime_stamp(String.valueOf(timeAgo.timeAgo(millis)));

0 个答案:

没有答案