不同的时间在不同的设备中有不同的时间

时间:2015-06-23 19:07:53

标签: android date time timezone timeago

我想获得一些数据的时间,例如我在三个小时前将一些数据插入到数据库中,但在具有不同时区的不同设备中,我看到了不同的timeAgo。

我该怎么办?
这是我的代码

public static String timeAgo(long millis) {     
    long diff = new Date().getTime() - millis;
    Resources r = G.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();
}

0 个答案:

没有答案